Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am making window application using C#.NET and SQL SERVER 2008.I want that thedata which is stored in SQL SERVER get displayed on combobox.I tried following code but couldnt make it.

private void cbomedia_MouseClick(object sender, MouseEventArgs e)
        {         
             SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=PSM");
            con.Open();
           SqlCommand com = new SqlCommand("select * from Target_Audience", con);
            SqlDataReader reader;
                       
               reader = com.ExecuteReader();
              cbomedia.DataSource = reader;
       cbomedia.DisplayMember = "Media_method_ID";
              reader.Close();
                con.Close();
       
        }

i got an error
complex DataBining accepts as a data source either an IList or an IListSource

Please tell me
regards
shivani
Posted

try it with Dataset

Dataset ds = new Dataset();
DataAdapter dt = new dataAdapter(Com);
dt.fill(ds);

comboBox2.DataSource = ds;
comboBox2.DisplayMember = "MemberName";
comboBox2.ValueMember = "MemID";



C#
//with DataReader
try
{
// opening the connection
connection.Open();
OleDbDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
// Iterate through the table
while (dr.Read())
{
//adding item to the combobox. you can also use dr[1] to get the country name
comboBox1.Items.Add(dr["CountryName"]);
}
}
}
catch (Exception ex)
{
MessageBox.Show("The following error occured :rn" + ex.ToString());
}
finally
{
// close the connection and DataReader
connection.Close();
dr.Close();
}
}
 
Share this answer
 
v2
Comments
shivani 2013 2-May-11 1:47am    
I am getting An error
Cannot bind to the new display member
Parameter name:newDisplayMemeber
Mahendra.p25 2-May-11 1:58am    
write your Query like

you are Using DataSet or DataReader if DataSet check that your Query is returning

Media_method_ID from database or not
Mahendra.p25 2-May-11 2:01am    
comboBox1.DataSource=ds.Tables[0];
comboBox1.DisplayMember="Media_method_ID";
Hi,

Please use DataSet instead of SqlDataReader.

for More details go through with this article
DataSet with combobox


Regards
AR
 
Share this answer
 
v2

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