1

I am writing a small application in C# using windows forms. I have a combo box that I am populating by querying a database for column names to use as the values inside the combo box. My code currently can get the values just fine, however whenever I click on the combo box it removes any text and just displays a blank 'selected option'. I have tried multiple things to correct this (changed the database field from char to varchar), tried binding to a different dataset etc. but nothing has worked. Ive also looked at other posts on this such as C# comboBox databinding - nothing happens, then it goes back to blank

Below is my code, and I believe I am doing the displaymember/valuemember part wrong however I do not understand what it is that is wrong. The column name in the database is Reason and it consists of 3 values. Any help is appreciated.

String ConnString =    ConfigurationManager.ConnectionStrings["Portal1"].ConnectionString;
SqlConnection conn = new SqlConnection(ConnString);

conn.Open();

SqlCommand sc = new SqlCommand("select [Reason] from tblReasons", conn);
SqlDataReader reader;

reader = sc.ExecuteReader();
DataTable dt = new DataTable();


dt.Columns.Add("Reason", typeof(string));

dt.Load(reader);

cboxReason.ValueMember = "Reason";
cboxReason.DisplayMember = "Reason";
cboxReason.DataSource = dt;


conn.Close(); 
Community
  • 1
  • 1
G3TH
  • 247
  • 4
  • 14
  • Couple of questions, did you try without specifying cboxReason.ValueMember = "Reason"; cboxReason.DisplayMember = "Reason"; And, Are you sure there is data in "dt" – Gaurav Sharma Mar 12 '15 at 18:46
  • Have you tried `dt.DefaultView`? – bokibeg Mar 12 '15 at 18:46
  • 2
    Can you post a [minimal complete verifiable example](http://stackoverflow.com/help/mcve) because there is nothing wrong with your code. I have tried it and everything works fine - comboBox is filled and never loses its values. Perhaps you are doing something else with this comboBox? – Eugene Podskal Mar 12 '15 at 18:50
  • I agree with @Eugene, it **should** work though I haven't tried it. Maybe post a screenshot of what exactly happens with the combo box? – bokibeg Mar 12 '15 at 18:54
  • Thank you guys for looking at this with me. I tried the above (and below) solutions and nothing worked so I started at step 1 again. I fixed my problem, and it turns out that when created the database to draw values from I had set the type to char(50). When I changed this to varchar I forgot to manually remove all the white space created by the char. This in turn meant that when I went to grab that information from the database the return string would be to long to fit into the combo box field which made it remain blank. – G3TH Mar 12 '15 at 19:03

2 Answers2

1

Your code looks OK. I would not add the column, that should happen automatically. Here is my sample code that works:

        SqlConnection conn = new SqlConnection(ConnString);

        conn.Open();

        var reader = new SqlCommand("select ID from Users", conn).ExecuteReader();

        DataTable dt = new DataTable();
        dt.Load(reader);

        comboBox1.ValueMember = "ID";
        comboBox1.DisplayMember = "ID";
        comboBox1.DataSource = dt;
        conn.Close();

Note: This populates with the list of values in the column. For the list of column names, I would suggest changing your query to return a list of columns for the table (DB specific query) OR look at the DataTable.Columns collection for the column names.

Matthew Frontino
  • 496
  • 2
  • 12
0

Did you try the answer from C# - Fill a combo box with a DataTable

cboxReason.BindingContext = this.BindingContext;
Community
  • 1
  • 1
d89761
  • 1,434
  • 9
  • 11