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

时间:2015-10-11 02:45:04

标签: c# mysql datagridview

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);
                } 

我收到错误

  

System.NullReferenceException未处理         的HResult = -2147467261         Message =对象引用未设置为对象的实例。

是的,请帮助我。我试图在for循环中执行查询,但是我得到的值来自gridview。

1 个答案:

答案 0 :(得分:0)

谢谢你们关心的问题:)。我只是解决了问题:))我只是用这个

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

而不是这个

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