473,480 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

reading data from dataset bound to a combo box.

Hi there..

I am a VB6 developer so kinda trying to figure how to work with
VB.NET. I have a scenario where i have a DataSet bound to a combo box,
and i would like to read a row from the DataSet based on the item
selected in the Combo Box.

For e.g.
I have a dataset that has 1 table containing 3 fields say ID, Name,
and Address.
This dataset is bound to a Combo Box with ID as a ValueMember and Name
as a DisplayMember.
The Combo Box fills up perfectly with the Name field.

The problem i am facing is, when i select a Name from the Combo Box, i
would like to read the Address for that Name from the DataSet.

I did find a few samples but they would go through the loop to read
rows in a DataSet, but i could not find a sample where i can read a
row from a DataSet for a selected value.

Any help on this one will be much appreciated. Thanks.

Hetal.

Apr 25 '07 #1
5 5218

If youre workign with winforms (as opposed to asp.net) the number 1 thing to
remember/know about comboboxes.

You put (some kind of) object in a combobox, and NOT value/text pairs.

What do I mean?

If you have a class

public class Emp

and a collection of Emp's
public class EmpCollection

and you bind a combobox to some EmpCollection

Each item in the combobox is an Emp object.

You probably have a DataRow object in each item of the combobox.

You'll have to pull it out, and cast it to work with it.


"Hetal" <he*************@gmail.comwrote in message
news:11**********************@c18g2000prb.googlegr oups.com...
Hi there..

I am a VB6 developer so kinda trying to figure how to work with
VB.NET. I have a scenario where i have a DataSet bound to a combo box,
and i would like to read a row from the DataSet based on the item
selected in the Combo Box.

For e.g.
I have a dataset that has 1 table containing 3 fields say ID, Name,
and Address.
This dataset is bound to a Combo Box with ID as a ValueMember and Name
as a DisplayMember.
The Combo Box fills up perfectly with the Name field.

The problem i am facing is, when i select a Name from the Combo Box, i
would like to read the Address for that Name from the DataSet.

I did find a few samples but they would go through the loop to read
rows in a DataSet, but i could not find a sample where i can read a
row from a DataSet for a selected value.

Any help on this one will be much appreciated. Thanks.

Hetal.

Apr 25 '07 #2
Thank you sloan. I created an object of class DataRowView and i am
casting the item selected in Combo Box to DataRowView. Keeping the
previous example in mind, here is the snippet of code that i wrote to
get the Address from DataSet.

'In Declaration section.
Dim drvStores As DataRowView

'Code to read the data from dataset based on combo item selection.
Private Sub cmbStores_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbStores.SelectedIndexChanged
drvStores = DirectCast(cmbStores.SelectedItem, DataRowView)
MsgBox(drvStores("Address"))
End Sub

Am i heading the right direction? I mean, is this an efficient way of
reading data from a dataset? Thanks.

Apr 26 '07 #3
oh.. i missed mentioning that i am working with windows forms. thanks.

Apr 26 '07 #4

Youre on the right path.

Private Sub cmbStores_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbStores.SelectedIndexChanged

'try this

dim o as object
o = cmbStores.SelectedItem
if not (o is nothing) then
Console.Write ( o.GetType().ToString() )
'put a watch on "o" in the debugging window, and it should tell you
the type also
end if

' drvStores = DirectCast(cmbStores.SelectedItem, DataRowView)
' MsgBox(drvStores("Address"))

End Sub

the dim o as object is ONLY FOR FIGURING OUT WHAT YOU GOT.
DO NOT LEAVE THAT IN THERE.

after you have the object type, then you can cast.
PS

You might want to bind your combobox to the dataset.TABLE , not just hte
dataset.

Ex:

if not ( ds.Tables(0) is nothing) then

cbo1.DataSource = ds.Tables(0)

end if

OR better (if its a strong typed dataset)
if not ( ds.Employee is nothing) then

cbo1.DataSource = ds.Employee

end if
where Employee is a strong table name in your dataset.


"Hetal" <he*************@gmail.comwrote in message
news:11**********************@t38g2000prd.googlegr oups.com...
Thank you sloan. I created an object of class DataRowView and i am
casting the item selected in Combo Box to DataRowView. Keeping the
previous example in mind, here is the snippet of code that i wrote to
get the Address from DataSet.

'In Declaration section.
Dim drvStores As DataRowView

'Code to read the data from dataset based on combo item selection.
Private Sub cmbStores_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbStores.SelectedIndexChanged
drvStores = DirectCast(cmbStores.SelectedItem, DataRowView)
MsgBox(drvStores("Address"))
End Sub

Am i heading the right direction? I mean, is this an efficient way of
reading data from a dataset? Thanks.

Apr 26 '07 #5
Thanks Sloan. It was a very helpful piece of info.

Apr 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1796
by: Paul Edwards | last post by:
I am writing in c# with Visual Studio 2003. I am creating a form that shows one record from a Dataset that is populated with only one record from SQL Server 2000. I am using bound controls, both...
0
1760
by: Neo | last post by:
I was wondering what is the "right" way to deal with datasets is. Particularly sharing DataSets between forms. Here is my situation. I have a simple Customer Database, that holds some information...
2
1156
by: resti | last post by:
hi how can i filter my dataset using the value of my combo box thnks
1
1467
by: Hardik Shah | last post by:
Hi, I have created a entry module with data binding , I have two combo box with same data adapter with same data set. when I change the first combobox's value, there is automatically changed to...
1
4852
by: Bill | last post by:
Problem: Combo box data disappears from view when a requery is done See "Background" below for details on tables, forms & controls On a form, I want to use the setting of bound combo box C1...
0
1252
by: Hetal | last post by:
Hi there.. I am a VB6 developer so kinda trying to figure how to work with VB.NET. I have a scenario where i have a DataSet bound to a combo box, and i would like to read a row from the DataSet...
8
3740
by: T Driver | last post by:
Anyone have any idea how I can do the following? I have a connection to an XML file on a site I do not control, getting a string representation of the xml data that I can then feed to my...
1
3554
by: Bob Johnson | last post by:
..NET 3.5, Windows Forms app: My objective is to display parent/child data in two ComboBox controls, such that when a parent item is selected in one combo box, associated child items appear in the...
10
5818
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi, Problem: How can I databind (or put) a SqlServer query's row return of 115,000 items into a ComboBox quickly? Not much longer than a matter of seconds, that is... Scenario: I am...
0
6908
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...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.