Hi all,

I have a combo box which i wish to display all the Usernames from the table Users, but with the UsergroupID of 1. However my codes currently displays ALL Username records. How can i modify it to only fetch Usernames records with the UsergroupID of 1?

Codes as follows:

string query = "SELECT Username FROM Users";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, sConnectionString);
DataTable source = new DataTable();
dAdapter.Fill(source);
UsernameComboBox.DataSource = source;
UsernameComboBox.DisplayMember = "Username";

Table: Users
- ID (PK)
- Username
- UsergroupID

Any help will be greatly appreciated, cheers!

Recommended Answers

All 2 Replies

I have solved it, thanks.

string query = "SELECT Username, UsergroupID FROM Users WHERE (UsergroupID = 1)";
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, sConnectionString);
            DataTable source = new DataTable();
            dAdapter.Fill(source);
            UsernameComboBox.DataSource = source;
            UsernameComboBox.DisplayMember = "Username";

You need to change your SQL Query. Please see the WHERE clause

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.