Simple question on DataTable and DataGrid

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

Hi,

I'm trying to load data from SQL Server into a DataGrid. I planned on using
a DataTable to store the data, but I can't get the logic correct. Can
someone please tell me what I'm doing wrong? Thanks.

try

{

DatabaseConnection dc = new DatabaseConnection(); //this handles all the ADO
connections - this works fine used in other parts of the application

dc.Connect(); //opens the connection for reading

this.sqlSelectCommand1.CommandText = "SELECT RecipeIngredientName,
IngredientQuantity FROM RecipeIngredients WHERE (RecipeID = " + sRecipeID +
") order by RecipeIngredientName asc";


SqlCommand cmdSQL = null;


cmdSQL = new SqlCommand(this.sqlSelectCommand1.CommandText,dc.Connection);

SqlDataReader reader = cmdSQL.ExecuteReader();


while (reader.Read())

{

What do I do here? I need to load the data into a table so the grid can
display it

}



} //try



Thanks in advance,

Doug
 
Doug said:
SqlDataReader reader = cmdSQL.ExecuteReader();

Use a SqlDataAdapter instead. Once the SelectCommand property has been
set, you can just call the Fill method to populate your DataTable:

SqlConnection conn = new SqlConnection(<connectionstring>);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(<querystring>, conn);
adapter.Fill(datatable);

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Hi Doug,

Why don't you use SqlDataAdapter.
Just create it, set its SelectCommand to sqlSelectCommand1 and invoke

myDataAdapter.Fill(myDataTable);
 
Back
Top