Click here to Skip to main content
11,581,515 members (62,733 online)
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 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  
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  

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

  Print Answers RSS
0 OriginalGriff 340
1 Sergey Alexandrovich Kryukov 160
2 virusstorm 114
3 Frankie-C 105
4 RyanDev 84
0 OriginalGriff 1,273
1 Sergey Alexandrovich Kryukov 895
2 Abhinav S 597
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2015
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