Solved

Combo boxes

Posted on 2003-11-11
9
730 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 41
WCF Service Application cannot connect from TCP terminal 1 31
Hey!!!! 1 20
C# LINQ ForEach() question 6 19
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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