?
Solved

Combo boxes

Posted on 2003-11-11
9
Medium Priority
?
735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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