473,698 Members | 2,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem Binding a Dataset to a ComboBox

I have a subroutine that I call to bind a dataset to a combobox. It
works sometimes. See the code below..

1 Private Sub BindDropDownToD ataSet( _
2 ByRef control As System.Windows. Forms.ComboBox, _
3 ByVal ds As DataSet)
4 Try
5 control.DataSou rce = ds.Tables(0)
6 control.ValueMe mber = ds.Tables(0).Co lumns(0).Column Name
7 control.Display Member =
ds.Tables(0).Co lumns(1).Column Name
8 Catch e As Exception
9 MsgBox(e.Messag e)
10 End Try
11 End Sub

Code to call my sub is ...

BindDropDownToD ataSet(ComboBox 1, Dataset1)
BindDropDownToD ataSet(ComboBox 2, Dataset2) <-- fails on this call

The problem is that one time it will work just fine. Then I call it
to fill another combobox and it fails on line 6 with the error ...

"Cast from type 'DataRowView' to type 'String' is not valid."

What is really weird is that while debugging, after it executes line 6
it goes to the catch. Then if I "Set the Next Statement" to line 6,
it works just fine.
Nov 20 '05 #1
3 1858
Cor
Hi Russ,

I do not see it,
However

1 Private Sub BindDropDownToD ataSet( _
2 ByRef control As System.Windows. Forms.ComboBox, _
3 ByVal ds As DataSet)
Why byRef a control can done byval because it is a reference so a ByVal is
normal Byref gives extra code
4 Try
Why a try, if it works onces it works for ever, there are no external
influences
(Or is this only for testing?)
5 control.DataSou rce = ds.Tables(0)
6 control.ValueMe mber = ds.Tables(0).Co lumns(0).Column Name
7 control.Display Member =
ds.Tables(0).Co lumns(1).Column Name
8 Catch e As Exception
9 MsgBox(e.Messag e)
10 End Try
11 End Sub

Code to call my sub is ...

BindDropDownToD ataSet(ComboBox 1, Dataset1)
BindDropDownToD ataSet(ComboBox 2, Dataset2) <-- fails on this call

For me the most probably it that the Select from the dataset2 contains only
one element.
(However if not I think there is something with the creation of the dataset)

So when you do not find it, send the code how you create that dataset2
The problem is that one time it will work just fine. Then I call it
to fill another combobox and it fails on line 6 with the error ...

"Cast from type 'DataRowView' to type 'String' is not valid."

What is really weird is that while debugging, after it executes line 6
it goes to the catch. Then if I "Set the Next Statement" to line 6,
it works just fine.


I hope this helps?

Cor
Nov 20 '05 #2
"Cor" <no*@non.com> wrote in message news:<eW******* *******@tk2msft ngp13.phx.gbl>. ..
Hello Cor,

I solved the problem, but I am not sure why.

I had a subroutine

Private Sub ComboBox2_Selec tedIndexChanged (ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
ComboBox2.Selec tedIndexChanged

. . . code goes here
End Sub

I changed it to

Private Sub ComboBox2_Selec tionChangeCommi tted(ByVal sender As Object,
ByVal e As System.EventArg s) Handles
ComboBox2.Selec tionChangeCommi tted

. . . code goes here
End Sub

Now everything is working.

Any Ideas of why "SelectedIndexC hanged" would cause
control.ValueMe mber to fail, yet "SelectionChang eCommitted" would not?

Hi Russ,

I do not see it,
However

1 Private Sub BindDropDownToD ataSet( _
2 ByRef control As System.Windows. Forms.ComboBox, _
3 ByVal ds As DataSet)
Why byRef a control can done byval because it is a reference so a ByVal is
normal Byref gives extra code


I changed it to byVal, it made no difference.
4 Try


Why a try, if it works onces it works for ever, there are no external
influences
(Or is this only for testing?)
5 control.DataSou rce = ds.Tables(0)
6 control.ValueMe mber = ds.Tables(0).Co lumns(0).Column Name
7 control.Display Member =
ds.Tables(0).Co lumns(1).Column Name
8 Catch e As Exception
9 MsgBox(e.Messag e)
10 End Try
11 End Sub

Code to call my sub is ...

BindDropDownToD ataSet(ComboBox 1, Dataset1)
BindDropDownToD ataSet(ComboBox 2, Dataset2) <-- fails on this call


For me the most probably it that the Select from the dataset2 contains only
one element.
(However if not I think there is something with the creation of the dataset)

So when you do not find it, send the code how you create that dataset2


The dataset is fine. It even fails if I use the same dataset.
i.e.
BindDropDownToD ataSet(ComboBox 1, Dataset1)
BindDropDownToD ataSet(ComboBox 2, Dataset1) <-- fails on this call

The problem is that one time it will work just fine. Then I call it
to fill another combobox and it fails on line 6 with the error ...

"Cast from type 'DataRowView' to type 'String' is not valid."

What is really weird is that while debugging, after it executes line 6
it goes to the catch. Then if I "Set the Next Statement" to line 6,
it works just fine.


I hope this helps?

Cor

Nov 20 '05 #3
Cor
Hi Russ,

No,

However the selectedindexch anged event fires also when it is initializing
and then there happens sometimes things I also do not understand. If I use
it I always protect it for firing with a bool/switch/flaw whatever you name
it.

Check very good if the SelectionChange Committed is good working for you.
I thought that if you bind the displaymember and the valuemember there where
no problems, but I check that always 10 times. (There where situations that
it did give with me the previous valuemember).

Cor
Nov 20 '05 #4

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

Similar topics

6
7478
by: Omar | last post by:
When I try to databind my comboBox (specifically field "emplcode") to a filled dataset , the contents of the comboBox displays a bunch of "System.Data.DataRowView". I assume the amount of times "System.Data.DataR..." is displayed inside the combobox is the amount of records in the dataset. On the other hand, if my query is "select emplcode from payemployee", the databind will work fine (but I don't want to limit the dataset to one field)....
13
4319
by: Paul Slavin | last post by:
I have a textbox bound to a dataview, when I update the text in the textbox no changes take place in the underlying dataset. Why is this?? any answers appreciated, as to due to the underlying structure of the datasets, i.e lots of child tables etc, I cannot use bindingcontext
5
6681
by: Poonam | last post by:
I have a department table with DeptID and DeptName. I am trying to fill my combobox with DeptName. Here is my code but it does not work? Can someone help me? Dim pxy As New Channel1.Channel dsDepts = pxy.getDepartments() cmbDept.DisplayMember = "DeptName" cmbDept.ValueMember = "DeptID" cmbDept.DataSource = dsDepts.Tables("Department")
30
4583
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
0
2211
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue correctly, posting to the wrong forum, or committing some other sort of faux pas. My team is developing a Windows Forms application using VS 2005 with SQL Server 2005 on the back end and we're having a problem using ComboBoxes data bound to lookup...
0
1404
by: Tommaso Caldarola | last post by:
2.0 - No Typed DataSet. My custom object implements all the needed interface (IList, IBindingList, ITypedList) in order to manage complex binding. With DataGridView control I have no problem. I have encountered problems when I try using data bound ComboBox, namely if I set combo.DataSource = OrderTypeCollection; (of OrderType class e.g.)
3
1646
by: Peter | last post by:
OK, so in addition to the problem I mentioned before (c.f. "Data Binding to Textbox"), I realized that my comboboxes are not really bound to the internal data set. They are just set to manually change to reflect whatever the selected row's foreign keys are. So the question is, how can I bind the SelectedItem of a combobox, which is filled with DataRowView objects from its bound table, to a field in whichever row of a dataset happens to...
1
1764
by: Robert Dufour | last post by:
I have a table of addresses and it contains a CountryId which points to a table of countries. The datatstructure of the Countries Lookup tables is actually created from three tables - a CountryIdtable - a Countries localized tables and an cultures tables From which I extract a dataset select CountryID - CountryName - Culture name (with inner joins between the three) where CultureName = CurrentCulture.CurrentUiCulture.tostring. So I would...
1
2233
by: Jim | last post by:
Hi, Trying to implement databinding. I've a dataset with a table, a windows form with typical controls: combobox, datetimepicker, textbox . . . I use what I think is a normal method to establish the bindings: textbox1.databindings.add("text", dataset, table.column) The records displays in the UI(if it exists). I can create an initial record and successfully save to the database.
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.