Solved

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

Posted on 2004-08-25
6
394 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.

743 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

10 Experts available now in Live!

Get 1:1 Help Now