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. How to retrieve data from database

How to retrieve data from database

Scheduled Pinned Locked Moved C#
databasequestioncsharphelptutorial
6 Posts 4 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.
  • L Offline
    L Offline
    lalit14
    wrote on last edited by
    #1

    I want to retrive data from SQL database using C#. and i want to put that data in DropDownList. How can i do this????? Please Help me. Thanks In advance.......!!

    L C B 3 Replies Last reply
    0
    • L lalit14

      I want to retrive data from SQL database using C#. and i want to put that data in DropDownList. How can i do this????? Please Help me. Thanks In advance.......!!

      L Offline
      L Offline
      lalit14
      wrote on last edited by
      #2

      and I want to put that data in DropDownlist without using data source

      L 1 Reply Last reply
      0
      • L lalit14

        and I want to put that data in DropDownlist without using data source

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        How about searching it in google ... This is for example : http://www.dotnetspider.com/resources/7644-Bind-DropDownList-from-DataBase-Using-c.aspx[^] If you don't want to use the datasource, then take the DataTable from dataSet.Tables[0] and read the data.

        L 1 Reply Last reply
        0
        • L lalit14

          I want to retrive data from SQL database using C#. and i want to put that data in DropDownList. How can i do this????? Please Help me. Thanks In advance.......!!

          C Offline
          C Offline
          CoderForEver
          wrote on last edited by
          #4

          I think this can help u u can use SqlDataAdapter and DataSet classes

                  SqlDataAdapter dd = new SqlDataAdapter();
                  DataSet de = new DataSet();
                 
                  // For the Employees
                  dd = new SqlDataAdapter("SELECT EmpName,isWaiter,isCashier FROM Employees", con);
          

          //con is ur connection string to ur database
          de.Clear();
          try
          {
          con.Open();

                      dd.Fill(de);
                      int k;
                
                      for (k = 0; k < de.Tables\[0\].Rows.Count; k++)
                      {
                          
                              comboBox2.Items.Add(de.Tables\[0\].Rows\[k\].ItemArray\[0\].ToString());
                      }
                  }
                 catch(Exception ex)
                 {
                  MessageBox.Show(ex.Message.ToString());
                 }
          
          1 Reply Last reply
          0
          • L Lost User

            How about searching it in google ... This is for example : http://www.dotnetspider.com/resources/7644-Bind-DropDownList-from-DataBase-Using-c.aspx[^] If you don't want to use the datasource, then take the DataTable from dataSet.Tables[0] and read the data.

            L Offline
            L Offline
            lalit14
            wrote on last edited by
            #5

            Thanks...............!!

            1 Reply Last reply
            0
            • L lalit14

              I want to retrive data from SQL database using C#. and i want to put that data in DropDownList. How can i do this????? Please Help me. Thanks In advance.......!!

              B Offline
              B Offline
              Blikkies
              wrote on last edited by
              #6

              using System.Data;
              using System.Data.SqlClient;

              Class test
              {
              public void pageload()
              {

                SqlConnection conn = new SqlConnection(@"User id:sa; Password:password; Initial catalog: databaseName; data source:localhost");
              		conn.Open();
              		SqlCommand cmd = conn.CreateCommand();
              		cmd.CommandText = "SELECT bob FROM table";
              		SqlDataReader reader = cmd.ExecuteReader();
              		
              		while (reader.Read())
              		{
              			
              			dropdownlist.Items.Add(reader\["bob"\].ToString());
              		}
              		reader.Close();
              		conn.Close();
                     }
              

              }

              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