Kasuni Abegunawardana

Kasuni Abegunawardana

  • NA
  • 211
  • 124.5k

Incorrect syntax near '2017-12-04'.

Dec 28 2017 9:06 AM
Hi all,
 
I got an error like this. Why is that? I want to select two data ranges using two text boxes and fill it into grid view?
 
 
Incorrect syntax near '2017-12-04'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '2017-12-04'.
Source Error:
Line 26: SqlDataAdapter sda = new SqlDataAdapter(cmd);
Line 27: DataTable dt = new DataTable();
Line 28: sda.Fill(dt);
Line 29: GridView1.DataSource = dt;
Line 30: GridView1.DataBind();
 
 
 
 
protected void btn_Search_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM InventoryRecords_tb where BuyingDate '" + txtStartDate.Text + "' between '" + txtEndDate.Text + "' ", con);
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
sda.Dispose();
cmd.Dispose();
con.Close();
}
 

Answers (3)