Simple question on DataTable and DataGrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug

    #1

    Simple question on DataTable and DataGrid

    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

    {

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

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

    this.sqlSelectC ommand1.Command Text = "SELECT RecipeIngredien tName,
    IngredientQuant ity FROM RecipeIngredien ts WHERE (RecipeID = " + sRecipeID +
    ") order by RecipeIngredien tName asc";


    SqlCommand cmdSQL = null;


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

    SqlDataReader reader = cmdSQL.ExecuteR eader();


    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



  • Frank Oquendo

    #2
    Re: Simple question on DataTable and DataGrid

    Doug wrote:
    [color=blue]
    > SqlDataReader reader = cmdSQL.ExecuteR eader();[/color]

    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(< connectionstrin g>);
    SqlDataAdapter adapter = new SqlDataAdapter( );
    adapter.SelectC ommand = new SqlCommand(<que rystring>, conn);
    adapter.Fill(da tatable);

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


    (Pull the pin to reply)


    Comment

    • Miha Markic

      #3
      Re: Simple question on DataTable and DataGrid

      Hi Doug,

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

      myDataAdapter.F ill(myDataTable );

      --
      Miha Markic - RightHand .NET consulting & development
      miha at rthand com

      "Doug" <dkhandler@hotm ail.com> wrote in message
      news:O3r5xJtqDH A.2500@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi,
      >
      > I'm trying to load data from SQL Server into a DataGrid. I planned on[/color]
      using[color=blue]
      > 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
      >
      > {
      >
      > DatabaseConnect ion dc = new DatabaseConnect ion(); //this handles all the[/color]
      ADO[color=blue]
      > connections - this works fine used in other parts of the application
      >
      > dc.Connect(); //opens the connection for reading
      >
      > this.sqlSelectC ommand1.Command Text = "SELECT RecipeIngredien tName,
      > IngredientQuant ity FROM RecipeIngredien ts WHERE (RecipeID = " + sRecipeID[/color]
      +[color=blue]
      > ") order by RecipeIngredien tName asc";
      >
      >
      > SqlCommand cmdSQL = null;
      >
      >
      > cmdSQL = new SqlCommand(this .sqlSelectComma nd1.CommandText ,dc.Connection) ;
      >
      > SqlDataReader reader = cmdSQL.ExecuteR eader();
      >
      >
      > 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
      >
      >
      >[/color]


      Comment

      Working...