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?