I'd like to get the total amount of purchases according to the current month, but in my SQL Server database I set the date field to write nvarchar(50). When the code is executed, I get an error:
Conversion failed when converting date and/or time from character string.
Is there a way to change the code to make it accept nvarchar(50) field type but I don't want to change it from database to date type I want it to work on nvarchar(50) type.
Code:
int month = Convert.ToInt32(DateTime.Now.Month.ToString());
DataTable tblItems_40 = new DataTable();
tblItems_40 = db8.readData("SELECT SUM(Total) FROM Buy_Detalis WHERE MONTH(Date)
='" + month + "' ", "");
tblItems8 = db8.readData("SELECT * From Buy_Detalis WHERE Date ='" +
month.ToString() + "'", "");
if (tblItems8.Rows.Count <= 0)
{
labelControl6.Text = "0.ج.م";
}
else if (tblItems8.Rows.Count >= 1)
{
labelControl6.Text = (tblItems_40.Rows[0][0].ToString)();
labelControl6.Text = Decimal.Parse(labelControl6.Text).ToString("C");
}