Reputation: 93
private void btnCheck_Click(object sender, EventArgs e)
{
string costring = connection();
string MyQuery = "select expense from dbo.KmtAccounts where date between '"+ txtFromDate +"' and '" +txtToDate +"' and registernumber='" + txtRegNo.Text + "'";
SqlConnection conn = new SqlConnection(costring);
SqlCommand cmd = new SqlCommand(MyQuery, conn);
conn.Open();
txtResult.Text = Convert.ToString(cmd.ExecuteScalar());
conn.Close();
}
Upvotes: 0
Views: 394
Reputation: 113352
The issue causing the exception is that the value in txtFromDate
and/or txtToDate
isn't understood as representing a date.
The bigger issue is that there seems to be little or nothing to prevent one of those values containing a valid value followed by ';delete from dbo.KmtAccounts;---
.
Both problems will be solved by making the query select expense from dbo.KmtAccounts where date between @from and @to and registernumber=@reg
and then using parameters to add the dates (as dates, not as strings) and reg-number into the query.
Upvotes: 2
Reputation: 8359
There are several point to improve within your code.
Upvotes: 3