Solved

Combo boxes

Posted on 2003-11-11
9
724 Views
Last Modified: 2010-04-16
I need to auto populate a combo box from a database.  For example, assuming I have a DB that has a table called Client, and client table is in MS Access like:

Client
ClientID    ClientName    ClientPhone
100002    Best Buy         981-0006
239292    Hooters          393-8434
938423    Powersource   434-4312


then when I populate the combobox called client, I need to have:

<comboBox1>
     <clientID="100002">Best Buy
     <clientID="239292">Hooters
     <clientID="938423">Powersource
</combobox1>

does that make sense?  How do I populate that?  Thanks.

0
Comment
Question by:substand
  • 3
  • 3
9 Comments
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 9728985
hi
try the following code i hope this will help u

            OleDbDataReader dr;
            string sQry="Select ClientId,ClientName from Client";
            OleDbCommand cmd=new SqlCommand (sQry,conn);
            dr=cmd.ExecuteReader();
            DropDownList1.DataSource=dr;  
            DropDownList1.DataTextField="ClientName";    
            DropDownList1.DataValueField="ClientId";
            DropDownList1.DataBind () ;
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9729334
I would make a dataset with a table inside, then fill in the data with a data adapter (with only select command probably) and then set the DataSource, DataMember, DataValueField and DataText field properties. The data adapter and dataset can be made directly from within the IDE and are easy to configure and work with.
0
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 9729413
ofcourse u can use dataadapter also and it is better to use it instead of datareader
so u can use as follows
actually the main point is populating combo , so u can use anything u want

SqlDataAdapter da=new SqlDataAdapter(sQry,Global.conn );  
DataSet ds=new DataSet();
da.Fill (ds);
DropDownList1.DataSource=ds;  
DropDownList1.DataTextField="LogIn";    
DropDownList1.DataValueField="Id";
DropDownList1.DataBind () ;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:rashmitodkar
ID: 9729440
oops! the abv code is mine , i mean for my app. so just replace it with ur conditions like

string sQry="Select ClientId,ClientName from Client";
OleDbDataAdapter da=new OleDbDataAdapter (sQry,conn );  
DataSet ds=new DataSet();
da.Fill (ds);
DropDownList1.DataSource=ds;  
DropDownList1.DataTextField="ClientName";    
DropDownList1.DataValueField="ClientId";
DropDownList1.DataBind () ;

0
 
LVL 10

Author Comment

by:substand
ID: 9743973
OleDbConnection connection;
OleDbCommand command;
try
      {
            //initialize dr dropdown
            using(connection = new OleDbConnection(dbConnectionString))
            {
                  connection.Open();
                  using(command = connection.CreateCommand())
                  {
                        string DoctorQuery="Select StaffID, LastName from Staff where NurseOrDoctor=1 order by LastName";
                        command.CommandText = DoctorQuery;
                        OleDbDataReader dr = command.ExecuteReader();
                        DoctorID.DataSource=dr;  
                        DoctorID.Text="LastName";    
                        DoctorID.ValueMember="StaffID";
                        DoctorID.DataBind () ;
                        dr.Close();
                  }
            }
                 }
      catch( Exception ex)
      {
            MessageBox.Show( ex.Message);
                                this.Close();
      }
      
0
 
LVL 10

Author Comment

by:substand
ID: 9743998
I am using windows forms, not web forms if that makes a difference.  what you said didn't work, so I changed to this.  There is also no databind method so I took it out and ran it and it said complex databinding only allows an IList ???

any ideas?
0
 
LVL 10

Accepted Solution

by:
substand earned 0 total points
ID: 9744410
Nevermind, I found it myself:

(assuming connection and command are predefined)

string NurseQuery="Select StaffID, LastName from Staff where NurseOrDoctor=0 order by LastName";
DataTable nurseTable=new DataTable();
command.CommandText = NurseQuery;
OleDbDataAdapter da2=new OleDbDataAdapter(command);
da2.Fill(nurseTable);
                  
NurseID.DataSource=nurseTable;  
NurseID.DisplayMember=nurseTable.Columns["LastName"].ToString();    
NurseID.ValueMember=nurseTable.Columns["StaffID"].ToString();
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now