Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Error Message

Object reference not set to an instance of an object.


around
string intno4, intno5;



How do I resolve this ?

Thanks




protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
     {
         //GRIDVIEW BEGIN

         //txtSave.Enabled = false;
         try
         {
             //connection to the database
             //
             //
             string str;
             str = ConfigurationManager.ConnectionStrings["WEBHR"].ConnectionString;
             SqlConnection sqlcon = new SqlConnection(str);

             string intno,intno2;
             string intno4, intno5;

             //Get the intno
             intno  = GridView1.SelectedRow.Cells[1].Text;
             intno2 = GridView1.SelectedRow.Cells[4].Text;

             //call the stored procedure
             SqlCommand SqlCmd = new SqlCommand("sp_Getleavactid", sqlcon);
             SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;


             //Supply the User_id parameter
             SqlCmd.Parameters.AddWithValue("@BRNNO", intno);
             SqlCmd.Parameters.AddWithValue("@BYEAR", intno2);

             //Create and supply the output parameters


             SqlCmd.Parameters.Add("@BEMPNO", System.Data.SqlDbType.VarChar, 50);
             SqlCmd.Parameters["@BEMPNO"].Direction = System.Data.ParameterDirection.Output;

       //Open the sql data connection
             sqlcon.Open();

             //Execute the stored procedures
             SqlCmd.ExecuteNonQuery();

             //Assign the results to the controls


             txt_Code.Text       = SqlCmd.Parameters["@BRNNO"].Value.ToString();
              txt_Year.Text       = SqlCmd.Parameters["@BYEAR"].Value.ToString();


             lblstatus.Text = "Grid record successful";

             //
             //
             sqlcon.Close();

             ///
             ///
             ///GRIDVIEW2
             ///
             ///

             string str1;
             str1 = ConfigurationManager.ConnectionStrings["WEBHR"].ConnectionString;
             SqlConnection sqlcon1 = new SqlConnection(str1);

             string intno4, intno5;

             //Get the intno
             intno4 = GridView2.SelectedRow.Cells[1].Text;
             intno5 = GridView2.SelectedRow.Cells[4].Text;

             //call the stored procedure
             SqlCommand SqlCmd1  = new SqlCommand("sp_GetLeavGrid2id", sqlcon1);
             SqlCmd1.CommandType = System.Data.CommandType.StoredProcedure;


             //Supply the User_id parameter
             SqlCmd1.Parameters.AddWithValue("@BRNNO", intno4);
             SqlCmd1.Parameters.AddWithValue("@BYEAR", intno5);

             //Create and supply the output parameters

             //Create and supply the output parameters
             SqlCmd1.Parameters.Add("@BEMPNO", System.Data.SqlDbType.VarChar, 50);
             SqlCmd1.Parameters["@BEMPNO"].Direction = System.Data.ParameterDirection.Output;


             //Open the sql data connection
             sqlcon1.Open();

             //Execute the stored procedures
             SqlCmd1.ExecuteNonQuery();

             //Assign the results to the controls

             txt_Code.Text = SqlCmd1.Parameters["@BRNNO"].Value.ToString();
             txt_Year.Text = SqlCmd1.Parameters["@BYEAR"].Value.ToString();



             //  txt_Ed_Date.Text = SqlCmd.Parameters["@BEDDATE"].Value.ToString();

             lblstatus.Text = "Grid2View record successful";

             //
             //
             sqlcon1.Close();




             ///
             ///
             ///GRIDVIEW2
             ///
             ///



         }
         catch (Exception ex)
         {
             lblstatus.Text = ex.Message;

         }

         //GRIDVIEW END
     }


What I have tried:

Tried to debug to no avail. This is a specific example
Posted
Updated 13-Mar-17 3:04am
v2
Comments
Karthik_Mahalingam 11-Mar-17 6:27am    
How will this line causes object reference error
 string intno4, intno5;
Member 12770648 12-Mar-17 16:52pm    
When I debugged

1 solution

It cannot be the line you are indicating - as Karthik Bangalore has pointed out.

It is most likely to be one of the lines similar to
C#
intno4 = GridView2.SelectedRow.Cells[1].Text;

If Cells[1] is empty then the .Text call will generate the error you are experiencing. Similarly if there is no row selected on the GridView you will get the same error
 
Share this answer
 

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