Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used 2 gridview and I used this code behind:
C#
protected void SAVE_bt_Click(object sender, EventArgs e)
    {
        if (Hidden_txt.Text == Convert.ToString(1))
        {
            
            for (int i = 0; i < FirstDD_gv.Rows.Count; i++)
            {
                GridViewRow row = FirstDD_gv.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

                if (isChecked)
                {

                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = new SqlConnection(Class1.CnnStr);
                    cmd.CommandText = "insert into table values(@Po_Number,@Line_No,@DelDate,@First_Sec)";
                    cmd.Connection.Open();
                    cmd.Parameters.AddWithValue("@Po_Number", PoNumber_lbl.Text);
                    cmd.Parameters.AddWithValue("@Line_No", FirstDD_gv.Rows[i].Cells[1].Text);
                    cmd.Parameters.AddWithValue("@DelDate", DeliveryDate_txt.Text);
                    cmd.Parameters.AddWithValue("@First_Sec", Hidden_txt.Text);
                    cmd.ExecuteNonQuery();

                }
            }
        }
        else
        {
           
            for (int i = 0; i < SecondDD_gv.Rows.Count; i++)
            {
                GridViewRow row = SecondDD_gv.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

                if (isChecked)
                {

                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = new SqlConnection(Class1.CnnStr);
                    cmd.CommandText = "insert into table values(@Po_Number,@Line_No,@DelDate,@First_Sec)";
                    cmd.Connection.Open();
                    cmd.Parameters.AddWithValue("@Po_Number", PoNumber_lbl.Text);
                    cmd.Parameters.AddWithValue("@Line_No", SecondDD_gv.Rows[i].Cells[1].Text);
                    cmd.Parameters.AddWithValue("@DelDate", DeliveryDate_txt.Text);
                    cmd.Parameters.AddWithValue("@First_Sec", Hidden_txt.Text);
                    cmd.ExecuteNonQuery();

                }
            }
        }


    }
depends on my hidden_txt.text that just shows one of them
when I used first gridview I don't have any problem but when I used second grid view It shows me this error:
"Object reference not set to an instance of an object"
what should I do?
Posted
Updated 26-Nov-11 23:20pm
v2
Comments
Neema Derakhshan 27-Nov-11 5:19am    
How many cells do you have in a row,in second gridView??
SecondDD_gv.Rows[i].Cells[1].Text);
check it out,Row[i] has 2 cells??
Addy Tas 27-Nov-11 5:28am    
Is the exception thrown on the line:
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

If so the row does now have the checkbox control or the control does not have the name chkSelect.

The first thing to do is to find out which line is giving the error! Put a try...catch block around your method:
C#
protected void SAVE_bt_Click(object sender, EventArgs e)
     {
     try
         {
         if (Hidden_txt.Text == Convert.ToString(1))
             {
             ...
             }
         else
             {
             ...
             }
         }


     catch (Exception ex)
         {
         // Log or display ex.ToString() here
         }
     }
When you have that info, you can start to look at what might be the problem.
 
Share this answer
 
Your data source and grid should have same data type. The number of rows dosen't affect.
 
Share this answer
 
Add the following codes:

C#
if(!String.IsNullOrEmpty(Hidden_txt.Text))
{
   if (Hidden_txt.Text == Convert.ToString(1))
   {
      //your code here.
   }
}


Hope it helps!

Regards,
Eduard
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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