0

Why I cant add the value on the last query (EventEndDate)

string insertQuery = "INSERT INTO eventreservation(EventID,CustomerName,CustomerIC," +
                      "CustomerPhone,StartDate,EndDate) VALUES('" + txtEventID.Text + "'," +
                      "'" + txtCustomerName.Text + "','" + txtCustomerIC.Text + "','" + 
                      txtCustomerPhone.Text + "','" + EventStartDate.Text + "'," + 
                      EventEndDate.Text + ")";
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109

1 Answers1

2

You have missed a single quote ' in your last entry:

," + EventEndDate.Text + "

That should be:

,'" + EventEndDate.Text + "'

However this kind of string concatenation is open for SQL injection. Try parameterized queries instead. Something like this:

string insertQuery = "INSERT INTO eventreservation(EventID,CustomerName,CustomerIC," +
                             "CustomerPhone,StartDate,EndDate)VALUES(@EventID,@CustomerName," +
                             "CustomerIC,@CustomerPhone,@StartDate,@EndDate)";
insertCommand.Parameters.AddWithValue(@EventID,txtEventID.Text);
//Other parameters

Although specify the type directly and use the Value property is more better than AddWithValue:

insertCommand.Parameters.Add("@EventID", SqlDbType.VarChar).Value = txtEventID.Text;
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109