Solved

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

Posted on 2004-08-25
6
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 93
asp web application 3 55
Create a Dropdown list with sub group 10 39
Health check of winows and webservces 5 50
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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