Solved

Combo boxes

Posted on 2003-11-11
9
728 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

20 Experts available now in Live!

Get 1:1 Help Now