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

我得到一個錯誤

未處理System.NullReferenceException HResult = -2147467261 Message =對象引用未設置為對象的實例。

任何人都可以幫助我。 我試圖在for循環中執行查詢,但是即時通訊的值來自gridview。

謝謝你們的關心:)。 我只是解決問題:))我只是用這個

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

而不是這個

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