SqlDataSource参数问题 - 使用loginName1作为sql参数

时间:2011-07-03 18:12:08

标签: asp.net sql-server parameters sqldatasource login-control

我正在尝试检索特定于当前登录用户的数据库信息。

以下3行让我知道var1是正确的(因为它在页面上显示)

Dim var1 As String<Br>
var1 = LoginName1.Page.User.Identity.Name<br>
Response.Write(var1)

但是,当我尝试使用var1作为参数时它不起作用....只是想知道我错过了什么。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT aspnet_Clubs.ClubName FROM aspnet_Clubs INNER JOIN aspnet_Users ON aspnet_Clubs.ClubID = aspnet_Users.ClubLinkID WHERE (aspnet_Users.UserName = @var1 )">
    </asp:SqlDataSource>
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
        CellPadding="2" DataSourceID="SqlDataSource1" ForeColor="Black" 
        GridLines="None" Height="50px" Width="274px">
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
        <EditRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <Fields>
            <asp:BoundField DataField="ClubName" HeaderText="ClubName" 
                SortExpression="ClubName" />
        </Fields>
        <FooterStyle BackColor="Tan" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
            HorizontalAlign="Center" />
    </asp:DetailsView>

2 个答案:

答案 0 :(得分:1)

您没有设置SqlDataSource的选择参数值。

Here's an overview包含使用参数的代码示例

答案 1 :(得分:0)

您需要为SqlDataSource定义参数,然后进行设置。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT aspnet_Clubs.ClubName FROM aspnet_Clubs INNER JOIN aspnet_Users ON aspnet_Clubs.ClubID = aspnet_Users.ClubLinkID WHERE (aspnet_Users.UserName = @var1 )">

<SelectParameters>
<asp:Parameter Name="var1" Type="String" />
</SelectParameters>
    </asp:SqlDataSource>

然后在Page_Load或类似的地方:

SqlDataSource1.SelectParameters("var1").DefaultValue = User.Identity.Name