Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi..
I have inserted update panel in my gridview but when i execute my program then this error
C#
Object reference not set to an instance of an object.

Come.When i put form tag before gridview then it working well but page reload but i want page not reload when i search database in gridview and press search button.When i remove form tag then error come.How can i resolve this issue and i also check userid and seesion user name vy using breakpoints ,userid and session etc working fine ,values coming from userid and username,commnd etc,

What I have tried:

Here is my aspx code :
ASP.NET
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    </asp:ScriptManager>
  
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate>
Search Database    
     <asp:TextBox runat="server" ID="txtSrch" CssClass="txtSrch" Font-Size="Medium"  Width="200px" Height="35px" placeholder="Database Name"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" CssClass="srchreq" ControlToValidate="txtSrch" runat="server" ForeColor="Red" ErrorMessage="Name is required"></asp:RequiredFieldValidator>
     <br />
     <br />
      <asp:Button CssClass="searchDb" ID="btn" runat="server" Text="Search"  OnClick="Search"/>

<asp:GridView ID="GridView1" runat="server" Height="63px" 
        style="margin-left: 270px; margin-top: 6px" Width="434px" CellPadding="3" 
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
        BorderWidth="1px">
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>
    
    <br />
     
     
   </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

and here is aspx.cs code :
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string user = Session["name"].ToString();
            SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
            SqlCommand cmd2 = new SqlCommand("SELECT User_ID from tbl_user WHERE User_Name='" + user + "'", cnn);
            cnn.Open();
            string id = cmd2.ExecuteScalar().ToString();
            int ID = Int32.Parse(id);
            SqlCommand cmd = new SqlCommand("SELECT Database_Name,Description,Date FROM  Create_db WHERE User_ID='" + ID + "'", cnn);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            cmd.ExecuteNonQuery();
        cnn.Close();
      }
     }
//here is my search method 
 public override void VerifyRenderingInServerForm(Control control)
    {
        
    }
Posted
Updated 18-Jul-23 6:09am

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your full code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 12369816 9-May-16 5:43am    
Well thanks for explanation but i don;t need explanation of error i need "SOLUTION" of error so that's why i post question here that how can i resolve this error and i know well that this error come due to null variable.I think object is not null it gives me values i also check by using break points that it give me values and i also explain above that when i include <form runat="server" > tag before gridview and end form ag after gridview </form> then it working well ,it show output in my gridview but when i remove <form> tag then its gives me error object refrence not set an instace object, and i think if it gives values null then output never come,always its shows me error.
OriginalGriff 9-May-16 5:55am    
We can't solve it for you: that's what I said!
We can't run your code under exactly the same circumstances you do, with the same data you use. It's up to you to use the debugger to find out what is null, and then work out how it got to be null - I can't do that for you!
So give it a try, and see what information you can find out.
Member 12369816 9-May-16 6:11am    
values not null,if values null then it not work with <form tag
OriginalGriff 9-May-16 6:33am    
Something *is* null - that is what the exception message is telling you.
And only looking closely at what it happening *while the code is running* will tell you what.
The debugger is the easiest tool to use to find out what is null - then you can move on to the next stage which is "why is it null?"
Member 12369816 9-May-16 6:46am    
I'm really confused when i use <form runat="server"></form> this line before starting gridview then output working well,gridview show data ,but when i remove this line then error come ' 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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.'
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferences by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 
Object reference not set to an instance of an object. is thrown by .net when the object is null and you are trying to access it.
which code line gives you error ? Please elaborate.
see if the object in code line is NULL.
 
Share this answer
 
Comments
Member 12369816 9-May-16 5:41am    
I think object is not null it gives me values i also check by using break points that it give me values and i also explain above that when i include <form runat="server" > tag before gridview and end form ag after gridview </form> then it working well ,it show output in my gridview but when i remove <form> tag then its gives me error object refrence not set an instace object, and i think if it gives values null then output never come,always its shows me error.

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