2015-10-11 20 views
1
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); 
       } 

我得到一个错误我怎样才能从DataGridView的所有数据,并将其插入到数据库

System.NullReferenceException了未处理的HResult = -2147467261 消息=对象引用未设置为一个实例的一个对象。

任何人都可以帮助我。我试图在for循环中执行查询,但是我得到的值是来自gridview。

+0

的可能的复制(HTTP [什么是一个NullReferenceException,如何解决呢?]://计算器.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Backs

+0

您有一个SQL注入漏洞。 – SLaks

+0

你为什么不使用数据绑定的任何原因? –

回答

0

谢谢大家的关注:)。我只是解决问题:)),我只是用这个

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

,而不是这个

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