Skip to content

Error In adding to table !!

younis abdeljawwad

Asked May 24, 2012, 11:10 AM EDTSolved
Share
LinkedInX
i try to insert to table data but an error Occurs ,,,,,my table name is "class time"

here is the code 

            int class_maxid = 17;
            int starttime = 95;
            int endtime = 200;
            string courseday = "saturday";
            //insert to the clas table the values
            SqlCommand cmd5 = new SqlCommand("INSERT INTO class time(class_id,start_from,end_to,day)Values(@cls_id,@s_from,@e_to,@d)", cn1);
            cmd5.Parameters.AddWithValue("@cls_id", class_maxid);
            cmd5.Parameters.AddWithValue("@s_from", starttime);
            cmd5.Parameters.AddWithValue("@e_to", endtime);
            cmd5.Parameters.AddWithValue("@d", courseday);
            cmd5.ExecuteNonQuery();

the error is    "Incorrect syntax near 'time'."

!!!!!!

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • ✓ Accepted answer0
    if your table name contains a space as yours seems to, you must surround the table name in square brackets [ and ] 

    try this
    SqlCommand cmd5 = new SqlCommand("INSERT INTO [class time](class_id,start_from,end_to,day)Values(@cls_id,@s_from,@e_to,@d)", cn1);
    0
  • 0
    Yes, that's right my friend, it works will 
    Thanks.
    0