简体   繁体   English

我如何从datagridview获取所有数据并将其插入数据库

[英]How can i get all the data from datagridview and insert it into database

string IdDB = "select transid from transtbl where fullname = '"+textBox6.Text+"'";
                con.SelectRec(IdDB, dt);
                string IdDB2 = dt.Rows[0].ItemArray[0].ToString();

                for (int rows = 0; rows < dataGridView2.Rows.Count; rows++)
                {
                    int pProdID = int.Parse(dataGridView2.Rows[rows].Cells[0].Value.ToString());
                    string pProdName = dataGridView2.Rows[rows].Cells[1].Value.ToString();
                    double pPrice = double.Parse(dataGridView2.Rows[rows].Cells[2].Value.ToString());
                    int qQty = int.Parse(dataGridView2.Rows[rows].Cells[3].Value.ToString());
                    double tttTotal = double.Parse(dataGridView2.Rows[rows].Cells[4].Value.ToString());

                    string transdltdSql = "Insert into transdetailedtbl (transID, ProdID, ProdPrice, Qty, Total) value ("+int.Parse(IdDB2)+","+pProdID+","+pPrice+","+qQty+","+tttTotal+")";
                    con.ModRec(transdltdSql);
                } 

I get an error 我得到一个错误

System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object. 未处理System.NullReferenceException HResult = -2147467261 Message =对象引用未设置为对象的实例。

anyone help me please. 任何人都可以帮助我。 i was trying to execute query in a for loop but the value im getting is from the gridview. 我试图在for循环中执行查询,但是即时通讯的值来自gridview。

thank you guys for the concern :). 谢谢你们的关心:)。 i just solve the problem :)) i just use this 我只是解决问题:))我只是用这个

int pProdID = Convert.ToInt32(dataGridView2.Rows[rows].Cells[0].Value); 

rather than this 而不是这个

int pProdID = int.Parse(dataGridView2.Rows[rows].Cells[0].Value.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM