在DataGridview中添加数据,然后插入到数据库中 [英] Add data in DataGridview then Insert into Database

查看:70
本文介绍了在DataGridview中添加数据,然后插入到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:对象引用未设置为对象的实例。



实际上,我想从DataGridView在数据库中插入数据。但是,在我的代码中,第一行插入了第一行,但其他行则没有。



我的代码:

Error:Object reference not set to an instance of an object.

Actually, I want to insert data in database from DataGridView. But, in my code 1ly first row is inserted but, other rows are not.

My Code:

private void btnSave_Click(object sender, EventArgs e)
       {
           int chkLoop;

           try
           {
               chkLoop = dataGridView1.Rows.Count;
               //chkLoop = chkLoop - 1;
               MessageBox.Show(chkLoop.ToString());
               foreach (DataGridViewRow row in dataGridView1.Rows)
               {
                   //MessageBox.Show(chkLoop.ToString());
                   //MessageBox.Show(row.co.ToString());
                   if (chkLoop != 0)
                   {
                       db.command.Parameters.Add("@ItemCode", SqlDbType.VarChar).Value = row.Cells["Category Code"].Value.ToString();
                       db.command.Parameters.Add("@ItemName", SqlDbType.VarChar).Value = row.Cells["Category"].Value.ToString();
                       //MessageBox.Show("" + row.Cells["Category Code"].Value.ToString());
                       db.Adapter("sp_NewItem", true);
                       chkLoop = chkLoop - 1;

                   }
               }
               MessageBox.Show("New Item added successfully.", "Successfully done", MessageBoxButtons.OK, MessageBoxIcon.Information);
               db.Adapter("select ItemName as [Item Name] from NewItem", "dt");
               dataGridPreviousList.DataSource = db.dataSet.Tables["dt"];
               //dataGridPreviousList.DataSource = db.Adapter("select ItemName as [Item Name] from NewItem");
           }
           catch (Exception err)
           {
               MessageBox.Show(err.Message);
           }
       }





请告诉我如何阅读所有行来自DataGridView?要插入的问题在哪里?



Please tell me how can I read all rows from DataGridView ? Where is the problem to insert ?

推荐答案

db.command.Parameters.Add("@ItemCode", SqlDbType.VarChar).Value = row.Cells["Category Code"].Value.ToString();
db.command.Parameters.Add("@ItemName", SqlDbType.VarChar).Value = row.Cells["Category"].Value.ToString();





当然你只是第一排。你永远不会告诉你的循环复制不同的行。它只是每次都复制相同的行。



Of course you are only getting the first row. You are never telling your loop to copy different rows. Its just copying the same row every time.


IT将有助于知道哪个行给出了错误,以及db是什么。通常,在循环中添加相同的参数不起作用。您需要将它们添加到循环外部,因此它们只添加一次,然后在循环内部,通过名称访问它们并设置它们的值。这可能是爆炸的一个可能原因。 'chkLoop'的东西是无用的,chkloop不能低于0,也没有做任何有用的东西。你的代码假设网格中的每个值都是新的并且需要插入,但是如果你清理我提到的方面,它应该以这种方式工作,据我所见。当然,如果你告诉我们什么对象是null以及哪个行有错误,那么我们也可能会发现其他问题。
IT would help to know what line gives the error, and what db is. In general, adding the same parameters in a loop, does not work. You need to add them OUTSIDE the loop, so they are added only once, then inside the loop, access them by name and set their value. That would be one possible reason for this to blow up. The 'chkLoop' stuff is usless, chkloop can't ever get to less than 0, nor does it do anything useful. Your code IS assuming every value in the grid is new and needs inserting, but if you clean up the aspect I mentioned, it should work that way, as far as I can see. Of course, if you told us what object was null and what line had the error, we might find other issues with it then, too.


这篇关于在DataGridview中添加数据,然后插入到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆