Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
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);
           }
       }


Please tell me how can I read all rows from DataGridView ? Where is the problem to insert ?
Posted
Comments
Santhosh Kumar Jayaraman 13-Aug-12 11:53am    
Where you are getting the error??
[no name] 13-Aug-12 15:05pm    
Did you find the solution ?
UL UL ALBAB 14-Aug-12 5:01am    
Yes, I have done it.
abdul rafi 3-Mar-14 3:01am    
how did u done it? please insert the code...

C#
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.
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
UL UL ALBAB 14-Aug-12 4:25am    
Thanks for your complement. Here, when I am calling adapter than this is not working. But, when I am calling ExecuteNonQuery than this is working in my logic.
Pratik65 14-Dec-12 14:01pm    
can u explain me ur code because i want to de the same thing but i want to insert the details multiple times i mean for all rows in the datagrid
Christian Graus 16-Aug-12 13:26pm    
Yes, your old code made no sense, I assumed it was some automagic thing. Actually calling a sproc, is going to work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900