SaraniO
SaraniO

Reputation: 607

Combobox doesn't show display member and value member in C#

With access I don't have this problem but now that I'm using SQL Serever the Combobox doesn't work as it work before.

this is how I get DisplayMember and ValueMember from database :

using (SqlConnection SqlCon = new SqlConnection(StrCon))
    {
        using (SqlDataAdapter SqlDa = new SqlDataAdapter(
               "Select HiveID, HiveNumber From tHives", SqlCon))
        {
            using (DataTable Dtable = new DataTable())
            {
                SqlDa.Fill(Dtable);

                HiveNumbercmb.DataSource = Dtable;
            }
        }
    }

And combobox property is set like this:

(in property window not by Code)

DisplayMember = HiveNumber
ValueMember = HiveID

but still instead of showing my HiveNumber it shows 3 Empty Item.

where is the problem?

Upvotes: 1

Views: 302

Answers (1)

LarsTech
LarsTech

Reputation: 81610

The Using block disposes the data table:

using (DataTable Dtable = new DataTable()) {
  SqlDa.Fill(Dtable);
  HiveNumbercmb.DataSource = Dtable;
}

so remove that and just declare the variable:

DataTable dTable = new DataTable();
SqlDa.Fill(dTable);
HiveNumbercmb.DataSource = dTable;

Upvotes: 2

Related Questions