Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. load dataGrid C#

load dataGrid C#

Scheduled Pinned Locked Moved C#
csharpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toto_2010
    wrote on last edited by
    #1

    I want to load in application dataGrid 3TIER with SqlDataReader and code of C#? thanks

    M 1 Reply Last reply
    0
    • T toto_2010

      I want to load in application dataGrid 3TIER with SqlDataReader and code of C#? thanks

      M Offline
      M Offline
      mrkeivan
      wrote on last edited by
      #2

      Hey, could you be more specific about what you are planning to do ?

      T 1 Reply Last reply
      0
      • M mrkeivan

        Hey, could you be more specific about what you are planning to do ?

        T Offline
        T Offline
        toto_2010
        wrote on last edited by
        #3

        I would like to fill my datagrid from sql database in accordance with the principles of the n-tier architecture (DAL,BLL,UI), but I do not know how to do it in connected mode

        M 1 Reply Last reply
        0
        • T toto_2010

          I would like to fill my datagrid from sql database in accordance with the principles of the n-tier architecture (DAL,BLL,UI), but I do not know how to do it in connected mode

          M Offline
          M Offline
          Morgs Morgan
          wrote on last edited by
          #4

          Not quiet certain on the above 'principles' but try this:

                 try
                 {
                      string query = @"SELECT \* FROM \[contacts\]";
                      conn.Close();//you must have created this connection string already
                      SqlCommand cmd = conn.CreateCommand();//sql command
                      cmd.CommandText = query;//declared above
                      conn.Open();//open your connection to the database
                      SqlDataReader reader = cmd.ExecuteReader();//execute reader to read from database
                      DataSet dataset = new DataSet();//you need this
                      DataTable table = dataset.Tables.Add();
                      table.Columns.Add("contactid", typeof(string));//these
                      table.Columns.Add("firstname", typeof(string));//must be column
                      table.Columns.Add("lastname", typeof(string));//names in
                      table.Columns.Add("email", typeof(string));//your datagrid
                      while (reader.Read())
                      {
                          //add rows to your table which already has column names/heards...see above
                          table.Rows.Add(reader\["contact\_id"\].ToString(), reader\["name"\].ToString(), reader\["surname"\].ToString(), reader\["email"\].ToString());
                      }
                      //check if any data was added in a while(reader.Read())
                      if (dataset.Tables\[0\].Rows.Count > 0 || dataset.Tables.Count > -1)
                      {
                          contacts.DataSource = table;//just like it says
                          contacts.DataBind();
                          contacts.Visible = true;
                      }
                  }
                  catch (SqlException fc)
                  {
                      error.Text = "Error occured :" + fc.Message;//catch that error
                  }
                  finally
                  {
                      conn.Close();//please close connection to your database when done
                  }
                  //hope this helps!!!
          

          Later

          T 1 Reply Last reply
          0
          • M Morgs Morgan

            Not quiet certain on the above 'principles' but try this:

                   try
                   {
                        string query = @"SELECT \* FROM \[contacts\]";
                        conn.Close();//you must have created this connection string already
                        SqlCommand cmd = conn.CreateCommand();//sql command
                        cmd.CommandText = query;//declared above
                        conn.Open();//open your connection to the database
                        SqlDataReader reader = cmd.ExecuteReader();//execute reader to read from database
                        DataSet dataset = new DataSet();//you need this
                        DataTable table = dataset.Tables.Add();
                        table.Columns.Add("contactid", typeof(string));//these
                        table.Columns.Add("firstname", typeof(string));//must be column
                        table.Columns.Add("lastname", typeof(string));//names in
                        table.Columns.Add("email", typeof(string));//your datagrid
                        while (reader.Read())
                        {
                            //add rows to your table which already has column names/heards...see above
                            table.Rows.Add(reader\["contact\_id"\].ToString(), reader\["name"\].ToString(), reader\["surname"\].ToString(), reader\["email"\].ToString());
                        }
                        //check if any data was added in a while(reader.Read())
                        if (dataset.Tables\[0\].Rows.Count > 0 || dataset.Tables.Count > -1)
                        {
                            contacts.DataSource = table;//just like it says
                            contacts.DataBind();
                            contacts.Visible = true;
                        }
                    }
                    catch (SqlException fc)
                    {
                        error.Text = "Error occured :" + fc.Message;//catch that error
                    }
                    finally
                    {
                        conn.Close();//please close connection to your database when done
                    }
                    //hope this helps!!!
            

            Later

            T Offline
            T Offline
            toto_2010
            wrote on last edited by
            #5

            thanks

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups