Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combo boxes

Posted on 2003-11-11
9
Medium Priority
?
737 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
Industry Leaders: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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