?
Solved

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

Posted on 2004-08-25
6
Medium Priority
?
406 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
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 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 2000 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

Technology Partners: 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!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 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