Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir,
Am get stuck in accessing into combobox.How to get single column value into the combobox from MSSQL database after debugging.

Here i have used this code in C#:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br mode="hold" />
C#
SqlConnection con;
        SqlCommand com;
        SqlDataReader dr;
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            con = new SqlConnection("Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True");
            com = new SqlCommand("Select Device_name from Monitor_devices", con);
            //cmd.CommandType = CommandTypeText;
            dr=com.ExecuteReader();
            //int j = 0;
            //if (dr.HasRows)
            //{
                while (dr.Read())
                {
                    comboBox2.Items.Add(dr[0].ToString());
                    //j++;
                }
                dr.Close();
                con.Close(); 
               

        }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


Any one know about this ,plz do help..

Thanks,
Pradeep CBZ

Edit: Tags added
Posted
Updated 2-Oct-16 20:21pm
v2
Comments
OriginalGriff 29-Mar-12 4:13am    
What is the problem?
What is it doing that is shouldn't, or not doing that it should?
Pradeep CBZ 29-Mar-12 4:23am    
hello sir,
Nothing happening,over here.After executing or debugging the code,then if i click on the combobox then it is not retriving into combobox from SQL database column.How to achieve it.How to make it visible the dropdownlist or how to retrive.

Thanks
Pradeep

try

C#
comboBox2.Items.Add(dr[0].ToString());

to
C#
comboBox2.Items.Add(dr["Device_name"].ToString());



Also you do not need to have the code in a "comboBox2_SelectedIndexChanged" event, you an remove that event... see below

C#
SqlConnection con;
        SqlCommand com;
        SqlDataReader dr;

                        con = new SqlConnection("Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True");
            com = new SqlCommand("Select Device_name from Monitor_devices", con);
            //cmd.CommandType = CommandTypeText;
                con.Open();

            dr=com.ExecuteReader();
            //int j = 0;
            //if (dr.HasRows)
            //{
                while (dr.Read())
                {
                    comboBox2.Items.Add(dr["Device_name"].ToString());
                    //j++;
                }
                dr.Close();
                con.Close(); 
 
Share this answer
 
v2
Comments
nikki88 29-Mar-12 4:46am    
please note, i change the position of your con.Open(); statement.
Pradeep CBZ 29-Mar-12 4:48am    
Hello sir,
even i changed my code into
<<<<<<
comboBox2.Items.Add(dr["Device_name"].ToString());
>>>>>>


But even though it is not working.I mean it is not giving error ,how to resolve it sir.

Thanks,
Pradeep.
Agrahari111 18-Sep-12 2:13am    
tihis is show only first row of the data base,how to show all values of one column
nikki88 18-Sep-12 4:21am    
This will add everything in the column, it adds each on as it moves through the while loop.
Hello
I have used this code using Entity Framework :

C#
Public Class BankAccount
{
   Public List<ema.shopping.model.bank> GetBank();
   {
      using(ShoppingEntities _ShoppingEntities = new ShoppingEntities())
      {
          return _ShoppingEntities.Bank.ToList(); 
      }
   }
}
</ema.shopping.model.bank>


and for you Form Code Behind:
C#
class.BankAccount _BankAccount = new BankAccount();
BankIDCombobox.DisplayName = "Name";
BankIDCombobox.ValueMember = "BankID";
BankIDCombobox.DataSource = _BankAccount.GetBank();


hope it will solve your problem
best regards
 
Share this answer
 
Comments
[no name] 3-Oct-16 7:04am    
Do you know that the OP has switched over to using Entity Framework in the past FOUR years since he posted his "question"?
C#
SqlConnection con;
string dbConnectionString = @"Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True";
string sqlTable = null;
con = new SqlConnection(dbConnectionString);
con.Open();

sqlTable = "Select Device_name from Monitor_devices";
SqlDataAdapter dscmd = new SqlDataAdapter(sqlTable, con);
DataSet ds = new DataSet();
dscmd.Fill(ds);
comboBoxColumn.DataSource = ds.Tables[0];
comboBoxColumn.DisplayMember = "Device_name";
con.Close();
 
Share this answer
 
v2
Comments
[no name] 2-Oct-16 15:59pm    
Good job. You successfully proved that you can resurrect already answered FOUR year old questions by copying an already existing solution. Well done.

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