473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

VB.NET: ComboBox in dynamic SQL Statement Question

Have some mercy on me, I am a NEWBIE! :rolleyes:

I was wondering if in VB.NET it was possible to take a combobox and use that as part of a select statement to change a label. Obviously this is done through an event handler, but I am not sure how to set up the code to use the selected value of the combo box and insert it in a sql statment.

I have no sample code, for I am lost in even starting this part of the project. Please help anybody?!
Aug 9 '06 #1
1 5542
krodman
12
In .NET a ComboBox can be used similarly as an HTML DropDownList in that it has a SelectedValue and a SelectedText property. This only applies when you're binding the ComboBox to a DataSource, and have the DisplayMember & ValueMember properties set. To know when the user changes their selection use the SelectedIndexChanged event of the ComboBox, and get the value from the property having the data that you need.

Bound ComboBox Example:
'Preferrably you would be getting your binding data from the database
Private Function ExampleDataTable() As DataTable
Dim dt As New DataTable()
Dim ComboDisplayData() As String = {"Display Text One", "Display Text Two"}
Dim ComboValueData() As Integer = {0, 1}

With dt.Columns
.Add("ValueData")
.Add("DisplayData")
End With

For i As Integer = 0 To ComboDisplayData.Length - 1
Dim row As DataRow = dt.NewRow

row("ValueData") = ComboValueData(i)
row("DisplayData") = ComboDisplayData(i)

dt.Rows.Add(row)
Next

Return dt

End Function

Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

ComboBox1.DisplayMember = "DisplayData" 'Data displayed to user
ComboBox1.ValueMember = "ValueData" 'The value needed for sql statement
ComboBox1.DataSource = Me.ExampleDataTable()

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim ValueNeededForSqlStatement As Integer = CInt(ComboBox1.SelectedValue)

'Your SQL Statement with concatenated value here
'Lable.Text = Value From Database

End Sub

Static ComboBox Items Example:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim ValueNeededForSqlStatement As String = Trim(ComboBox1.SelectedText)

'Your SQL Statement with concatenated value here
'Lable.Text = Value From Database

End Sub

I hope this help!
Aug 13 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: klh | last post by:
We use DB2 Connect v 7.2 FP7 in Windows NT hitting a OS/390 DB2 v7.1 database. We have a Websphere (java) application that issues dynamic SQL. Most of the time when we issue dynamic SQL SELECT...
3
by: rmatteson | last post by:
I am trying to figure out how to clear all items from a combobox (Access 2002). On my form, I have to comboboxes. Combobox 2 is populated with a set of child data dependent on the parent data...
6
by: Sakharam Phapale | last post by:
Hi All, How to fill one ComboBox from other ComboBox control? 1) Only setting the reference does the trick but doesn't show items in control. If you see in immediate window, it shows...
2
by: SKarnis | last post by:
We are trying to rebuild a current Access 2002 (XP) mdb program to VB.NET with a SQL database - we are having problems with a suitable combobox. There are many threads discussing multiple column...
11
by: simon | last post by:
hello, I have a form that has 10 dropdown lists in one section each of the dropdowns contain the same data what i'm looking to do is that when a user selects a value from one dropdown, change...
2
by: Newbie | last post by:
I have a combobox on a form. Coming from VB, I like to build a dynamic SQL statement and then set the control's data source to the SQL statement. Please tell me how to do that in access using...
3
by: fedya | last post by:
I am trying to have the last 12 months to always be the option in the dropdown for a combo box. (Basically a combobox, with dynamic options) I am using Access 2000. What is the function and...
3
by: DEC | last post by:
I have a VB application that's about 95% done. As usual, it's that last 5% that's got me stymied. The program is a Bill-of-material update program that interfaces with an SAP database. The BOM's...
6
by: robtyketto | last post by:
Greetings, Im a newbie to JSP/Javascript (unfortunalley). However I found enough example to create code that connects to my access db and populates two combo boxes. The selection of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.