ComboBox

N

nbicc

I am trying to add some items to a ComboBox with the following code, will
someone please tell me what I am doing wrong, the Items.Add is where the
problem is I think. Thanks for your help

strSQL = "SELECT [bid-number] FROM [job-setup] Where [company-code] = " +
PrepareStr(ccode);

con = new
SqlConnection("Server=icc-server;DataBase=Construction;Integrated
Security=SSPI;Persist Security Info=False");

comm = new SqlCommand(strSQL, con);

if (con.State != ConnectionState.Open)
{
con.Open();
}

conrdr = comm.ExecuteReader();

while (conrdr.Read())
{
cbBidnumber.Items.Add("bid-number");
}

con.Close();
 
M

Morten Wennevik [C# MVP]

Well, your code will add the string "bid-number" for each bid-number you have
and you will end up with a ComboBox full of "bid-number" strings. You might
get it to work if you change the Add line to

cbBidnumber.Items.Add(conrdr["bid-number"]);

You should also consider putting more space between the data layer and the
gui, if nothing else, try putting the data reading code in a Method returning
a List<int> containing the bid-numbers, and use this list as the
ComboBox.DataSource


private void FillComboBox()
{
comboBox1.DataSource = GetBidNumbers("myCompany");
}

public static IEnumerable<int> GetBidNumbers(string companyCode)
{
List<int> bidNumbers = new List<int>();
...
while(conrdr.HasRows && conrdr.Read())
bidNumbers.Add((int)conrdr["bid-number"]);

return bidNumbers;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top