无法从会话中检索值

时间:2014-09-24 07:06:35

标签: c# sql asp.net session

我想将session Id变量传递给SQLDataSource SelectParameter:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
                SelectCommand="SELECT DISTINCT * FROM [employees] WHERE ([username] = @username)"
                UpdateCommand="UPDATE [employees] SET first_name = @first_name, middle_name = @middle_name, last_name = @last_name, father_name = @father_name, nic = @nic, age = @age, contact_no = @contact_no, gender = @gender, join_date = @join_date, qualification = @qualification, qualification_year = @qualification_year, univ_college = @univ_college, salary = @salary, experience = @experience, remarks = @remarks, status = @status, role = @role, bank_acct_no = @bank_acct_no, bank_name = @bank_name, blood_group = @blood_group WHERE username = @username">
                <SelectParameters>
                    <asp:QueryStringParameter DefaultValue="<%= Response.Write((string)Session['usernameId']) %>" Name="username" QueryStringField="username"
                        Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>

但是会收到错误,并且无法检索会话ID。如何评价:

DefaultValue="<%= Response.Write((string)Session['usernameId']) %>"

2 个答案:

答案 0 :(得分:3)

您应该使用SessionParameter代替QueryStringParameter

<asp:SessionParameter Name="username" SessionField="usernameId" DefaultValue="fixedValue" Type="String" />

此外,DefaultValue是固定值,默认值定义中的Response.Write是无意义的。没有什么可写入HTML本身,您可以设置服务器端控件的值。

答案 1 :(得分:0)

试试这个:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
            SelectCommand="SELECT DISTINCT * FROM [employees] WHERE ([username] = @username)"
            UpdateCommand="UPDATE [employees] SET first_name = @first_name, middle_name = @middle_name, last_name = @last_name, father_name = @father_name, nic = @nic, age = @age, contact_no = @contact_no, gender = @gender, join_date = @join_date, qualification = @qualification, qualification_year = @qualification_year, univ_college = @univ_college, salary = @salary, experience = @experience, remarks = @remarks, status = @status, role = @role, bank_acct_no = @bank_acct_no, bank_name = @bank_name, blood_group = @blood_group WHERE username = @username">
            <SelectParameters>
               <asp:SessionParameter Name="SessionVar" SessionField="usernameId" ConvertEmptyStringToNull="true" />
            </SelectParameters>
        </asp:SqlDataSource>

或者您可以参考此链接:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sessionparameter.aspx