本文介绍了如何将gridview的所有行插入数据库中的另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我正在开发一个应用程序,其中将一个表中的值检索到Gridview中,并将该GridView的所有行插入到DB的另一张表中,并从下拉列表中选择一些其他值.该特定行已存在于表中,应该对其进行更新,否则应将其插入表中.
我尝试了此代码,但无法正常工作,请给我建议.
受保护的 无效 btnview0_Click(对象发件人,EventArgs e) { callgrid(); } 受保护的 无效 Btnrecv_Click(对象发件人,EventArgs e) { // callgrid(); int wid = Convert.ToInt32(ddlwarehouseid.SelectedItem.ToString()); // cmd = new SqlCommand(从PROD_INVENTORY I中选择I.QTY,在W仓库中,在REQUESTED_PROD_DETAIL P中在W.WAREHOUSE_ID = I.WAREHOUSE_ID AND P.PROD_GRP_ID = I.PROD_GRP_ID AND P.PRODUCT_ID = I.PRODUCT_ID,cn); // cn.Open(); // dr = cmd.ExecuteReader(); // dr.Read(); // 如果(dr.HasRows == true) // { // 尝试 // { for ( int i = 0 ; i < GridView1.Rows.Count; i ++) { cmd = 新 SqlCommand(" 从PROD_INVENTORY中选择I.QTY I,WAREHOUSES W,REQUESTED_PROD_DETAIL P,W.WAREHOUSE_ID =" + wid + " AND P.PROD_GRP_ID = " + GridView1.Rows [i] .Cells [ 1 ].文本+ " AND P.PRODUCT_ID =" + GridView1.Rows [i] .Cells [ 2 ].Text + " " ,cn); cn.Open(); dr = cmd.ExecuteReader(); Dr.Read(); 如果(dr.HasRows == true ) { 尝试 { cn.Close(); cmd = 新 SqlCommand(" 更新PROD_INVENTORY set QTY = QTY + " + GridView1.Rows [i] .Cells [ 3 ].文本+ " 其中WAREHOUSE_ID =" + wid + " AND PROD_GRP_ID =" + GridView1.Rows [i] .Cells [ 1 ].Text + " AND PRODUCT_ID =" + GridView1.Rows [i] .Cells [ 2 ].Text + " ",cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); } 捕获(例外) { Label3.Text = ex.Message.ToString(); } } 其他 { 尝试 { cn.Close(); cmd = 新 SqlCommand(" 插入PROD_INVENTORY值(" + GridView1.Rows [i] .Cells [ 1 ].文本+ " ," + GridView1.Rows [i] .Cells [ 2 ].文本+ " ,'1','1'," + GridView1.Rows [i] .Cells [ 3 ].Text + " ,'sajid','" + DateTime. Now.ToString()+ " ','sajid','" + DateTime.Now.ToString( )+ " ','Y','" + ddlwarehouseid.SelectedValue.ToString()+ " ')" ,cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); } 捕获(例外) { Label3.Text = ex.Message.ToString(); } } } } 公共 无效 Warehouseid() { da = 新 SqlDataAdapter(" 从WAREHOUSES中选择WAREHOUSE_ID" ,cn); DataSet ds = 新 DataSet(); da.Fill(ds); ddlwarehouseid.DataSource = ds; ddlwarehouseid.DataTextField = " WAREHOUSE_ID" ; ddlwarehouseid.DataValueField = " WAREHOUSE_ID" ; ddlwarehouseid.DataBind(); } 公共 无效 callgrid() { 字符串 id = ddlreqid.SelectedItem.ToString(); da = 新建 SqlDataAdapter(" 选择rd.REQUEST_ID,rd .PROD_GRP_ID,rd.PRODUCT_ID,rd.QTY,rd.UNIT_PRICE,rm.SUPPLIER_ID,rm.TOTAL_AMOUNT,rm.EFFECTIVE_END_DATE,rm.EFFECTIVE_START_DATE,rm.DESCRIPTION,rm.STATUS from REQUESTED_PROD_STRMIDDERMIDRE .REQUEST_ID和rd.REQUEST_ID ='" + id + " '",cn) ; da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } }
请给我一些建议
解决方案
hi
I am developing an application where, I am retriving values from one table in to a gridview and all rows of that gridview should be inserted to the other table in DB.with some additional values which I select from the dropdown list.on some condn if that particular row already exists in a table it should just get update otherwise it should get inserted into table.
I tried this code but its not working properly please advice me.
protected void btnview0_Click(object sender, EventArgs e) { callgrid(); } protected void Btnrecv_Click(object sender, EventArgs e) { //callgrid(); int wid = Convert.ToInt32( ddlwarehouseid.SelectedItem.ToString()); //cmd = new SqlCommand("SELECT I.QTY FROM PROD_INVENTORY I, WAREHOUSES W, REQUESTED_PROD_DETAIL P WHERE W.WAREHOUSE_ID=I.WAREHOUSE_ID AND P.PROD_GRP_ID=I.PROD_GRP_ID AND P.PRODUCT_ID=I.PRODUCT_ID", cn); //cn.Open(); //dr = cmd.ExecuteReader(); //dr.Read(); //if (dr.HasRows == true) // { // try // { for (int i = 0; i < GridView1.Rows.Count; i++) { cmd = new SqlCommand("SELECT I.QTY FROM PROD_INVENTORY I, WAREHOUSES W, REQUESTED_PROD_DETAIL P WHERE W.WAREHOUSE_ID=" +wid + " AND P.PROD_GRP_ID=" + GridView1.Rows[i].Cells[1].Text + " AND P.PRODUCT_ID=" + GridView1.Rows[i].Cells[2].Text +"" , cn); cn.Open(); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows == true) { try{ cn.Close(); cmd = new SqlCommand("update PROD_INVENTORY set QTY=QTY+" + GridView1.Rows[i].Cells[3].Text + " where WAREHOUSE_ID=" + wid+ " AND PROD_GRP_ID=" + GridView1.Rows[i].Cells[1].Text + " AND PRODUCT_ID=" + GridView1.Rows[i].Cells[2].Text + "", cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); } catch (Exception ex) { Label3.Text= ex.Message.ToString(); } } else { try { cn.Close(); cmd = new SqlCommand(" insert into PROD_INVENTORY values (" + GridView1.Rows[i].Cells[1].Text + "," + GridView1.Rows[i].Cells[2].Text + ",'1','1'," + GridView1.Rows[i].Cells[3].Text + ",'sajid','" + DateTime.Now.ToString() + "','sajid','" + DateTime.Now.ToString() + "','Y','" + ddlwarehouseid.SelectedValue.ToString() + "')", cn); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); } catch (Exception ex) { Label3.Text= ex.Message.ToString(); } } } } public void warehouseid() { da=new SqlDataAdapter("select WAREHOUSE_ID from WAREHOUSES",cn); DataSet ds = new DataSet(); da.Fill(ds); ddlwarehouseid.DataSource = ds; ddlwarehouseid.DataTextField = "WAREHOUSE_ID"; ddlwarehouseid.DataValueField = "WAREHOUSE_ID"; ddlwarehouseid.DataBind(); } public void callgrid() { string id = ddlreqid.SelectedItem.ToString(); da = new SqlDataAdapter(" select rd.REQUEST_ID,rd.PROD_GRP_ID,rd.PRODUCT_ID,rd.QTY,rd.UNIT_PRICE,rm.SUPPLIER_ID,rm.TOTAL_AMOUNT,rm.EFFECTIVE_END_DATE,rm.EFFECTIVE_START_DATE,rm.DESCRIPTION,rm.STATUS from REQUESTED_PROD_DETAIL rd,REQUESTED_PROD_MST rm where rd.REQUEST_ID= rm.REQUEST_ID and rd.REQUEST_ID='" + id + "'", cn); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } }
please give me some advice
解决方案
这篇关于如何将gridview的所有行插入数据库中的另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!