i have one form with few textboxes through which i add data into the grid using datatable and now i want to add data of grid to the table (Tbl_Invoice) with columns Invoice_Id(PK and identity specification true),Product_Name(nvarchar(50),Amount(numeric(10,2)),Product_Quantity(int),Product_Price(numeric(10,2)).
coding is
protected void Button3_Click(object sender, EventArgs e)
{ foreach (GridViewRow g1 in grd.Rows)
{ SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ConnectionString);
SqlCommand com;
com = new SqlCommand("insert into Tbl_Invoice(Product_Name,Amount,Product_Quantity,Product_Price) values ('" + g1.Cells[2].Text + "'," + g1.Cells[3].Text + "," + g1.Cells[4].Text + "," + g1.Cells[5].Text + ")", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
Label1.Text = "Records inserted successfully";
} }
public partial class invoice : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataRow dr;
public SqlDataReader reader;
Query.product objprdt = new product(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
Query.customer objcus = new customer(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
Query.cal objcal = new cal(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
DataTable dt1 = new DataTable();
protected void Button2_Click(object sender, EventArgs e)
{
dt.Columns.Add("Product Name");
dt.Columns.Add("Amount");
dt.Columns.Add("Qty");
dt.Columns.Add("Product Price");
for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Product Name"] = grd.Rows[intCnt].Cells[2].Text;
dr["Amount"] = grd.Rows[intCnt].Cells[3].Text;
dr["Qty"] = grd.Rows[intCnt].Cells[4].Text;
dr["Product Price"] = grd.Rows[intCnt].Cells[5].Text;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Product Name"] = TextBox6.Text;
dr["Amount"] = TextBox5.Text;
dr["Qty"] = TextBox4.Text;
dr["Product Price"] = TextBox3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
i am getting an error "Invalid column name 'Product_Price' "on com.executenonquery
pls help
regards