Problem : VB.NET, Combo Box Conversion from type ‘DataRowView’ to type ‘String’ is not valid error

Problem : VB.NET, Combo Box Conversion from type ‘DataRowView’ to type ‘String’ is not valid error

Hi,

I have a vb.net application I am developing in Visual Studio 2005.

I have two combo boxes on a form, one which displays a list of room types in a hotel, and the second combo box displays a list of room types for the selected room type selected in the first combo box.

The combo boxes are populated by a datatable (mysql).

Now I can populate both combo boxes perfectly fine, however I wanted to add an event onchange for the first combo box of room types. On change it is to repopulate the second combo box with what rooms are linked to the selected room type that was just changed.

However I get this error when I load the form,

” Conversion from type ‘DataRowView’ to type ‘String’ is not valid. ”

I assume this has to do with the fact that when the form is intially loaded somehow the onchange function is called on the second combo box before the first combo box is populated with any room types. If I preset the variable for the second combo box, then after the inital load of the form it works on the change event.

Can somebody please help?

Code of on change for first combo box.

Private Sub cboRoomType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboRoomType.SelectedIndexChanged

‘When room type box changed, lets change the list to what rooms we have available

Dim SelectedValue As String
SelectedValue = cboRoomType.SelectedValue

‘Run available rooms procedure.
LoadAvailRooms()

Label21.Text = SelectedValue

End Sub

‘ Code on loadresvariables that populates the second combo box of the room numbers available.

Private Sub LoadAvailRooms()

‘Load room numbers for selected room type.
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim dt As New DataTable

conn.ConnectionString = “server=server.com;user id=username;password=password;database=mydatabase”

myCommand.Connection = conn
myCommand.CommandText = “SELECT room_number FROM config_roomnumbers WHERE room_type_id = ” & cboRoomType.SelectedValue & “”

Try
conn.Open()
myCommand.ExecuteNonQuery()
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dt)

cboRoomNumber.DataSource = dt
cboRoomNumber.DisplayMember = “room_number”

Catch myerror As MySqlException
MessageBox.Show(“Error Connecting to Database: ” & myerror.Message)

Finally
conn.Dispose()
End Try

End Sub


Solution : VB.NET, Combo Box Conversion from type ‘DataRowView’ to type ‘String’ is not valid error

The code you show doesn’t capture the essential point I was making.  It says

cboRoomType.DataSource = dt
cboRoomType.DisplayMember = “type_shortcode”
cboRoomType.ValueMember = “id”

whereas, to set the .DisplayMember and .ValueMember BEFORE the .DataSource, would require it to say

cboRoomType.DisplayMember = “type_shortcode”
cboRoomType.ValueMember = “id”
cboRoomType.DataSource = dt

But there’s another issue.  I don’t know where you’re declaring dt, but it looks at least possible that it’s just an empty table – no columns, no rows – when this line is hit

cboRoomType.DataSource = dt

If that is the case, with your existing code, the binding manager will look at that empty table when it encounters this line

cboRoomType.DisplayMember = “type_shortcode”

and say “I can’t bind to that, it doesn’t exist”.

Try it like this

myCommand.Connection = conn
myCommand.CommandText = “SELECT type_shortcode, id FROM config_roomtype ORDER BY type_shortcode”

Try
conn.Open()
myCommand.ExecuteNonQuery()
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dt)

cboRoomType.DisplayMember = “type_shortcode”
cboRoomType.ValueMember = “id”
cboRoomType.DataSource = dt

Catch myerror As MySqlException
MessageBox.Show(“Error Connecting to Database: ” & myerror.Message)

Finally
conn.Dispose()
End Try