Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
i have created a grid view in c#:
<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 7-Jan-13 3:24am
Comments
Lakshmimsridhar at 7-Jan-13 9:29am
   
Please check by putting break point session id is getting value. its not getting value thats why giving error.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please check by putting break point session id is getting value. its not getting value thats why giving error.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Where you are setting property of session["id"]? Its not getting the proper value for session["id"].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 359
1 Sergey Alexandrovich Kryukov 355
2 CPallini 335
3 BillWoodruff 324
4 OriginalGriff 237
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100