1

我正在尝试从创建的表中填充数据,该表中aspnetdb.mdf存储有游戏数据,该表具有uniqueidentifier用户UserIdAsp Membership UserId。我想使用 a 从表中选择用户数据SqlDataSource并将其填充到DataGridView.

我已经阅读了一个又一个线程和 MSDN 文档,而对于我的生活,我无法让这个小丑工作。DataGridView甚至没有显示,只是一个空白屏幕。在设计器中,我可以看到我不想选择的列。这是代码。如果有人有想法、答案或任何东西,我将不胜感激。

我尝试过切换参数数据类型以及我能想到的任何东西,但这不起作用

在后面的代码中:

protected void userGameData_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    MembershipUser User = Membership.GetUser();
    object UserGUID = User.ProviderUserKey;

    userGameData.SelectParameters["UserId"].DefaultValue = UserGUID.ToString();

}

在 aspx 中

<form id="frm" runat="server">
    <asp:SqlDataSource ID="userGameData" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [cycleday], [playerwins], [housewins], [gametype], [gamedate], [phase] FROM [GameData] WHERE ([UserId] = @UserId)" OnSelecting="userGameData_Selecting">
        <SelectParameters>
            <asp:Parameter Name="UserId" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="rawGameResults" runat="server" AutoGenerateColumns="False" DataSourceID="userGameData">
        <Columns>
            <asp:BoundField DataField="cycleday" HeaderText="cycleday" SortExpression="cycleday" />
            <asp:BoundField DataField="playerwins" HeaderText="playerwins" SortExpression="playerwins" />
            <asp:BoundField DataField="housewins" HeaderText="housewins" SortExpression="housewins" />
            <asp:BoundField DataField="gametype" HeaderText="gametype" SortExpression="gametype" />
            <asp:BoundField DataField="gamedate" HeaderText="gamedate" SortExpression="gamedate" />
            <asp:BoundField DataField="phase" HeaderText="phase" SortExpression="phase" />
        </Columns>
    </asp:GridView>
</form>
4

1 回答 1

0

也许绑定在标记中不起作用,我会从代码隐藏中尝试

   string sql = "SELECT [cycleday], [playerwins], [housewins], [gametype], [gamedate], [phase] FROM [GameData] WHERE UserId = (SELECT [UserId] FROM aspnet_users WHERE UserName = '" & User.Identity.Name & "')";
   using (SqlConnection myConnection = new SqlConnection(connectionString))
   {
     using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
     {
       myConnection.Open();
       using (SqlDataReader myReader = myCommand.ExecuteReader())
       {
         DataTable myTable = new DataTable();
         myTable.Load(myReader);
         myConnection.Close();

         YourDataGridView.DataSource = myTable;
         YourDataGridView.DataBind();
       }
     }
   }

设置connectionString为您在项目中使用的 CS :)

于 2013-05-28T01:08:03.567 回答