Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Tell me any body how we display database table one column values in comboBox in c# window application.we used this code but its not work corret..plz help
C#
private void bindCombo()
         {
             string q = "select policestation from addpolicestation";
             SqlDataAdapter da = new SqlDataAdapter(q, "da.crim()");
             DataTable dt = new DataTable();

             da.Fill(dt);

             comboB1.Items.Add("--Select--");
             foreach (DataRow row in dt.Rows)
             {
                 comboB1.Items.Add(row["policestation"]);

             }
             comboB1.SelectedIndex = 0;
         }
Posted
Comments
Manoj Kumar Choubey 9-Aug-12 4:58am    
Are you getting error message ?
Manoj Kumar Choubey 9-Aug-12 5:00am    
please debug and check what happening during execution record fetching properly or not , is datatable empty etc.

i have find this solution...
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication44
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlDataAdapter da;
        DataSet ds = new DataSet();

        private void Form1_Load(object sender, EventArgs e)
        {
            // Build the Connection String and create a SQL Connection object
            String cnnStr = "Data Source = localhost; Initial Catalog = Northwind; Integrated Security = SSPI";
            SqlConnection cnn = new SqlConnection(cnnStr);
            // Create a SQL Command object from the connection object and setup the SELECT command
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM Customers";
            // Create the data adapter to use to get the data and handle connecting to the DB
            da = new SqlDataAdapter(cmd);
            // Get the data from the database. I am using the Northwind database the customers table
            da.Fill(ds, "Customers");
            // Disconnect the event handler to handle the comboBox1_SelectedIndexChanged event
            // while the combo box is being populated, otherwise it will fire a couple of times
            // before you are ready to handle them.
            this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // Connect the cobo box to the data source from where the data is comming from
            // In this case the Customers data table in the dataset.
            comboBox1.DataSource = ds.Tables["Customers"];
            // Tell the combo box what collumn to display to the user
            comboBox1.DisplayMember = "CompanyName";
            // Tell the combo box what collumn to use with the displayed value, value is not displayed
            comboBox1.ValueMember = "CustomerID";
            // Restored the event handler
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // When the user clicks on a item in the combo box ist primary key will be displayed.
            MessageBox.Show("Customer Primary Key is \"" + comboBox1.SelectedValue + "\"");
        }
    }
}


enjoy...
 
Share this answer
 
Hi please google first for any of your queries before posting

Check this,

Beginners guide to accessing SQL Server through C#[^]
 
Share this answer
 
With the combination of LInQ and ComboBox ItemSource property, we can easily achieve the functionality as:

C#
public List<string> GetPoliceStation()
        {
            List<string> retStation = new List<string>();
            using (DBEntities dbContext = new DBEntities(connectionString))
            {
                return DBEntities.AddPoliceStation.ToList();
            }
        }


comboB1.ItemsSource = GetPoliceStation();
comboB1.DisplayMemberPath = "PoliceStation";
comboB1.SelectedValuePath = "PoliceStation";
comboB1.SelectedIndex = 0;</string></string></string>
 
Share this answer
 
Get the data from databse and store it in datatable.Then follow my steps after datatable fill.
C#
string q = "select policestation from addpolicestation";
SqlDataAdapter da = new SqlDataAdapter(q, "da.crim()");
DataTable dt = new DataTable();
 
da.Fill(dt);
 
comboB1.DataSource=dt;
comboB1.DisplayMemberPath = "PoliceStation";
comboB1.SelectedValuePath = "PoliceStation";
comboB1.Items.Insert(0, new ListItem(String.Empty, String.Empty));
 
Share this answer
 
v2
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Hoi_Ton.Thu_vien_lop;
using Hoi_Ton.Du_lieu;
using System.Collections;


C#
namespace Hoi_Ton.Man_hinh
{
    public partial class MH_Dang_ky : Form
    {
        
        public MH_Dang_ky()
        {
            InitializeComponent();
        }


string chuoiketnoi = "server=(local);database=hoiton;integrated security=sspi";
SqlConnection ketnoi;
SqlDataAdapter daDaotrang;
SqlCommand bolenh;
DataSet dtDaotrang;
SqlDataReader drDaotrang;
DataTable dtbl;
C#
private void MH_Dang_ky_Load(object sender, EventArgs e)
        {

C#
ketnoi = new SqlConnection(chuoiketnoi);
            //
            string strCmd = "select madt,chitiet from t_ttindaotrang; select * from t_thongtin";

            daDaotrang = new SqlDataAdapter(strCmd, ketnoi);
            dtDaotrang = new DataSet();
            daDaotrang.Fill(dtDaotrang);
            dataGridViewDS.DataSource = dtDaotrang.Tables[1];
            cbBoxDao_trang.DataSource = dtDaotrang.Tables[0];
            cbBoxDao_trang.DisplayMember = "chitiet";
            cbBoxDao_trang.ValueMember = "madt";
            cbBoxDao_trang.Enabled = true;
            this.cbBoxDao_trang.SelectedIndex = -1;


        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900