我无法将数据从网格添加到数据库 [英] I am unable to add data from grid to database
问题描述
我有一个带有少量文本框的表单,通过它我可以使用数据表将数据添加到网格中,现在我想将表格(Tbl_Invoice)添加到表格(Tbl_Invoice),其中列为Invoice_Id(PK和标识规范为true),Product_Name(nvarchar( 50),金额(数字(10,2)),Product_Quantity(int),Product_Price(数字(10,2))。
编码是
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) //i have first 2 columns in which i took button field EDIT and REMOVE ,so how will start my loop
{ 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)
{ //on click button values of text are added into grid using datatable...i have 2 first column in which i took button field EDIT and Remove
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收到错误列名无效 Product_Price''在com.executenonquery上
请帮助
问候
i am getting an error "Invalid column name ''Product_Price'' "on com.executenonquery
pls help
regards
推荐答案
这是我的愚蠢/>
i写错了列名
感谢所有人查看它并为你的时间道歉
问候
it was my stupidity
i wrote wrong column name
thanks everyone for viewing it and appologize for ur time
regards
这篇关于我无法将数据从网格添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!