Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a grid view in c#:
XML
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                               AutoGenerateColumns="False" DataKeyNames="Id"
                                >
                               <Columns>
                                   <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
                                       ReadOnly="True" SortExpression="Id" />
                                   <asp:BoundField DataField="HeaderId" HeaderText="HeaderId"
                                       SortExpression="HeaderId" />
                                  
                                   <asp:BoundField DataField="Description" HeaderText="Description"
                                       SortExpression="Description" />
                                   <asp:BoundField DataField="DeviceId" HeaderText="DeviceId"
                                       SortExpression="DeviceId" />
                                   <asp:BoundField DataField="DateStamp" HeaderText="DateStamp"
                                       SortExpression="DateStamp" />
                                   
                               </Columns>
                           </asp:GridView>
                           <asp:SqlDataSource ID="SqlDataSource3" runat="server"
                               ConnectionString="<%$ ConnectionStrings:ConnectionStringline %>"
                               SelectCommand="SELECT * FROM [Tables]">
                               <SelectParameters>
                                   <asp:SessionParameter Name="HeaderId" SessionField="id" Type="Int32" />
                               </SelectParameters>
                           </asp:SqlDataSource>

and in the page apsx.cs
i have bind this gridview
public void bindgridview()
       {

           SqlCommand sqlcmd;
           SqlDataAdapter dap;
           DataTable data = new DataTable();
           SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringline"].ConnectionString);

           sqlcon.Open();

           sqlcmd = new SqlCommand("SELECT [Id], [HeaderId],[Description], [DeviceId], [DateStamp],  FROM [Tables] WHERE ([HeaderId] = '"+Session["id"].ToString()+"')", sqlcon);

           dap = new SqlDataAdapter(sqlcmd);

           dap.Fill(data);
           if (data.Rows.Count > 0)
           {
               GridView1.DataSource = data;

               GridView1.DataBind();
           }
       }


BUT WHEN I RUN GIVES ME THIS ERROR:

Object reference not set to an instance of an object.


WHAT TO FIX IN THIS CASE?
Posted
Comments
Lakshmimsridhar 7-Jan-13 9:29am    
Please check by putting break point session id is getting value. its not getting value thats why giving error.

If the exception is thrown at the line where Session["id"] is used, then probably it is null and trying to call .ToString() on the object throws the exception. I don't see any code that initializes Session["id"]. Check your code to see that it is initialized correctly before being used.
 
Share this answer
 
Where you are setting property of session["id"]? Its not getting the proper value for session["id"].
 
Share this answer
 
Please check by putting break point session id is getting value. its not getting value thats why giving 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