473,569 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

combobox gives unexpected cast error

using VS2005 - VB.Net

I noticed unexpected behaviour when using a combobox which has it's
datasource set to a datatable. Also the Valuemember and Displaymember are
being set. I'm getting a cast error when trying to read the SelectedValue.
However, if I change the order in which DataSource, Valuemember and
Displaymemeber are set, then the cast error is not thrown. What is happening
here?

Example (the error appears at ***) where ID is an integer, Name is a string:

Public Class FrmTestComboBox

Private Sub FrmTestComboBox _Load(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles MyBase.Load
FillCombo()
End Sub

Public Sub FillCombo()
ComboBox1.DataS ource = MyTable
ComboBox1.Value Member = "ID"
ComboBox1.Displ ayMember = "Name"
End Sub

Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
ComboBox1.Selec tedIndexChanged
Dim val As Integer
Dim row As DataRowView

If ComboBox1.Selec tedIndex -1 Then
*** val = CInt(ComboBox1. SelectedValue)
row = DirectCast(Comb oBox1.SelectedI tem, DataRowView)
Label1.Text = CStr(row("Name" ))
End If
End Sub
End Class
If I rewrite this (setting DataSource as the last line), it works as expected.

Public Sub FillCombo()
ComboBox1.Value Member = "ID"
ComboBox1.Displ ayMember = "Name"
ComboBox1.DataS ource = MyTable
End Sub

I did not expect the order of the lines of code would have any consequence
here ...
Also in the Documentation an example shows up (for a listbox) which first
sets the datasource, and then the valuemember and displaymember.
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20 .en/CPref17/html/P_System_Window s_Forms_ListCon trol_ValueMembe r.htm

Martin

--
It''s Software Jim, but not as we know it!
Jul 10 '06 #1
4 1330
Hello Martin,
If I rewrite this (setting DataSource as the last line), it works as
expected.

Public Sub FillCombo()
ComboBox1.Value Member = "ID"
ComboBox1.Displ ayMember = "Name"
ComboBox1.DataS ource = MyTable
End Sub
I did not expect the order of the lines of code would have any
consequence
When you set the DataSource property it's causincg the SelectedIndex event
to be raised. So your event handling code executes before you've told the
ComboBox which members in the table to bind to. When you re-order the statements
the properties are correctly set when the event is raised.

--
Jared Parsons [MSFT]
ja******@online .microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jul 10 '06 #2
Hi Martin,

Thank you for your post.

I think you're referring to following MSDN Library document:

http://msdn2.microsoft.com/en-us/lib...listcontrol.va
luemember(d=ide ).aspx

I have verified that the sample code does have this issue, it will trigger
the SelectedValueCh anged event three times if setting the DataSource first.

I have reported this issue to our MSDN team on this. Thank you for your
feedback! We strive to capture any and all product issues / product
feedback so as to ensure that we are continuously developing Microsoft
products to meet customer needs. This is exactly why feedback such as yours
is always taken very seriously.

Please feel free to post here if you need more help on this.

Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 11 '06 #3
Ha Walter and Jared,

Ah ... thanks. Indeed, I did not realize the event fired at that specific
piece of code.
Learning something everyday ;-)

--
It''s Software Jim, but not as we know it!
Jul 11 '06 #4
Hi Martin,

Thank you for your update.

Please feel free to post here if you need more help or anything is unclear.

Have a nice day!
Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 11 '06 #5

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

Similar topics

8
5416
by: | last post by:
Wel, I am rebuilding the VC# 2002 project that I have deployment problems with the 2003 version, hoping this solves the problems, but now I encounter this wierd bug??? If I have the project, and do not compile with "Allow Unsafe Code Blocks=false" set to true, then the project compiles and no problems. BUT if I compile with "Allow Unsafe...
5
7625
by: DS | last post by:
How do I go about binding a "struct" datatype to a comboBox? When I bind it by means of the DataSource, it doesn't give any error, but the comboBox just gets populated with "WindowsApplication2.Form1+Unit". Uncommenting the DisplayMember line has no effect. Uncommenting the ValueMember results in a crash: "Could not bind to the new display...
12
4698
by: Dica | last post by:
i can't seem to find a way to cast the value in my comboBox to integer. i've tried the following: short iProjectID = Convert.ToInt16(cboProjects.SelectedValue); i keep getting a "Sepcified cast is not allowed" error. how can i do this?
3
960
by: Don | last post by:
I'm getting a strange error from the following code: With Me.Combobox ' Clear list .Items.Clear() ' Add items to the list .Items.Add("item1") .Items.Add("item2")
3
1854
by: Russ | last post by:
I have a subroutine that I call to bind a dataset to a combobox. It works sometimes. See the code below.. 1 Private Sub BindDropDownToDataSet( _ 2 ByRef control As System.Windows.Forms.ComboBox, _ 3 ByVal ds As DataSet) 4 Try 5 control.DataSource = ds.Tables(0) 6 ...
10
2709
by: ZaphodBBB | last post by:
HI I have a form with 2 comboBoxes. The first comboBox gives me a list of customers to select from. I would like to have the second comboBox populated with the names of Departments. I only want the departments listed that match the Customer selected in the first combobox. Unfortunatly the original database that the tables were drawn from...
19
2191
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks like Text replaces an item or something like that. This results in a runtime error at which time I learn that the item that should be CQQ is a...
4
4955
by: EManning | last post by:
I have a combobox whose rowsource is a union query. This query displays a person's name in "lastname, firstname" format and in "firstname lastname" format. The query results look like this: Mouse, Mickey Mickey Mouse When a person is added, the querys' underlying recordset is updated in the NotInList event. I can't figure out how to...
5
5842
by: Christian | last post by:
Hello, i have a datagrid with some columns... the last column is a ComboBox with 2 items: "yes" and "no"... i'd like when i select a value "yes" the color's column became Yello.... someone have an idea.... thanks a lot!
0
7694
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...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
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...
0
5217
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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 we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
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...

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.