john kc
john kc

Reputation: 45

inserting data from excel to sql table

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";

}

Upvotes: 1

Views: 78

Answers (1)

Red fx
Red fx

Reputation: 1091

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

Upvotes: 1

Related Questions