gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:34:01 25 4
gpt4 key购买 nike

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 was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object.

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

最佳答案

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

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

而不是这个

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

关于c# - 如何从 datagridview 中获取所有数据并将其插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33061036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com