使用where子句中的session绑定网格视图 [英] bind a grid view using session in the where clause

查看:108
本文介绍了使用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绑定此gridview


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?

推荐答案

ConnectionStrings:ConnectionStringline %>

< span class =code-attribute> SelectCommand = SELECT * FROM [Tables] >
< SelectParameters >
< asp:SessionParameter 名称 = HeaderId SessionField = id 类型 = Int32 / >
< / SelectParameters >
< / asp:SqlDataSource >
ConnectionStrings:ConnectionStringline %>" SelectCommand="SELECT * FROM [Tables]"> <SelectParameters> <asp:SessionParameter Name="HeaderId" SessionField="id" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>



和页面apsx.cs

i绑定此gridview


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?


如果在使用Session [id]的行中抛出异常,则可能它为null并尝试在对象上调用.ToString()抛出例外。我没有看到任何初始化Session [id]的代码。检查你的代码,看它在使用之前是否已正确初始化。
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.


你在哪里设置session [id]的属性?它没有得到会话[id]的正确值。
Where you are setting property of session["id"]? Its not getting the proper value for session["id"].


这篇关于使用where子句中的session绑定网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆