Question Operator "&" is not defined for string and 'DataRowView'

The

Member
Joined
Nov 27, 2014
Messages
16
Programming Experience
3-5
I have that code:
VB.NET:
Dim SqlCmd As String
SqlCmd = "SELECT * FROM Floors Where [B_ID]=" & Buildings_List.SelectedValue
and I am getting the following error message:
Operator "&" is not defined for string "SELECT * FROM Floors Where [B_ID" and type 'DataRowView'

:( any idea please
 
Firstly, the fact that the SelectedValue is a DataRowView indicates that you have not set the ValueMember of the ListBox or whatever control that is. Just as you need to set the DisplayMember to determine which column's value gets displayed, so you must set the ValueMember to determine which column's value gets exposed via the SelectedValue.

Even then, SelectedValue is type Object so you're still relying on an implicit conversion. That indicates that you have Option Strict Off. Ideally, you would turn Option Strict On and then code like that would not even compile. If you have a String object exposed via an Object reference like that, you should be using CStr to cast it as type String.
 
Firstly, the fact that the SelectedValue is a DataRowView indicates that you have not set the ValueMember of the ListBox or whatever control that is. Just as you need to set the DisplayMember to determine which column's value gets displayed, so you must set the ValueMember to determine which column's value gets exposed via the SelectedValue.

Even then, SelectedValue is type Object so you're still relying on an implicit conversion. That indicates that you have Option Strict Off. Ideally, you would turn Option Strict On and then code like that would not even compile. If you have a String object exposed via an Object reference like that, you should be using CStr to cast it as type String.
Here is my code:
VB.NET:
Dim SqlCmd As String = "SELECT * FROM Buildings"
Dim DataAdapter As New OleDb.OleDbDataAdapter(SqlCmd, sqlconn)
Dim Buildings As New DataSet
Db_Connect()
DataAdapter.Fill(Buildings)
DataAdapter.Dispose()            
Buildings_List.DataSource = Buildings.Tables(0)           
Buildings_List.ValueMember = "B_ID"
Buildings_List.DisplayMember = "B_Name"
Dim SqlCmd As String = ""
SqlCmd = "SELECT * FROM Floors Where [B_ID]=" & CInt(Buildings_List.SelectedValue)
I though I am doing it right. The Value member should be fixed (by field name from the table)
 
Here is my code:
VB.NET:
Dim SqlCmd As String = "SELECT * FROM Buildings"
Dim DataAdapter As New OleDb.OleDbDataAdapter(SqlCmd, sqlconn)
Dim Buildings As New DataSet
Db_Connect()
DataAdapter.Fill(Buildings)
DataAdapter.Dispose()            
Buildings_List.DataSource = Buildings.Tables(0)           
Buildings_List.ValueMember = "B_ID"
Buildings_List.DisplayMember = "B_Name"
Dim SqlCmd As String = ""
SqlCmd = "SELECT * FROM Floors Where [B_ID]=" & CInt(Buildings_List.SelectedValue)
I though I am doing it right. The Value member should be fixed (by field name from the table)
I do wish that people could post clearly. That is obviously not the code that you're using because you have SqlCmd declared twice there, which would not compile. You also have a CInt there that you didn't have in the code you posted originally so you would no longer be getting the same error message even if the code did run.
 
I apologize for the confusing. I am working my way through it, and make change to it. Now I am getting different error message.
This part of the code is under the Load sub:
VB.NET:
Dim SqlCmd As String = "SELECT * FROM Buildings"
Dim DataAdapter As New OleDb.OleDbDataAdapter(SqlCmd, sqlconn)
Dim Buildings As New DataSet
Db_Connect()
DataAdapter.Fill(Buildings)
DataAdapter.Dispose()            
Buildings_List.DataSource = Buildings.Tables(0)           
Buildings_List.ValueMember = "B_ID"
Buildings_List.DisplayMember = "B_Name"
When the form load and combobox1 being filed, the other function run, which include:
VB.NET:
Dim SqlCmd As String = ""
SqlCmd = "SELECT * FROM Floors Where [B_ID]=" & CInt(Buildings_List.SelectedValue)
I tried Cstr and it didn't work, then I decide to use CInt as the value should be numbers, I am still getting the same error message, just it says:
conversion from type "DataRowView" to type 'Integer' is not valid
instead of:
conversion from type "DataRowView" to type 'String' is not valid

Thank you very much for your help

Also I want to point that currently, the column function very well, but the error message annoying.
 
Hi,

Surprisingly enough this is actually a common error. I Take it that the code which is trying to build your SqlCmd String variable is in the SelectedIndexChanged Event of a ListBox (or could be a ComboBox, it does not matter which)?

If so, then what is happening is that when you set the DataSource of the Control it will fire the SelectedIndexChanged Event due to the Data being added to the control. Now that?s fine but only if you have already set the DisplayMember and ValueMember Properties of the control. If you do not set these Properties first then it?s a DataRowView that is returned by the SelectedValue property.

Therefore, when populating controls through it's DataSource Property you must ALWAYS set the DsiplayMember and ValueMember properties before you set the DataSource property. i.e:-

With Buildings_List
  .DisplayMember = "B_Name"
  .ValueMember = "B_ID"
  .DataSource = Buildings.Tables(0)
End With


Hope that helps.

Cheers,

Ian
 
Hi,

Surprisingly enough this is actually a common error. I Take it that the code which is trying to build your SqlCmd String variable is in the SelectedIndexChanged Event of a ListBox (or could be a ComboBox, it does not matter which)?

If so, then what is happening is that when you set the DataSource of the Control it will fire the SelectedIndexChanged Event due to the Data being added to the control. Now that?s fine but only if you have already set the DisplayMember and ValueMember Properties of the control. If you do not set these Properties first then it?s a DataRowView that is returned by the SelectedValue property.

Therefore, when populating controls through it's DataSource Property you must ALWAYS set the DsiplayMember and ValueMember properties before you set the DataSource property. i.e:-

With Buildings_List
  .DisplayMember = "B_Name"
  .ValueMember = "B_ID"
  .DataSource = Buildings.Tables(0)
End With


Hope that helps.

Cheers,

Ian

Wow
just Wooooow

Thanks Ian, that works as magic. I envy your brilliant logic thinking.

Thank you all guys
 
Back
Top