本文介绍了使用where子句中的session绑定网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在c#中创建了一个网格视图: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> 和页面apsx.cs i绑定此gridviewand in the page apsx.csi have bind this gridviewpublic 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?推荐答案 和页面apsx.cs i绑定此gridviewand in the page apsx.csi have bind this gridviewpublic 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? 这篇关于使用where子句中的session绑定网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 12:42