0

I've been through oh so many tutorials and I just can't get this right. I'm trying to populate a gridview based on the results of a stored procedure. I'm probably code blind by now but I really tried to make sure I didn't mess up on something small.

This is the stored procedure:

ALTER PROCEDURE dbo.GetAllPlayersFromGame

(
@gameID int
)AS
/* SELECT all the players within a game */

SELECT playerName FROM Player WHERE gameID = @gameID

This is the code I use to create a datatable which I then use as the datasource for the gridview:

        public static DataTable PopulateGridView(string resultSetQuery)
    {
        //Populate gridview
        OpenConnection();
        SqlCommand sqlCommand = new SqlCommand(resultSetQuery, _sqlConnection);

        SqlDataReader reader = sqlCommand.ExecuteReader();

        DataTable dataTable = new DataTable();
        dataTable.Load(reader);
        return dataTable;
    }

And this is the method that calls everything and is supposed to populate the gridview:

protected void ShowPlayersInGame()
{

    GridViewShowPlayersInGame.DataSource = CreateDatabaseConnection.PopulateGridView("EXEC GetAllPlayersFromGame " + _gameId);
    GridViewShowPlayersInGame.DataBind();
}

When I debug I see all the data in the reader object so the stored procedure works. But something happens when it gets to ShowPlayersInGame because the gridview won't take the data. I've probably missed something rudimentary since I haven't used gridviews a lot before.

Any ideas?

3
  • Is the GridView in a custom control with your own CreateChildControls method? Commented Feb 25, 2010 at 10:36
  • Ehm.. if that's something that can happen when I generate a page from a master page then maybe. I don't know what that really means so I'm not sure. How can I clarify this? Commented Feb 25, 2010 at 13:35
  • The answer to that question is a no. The grid view is in a contentplaceholder in a contentpage derived from a master page. It is also in an updatepanel (Ajax). No custom control. Commented Feb 25, 2010 at 14:20

1 Answer 1

1

So.. STUPID problem it was.

  <asp:GridView ID="GridViewShowPlayersInGame" runat="server" 
    AutoGenerateColumns="True" >
</asp:GridView>

I had AutoGenerateColumns="False"

Now it works as intended. Thanks for reading this though!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.