1

I am getting "Object reference not set to an instance of an object" error while inserting/updating data into a sql server database from gridview . Anybody please help .

protected void GridAllStore_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        storelocatorDataSetTableAdapters.storedbTableAdapter tastoreInsert = new storelocatorDataSetTableAdapters.storedbTableAdapter();
        if (e.CommandName.Equals("Insert"))
        {
            TextBox txtNewName = new TextBox();
            TextBox txtNewContact = new TextBox();
            TextBox txtNewAddress = new TextBox();
            txtNewName = (TextBox)GridAllStore.FooterRow.FindControl("txtNewName");
            txtNewContact = (TextBox)GridAllStore.FooterRow.FindControl("txtNewContact");
            txtNewAddress = (TextBox)GridAllStore.FooterRow.FindControl("txtNewAddress");
            tastore.Insert(txtNewName.Text, txtNewContact.Text, txtNewAddress.Text);    
            FillGrid();          
        }
    }

Here is the error message:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 107: txtNewContact = (TextBox)GridAllStore.FooterRow.FindControl("txtNewContact");
Line 108: txtNewAddress = (TextBox)GridAllStore.FooterRow.FindControl("txtNewAddress");
Line 109: tastore.Insert(txtNewName.Text, txtNewContact.Text, txtNewAddress.Text);
Line 110: FillGrid();
Line 111: }

Source File: C:\Users\DELL\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\AdminPanel.aspx.cs Line: 109

4
  • 2
    Breakpoint the method and step through reviewing instances and their properties for nulls. Start on GridAllStore then FooterRow as the most likely. Commented Apr 24, 2012 at 7:09
  • You are using FillGrid on what? Commented Apr 24, 2012 at 7:11
  • Using FillGrid to load the data into the datagrid . Commented Apr 24, 2012 at 7:19
  • Tastore could also be null and you seem to declare tastoreInsert and never use it. Commented Apr 24, 2012 at 7:26

3 Answers 3

1

FindControl can return null, when you access Text property of null object exception will throw. you can check null before access Text property.

        var txtNewNameTb = (TextBox)GridAllStore.FooterRow.FindControl("txtNewName");
        var txtNewContactTb = (TextBox)GridAllStore.FooterRow.FindControl("txtNewContact");
        var txtNewAddressTb = (TextBox)GridAllStore.FooterRow.FindControl("txtNewAddress");

        if (txtNewNameTb == null || txtNewContactTb == null || txtNewAddressTb  == null) { return; }

        if(tastore == null)  { return; }

        tastore.Insert(txtNewNameTb.Text, txtNewContactTb.Text, txtNewAddressTb.Text);    

        FillGrid();          

In your ASPX FooterTemplate there should be a 3 Text Boxes like below.

<FooterTemplate> 
<asp:TextBox ID="txtNewName" runat="server" >
</asp:TextBox> 
<asp:TextBox ID="txtNewContact" runat="server" >
</asp:TextBox> 
<asp:TextBox ID="txtNewAddress" runat="server" >
</asp:TextBox> 
</FooterTemplate> 
Sign up to request clarification or add additional context in comments.

1 Comment

It's giving me error in this line : var txtNewName = txtNewNameTb ? txtNewNameTb.Text : string.Empty; It says : Error 2 Cannot implicitly convert type 'System.Web.UI.WebControls.TextBox' to 'bool'
1

The error message means tastore is null in line 109, so tastore needs to be initialised. You have confused yourself by writing tastoreInsert in the function header and tastore.Insert in the body.

Edit: Sorry, it could also mean that any of the three textboxes does not exist. FindControl returns null if the control is not found, so you'd need to look into those too. Debug!

12 Comments

Tastore won't even exist so I'd guess that wouldn't compile.
@AdamHouldsworth Yes it does! The error is a run-time error, so it must have compiled without error. That's why I said the OP is confusing himself with names like tastore and tastoreInsert.
@FahimAhmed Where do you initialise tastore then? That part of the program isn't shown, and the error message means tastore is null.
I would first assume that it is a typo in the question. I know that the exception is a runtime one. We can only go off the code and stacktrace provided in which case my point stands. The code doesn't show tastore being declared.
@fahimahmed all I can advise is that you try what I suggested in the comments to your question. Something is null and your code is trying to use it, simply.
|
0

use Convert.ToString(txtNewNameTb.Text)
it automatically converts NULL to empty string.

try like :-

tastore.Insert(Convert.ToString(txtNewName.Text),Convert.ToString( txtNewContact.Text), Convert.ToString(txtNewAddress.Text));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.