Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a 7 textbox in my grid view but when i enter value in textbox and submit then it shows an error object reference not set to an instance of an object..
here is my code
C#
for(int count = 0; count < GridView1.Rows.Count; count++)
    {
        TextBox txt1 = (TextBox)GridView1.Rows[count].FindControl("txt1");
        TextBox txt2 = (TextBox)GridView1.Rows[count].FindControl("txt2");
        TextBox txt3 = (TextBox)GridView1.Rows[count].FindControl("txt3");
        TextBox txt4 = (TextBox)GridView1.Rows[count].FindControl("txt4");

        TextBox txt5 = (TextBox)GridView1.Rows[count].FindControl("txt5");
        TextBox txt6 = (TextBox)GridView1.Rows[count].FindControl("txt6");
        TextBox txt7 = (TextBox)GridView1.Rows[count].FindControl("txt7");
       then i write connection string
           string str;
        str = "insert into leave1(pl,sl,cl,lwp,ot,night,sunday)values('" + txt1.Text + "','" + txt2.Text + "','" + txt3.Text + "','" + txt4.Text + "','" + txt5.Text + "','" + txt6.Text + "','" + txt7.Text + "')";
        SqlCommand cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.DataBind();
}

plz help me....where m wrong
Posted
Updated 7-Jun-12 22:03pm
v2

1 solution

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Out here, the way you have coded, it should have atleast 7 rows of data and they should have textbox controls to be found. You sure it's 7 rows? Or you have 7 columns and less number of rows? Adjust and check.
 
Share this answer
 
Comments
Rini Agrawal 8-Jun-12 4:37am    
nw no error is coming but in database for every textbox 0 is inserting not value
Sandeep Mewara 8-Jun-12 4:58am    
Use Visual Studio DEBUGGER. Press F11 and see step by step on how execution is happening and what values are being evaluated for every line and sent to DB.
Maroti Wankhede 19-Jan-13 8:05am    
Please give the suggestion on purchase inquiry First table indentmaster and select indent id fill all data in grid view then i insert the same grid view niw quantity this grid not update first data.

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