Solved

Fill a ComboBox using SQL statement, and return the primary key of the table

Posted on 2004-08-25
6
396 Views
Last Modified: 2008-03-10
Hi Guys,

I have a table (tblProducts)
with columns:

ProductID (int Primary Key)
ProductName (VarChar)

Now what I want to do is a combo box that will list all the ProductName in the table, but that will return the associated ProductID.

The tricky part: My form has about 30 of these ComboBox, and I dont want to use Datasets, only SQL statements...

Anyone still wants to give it a try ?!
0
Comment
Question by:Francis_B
6 Comments
 
LVL 1

Expert Comment

by:chmohan
ID: 11893968
return it to where? You mean you want to update what he selects in the combobox ?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11893974
use a datareader
       'define a sql connection if you dont already have one ...        
       Try
           Cn.Open() 'open your connection
           Dim reader3 As SqlDataReader
           reader3 = cmd.ExecuteReader()
           While reader3.Read
               Combobox3.Items.Add(reader3.GetInt32(0), reader3.GetString(1))
              End While
       Catch ex As Exception
           MsgBox(ex.Message)
       Finally
           Cn.Close()
       End Try
0
 

Author Comment

by:Francis_B
ID: 11894114
Well, the combobox Value (the ProductID) will be saved in the tblOrders table.
So basically I want the items in the ComboBox list to be retreived from tblProducts, and then save the selected item's ID to the tblOrders table.

Please let me know if I'm not clear !
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 5

Expert Comment

by:jjacksn
ID: 11894622
I do not believe that you can select more than one item in a combo box, can you?
0
 

Author Comment

by:Francis_B
ID: 11894879
gregoryyoung, how would you translate this to C#?
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11896891
whoops I thought I was in vb.net forum I prefer C# :)

       //define a sql connection Cn if you dont already have one ... and a SqlCommandObject with your query/sproc
       try {
           Cn.Open() 'open your connection
           SqlDataReader reader3;
           reader3 = cmd.ExecuteReader();
           while(reader3.Read()) {
               Combobox3.Items.Add(reader3.GetInt32(0), reader3.GetString(1));
            }
       }
       catch Exception ex {
           MsgBox(ex.Message);
       }
       finally {
           Cn.Close();
       }
       End Try
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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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 …

919 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

18 Experts available now in Live!

Get 1:1 Help Now