1

Am getting an error while inserting data from excel to the database table. this is the error Incorrect syntax near 'NAME' this is my code:

 protected void btninsert_Click(object sender, EventArgs e)
{
    foreach (GridViewRow g1 in GridView1.Rows)
    {
        conStr = ConfigurationManager.ConnectionStrings["SqlConString"].ConnectionString;
        SqlConnection con = new SqlConnection(conStr);

        SqlCommand com = new SqlCommand("insert into MedicalItems (ITEM NAME,GROUP,ITEM TYPE,COST PRICE,SELLING PRICE,PURCHASE UOM,PURCHASE PACKAGING,DISPENSING UOM,QTY ON HAND,EXPIRY DATE,REORDER LEVEL,REORDER QUANTITY,BATCH#) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "','" + g1.Cells[10].Text + "','" + g1.Cells[11].Text + "','" + g1.Cells[12].Text + "','" + g1.Cells[13].Text + "')", con);

        con.Open();

        com.ExecuteNonQuery();

        con.Close();



    }

    Label2.Text = "Records inserted successfully";

}
2
  • 2
    ITEM NAME. Either, there is a colon missing or it should be [Item Name]. Commented Sep 19, 2017 at 13:28
  • Little Bobby Tables is also excited by this code. Use parameters! Commented Sep 19, 2017 at 13:30

1 Answer 1

1

Wrap the column names within [] like [ITEM NAME], for all the columns with whitespaces.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.