Solved

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

Posted on 2004-08-25
6
398 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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