I am using a datatable to fill a combobox and am getting the error about "conversion from type datarow to type string is invalid". What is really confusing is I am using this same exact code in a different program with no errors, but in this program I'm getting the error. Despite the error the combobox is filling and displaying as it should. There must be something else triggering this error but what? The lines giving the error are commented in the code below
Using con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & VE_docPath & VE_currentProject & ".accdb;Persist Security Info=True")
Using cmd As New OleDb.OleDbCommand("", con)
Try
'cmd.CommandText = "(SELECT * FROM PartDefinitions WHERE PartClass = 3)"
cmd.CommandText = "(SELECT PartDefID, PartClass, PartNumber, PartDescription FROM PartDefinitions WHERE PartClass = 3)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
Dim daGet As New OleDbDataAdapter()
Dim backshells_dt As New DataTable
Dim backshell_index As Integer = 0
Dim Backshells_DataRow As DataRow
daGet.SelectCommand = cmd
daGet.Fill(backshells_dt)
'Concatenate the part number and description columns and display that so the user does not have
'to figure out what just the part number is
backshells_dt.Columns.Add("concat")
For Each item As DataRow In backshells_dt.Rows
item("concat") = item("PartNumber") & ", " & item("PartDescription")
Next
'add a "None" row so we can make it the selected item
Backshells_DataRow = backshells_dt.NewRow()
Backshells_DataRow("PartDefId") = "0"
Backshells_DataRow("PartClass") = "3"
Backshells_DataRow("PartNumber") = "None"
Backshells_DataRow("concat") = "None"
backshells_dt.Rows.Add(Backshells_DataRow)
If backshells_dt.Rows.Count > 0 Then
Me.BackshellComboBox.DataSource = backshells_dt 'why am I getting datarows to string error here?
Me.BackshellComboBox.DisplayMember = "concat" 'why am I getting datarows to string error here?
Me.BackshellComboBox.ValueMember = "concat" 'why am I getting datarows to string error here?
backshell_index = Me.BackshellComboBox.FindStringExact("None")
Me.BackshellComboBox.SelectedIndex = backshell_index
End If
Catch SqlError As System.Data.SqlTypes.SqlTypeException
MsgBox(SqlError.Message, MsgBoxStyle.SystemModal, "Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.SystemModal, "Error")
End Try
end using
end using
I didn't try anything else, I don't know what to try. It has no errors in one program but not the other
I am using a datatable to fill a combobox and am getting the error about "conversion from type datarow to type string is invalid". What is really confusing is I am using this same exact code in a different program with no errors, but in this program I'm getting the error. Despite the error the combobox is filling and displaying as it should. There must be something else triggering this error but what? The lines giving the error are commented in the code below
Using con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & VE_docPath & VE_currentProject & ".accdb;Persist Security Info=True")
Using cmd As New OleDb.OleDbCommand("", con)
Try
'cmd.CommandText = "(SELECT * FROM PartDefinitions WHERE PartClass = 3)"
cmd.CommandText = "(SELECT PartDefID, PartClass, PartNumber, PartDescription FROM PartDefinitions WHERE PartClass = 3)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
Dim daGet As New OleDbDataAdapter()
Dim backshells_dt As New DataTable
Dim backshell_index As Integer = 0
Dim Backshells_DataRow As DataRow
daGet.SelectCommand = cmd
daGet.Fill(backshells_dt)
'Concatenate the part number and description columns and display that so the user does not have
'to figure out what just the part number is
backshells_dt.Columns.Add("concat")
For Each item As DataRow In backshells_dt.Rows
item("concat") = item("PartNumber") & ", " & item("PartDescription")
Next
'add a "None" row so we can make it the selected item
Backshells_DataRow = backshells_dt.NewRow()
Backshells_DataRow("PartDefId") = "0"
Backshells_DataRow("PartClass") = "3"
Backshells_DataRow("PartNumber") = "None"
Backshells_DataRow("concat") = "None"
backshells_dt.Rows.Add(Backshells_DataRow)
If backshells_dt.Rows.Count > 0 Then
Me.BackshellComboBox.DataSource = backshells_dt 'why am I getting datarows to string error here?
Me.BackshellComboBox.DisplayMember = "concat" 'why am I getting datarows to string error here?
Me.BackshellComboBox.ValueMember = "concat" 'why am I getting datarows to string error here?
backshell_index = Me.BackshellComboBox.FindStringExact("None")
Me.BackshellComboBox.SelectedIndex = backshell_index
End If
Catch SqlError As System.Data.SqlTypes.SqlTypeException
MsgBox(SqlError.Message, MsgBoxStyle.SystemModal, "Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.SystemModal, "Error")
End Try
end using
end using
I didn't try anything else, I don't know what to try. It has no errors in one program but not the other
Share Improve this question asked Mar 28 at 19:49 Perry 59Perry 59 456 bronze badges 3 |1 Answer
Reset to default 0the culprit seems to have been in my for each loop. Strange it didn't occur in the other programs (option strict) gave me the clue. Here is what it those lines look like now:
'Concatenate the part number and description columns and display that so the user does not have
'to figure out what just the part number is
shieldblocks_dt.Columns.Add("concat")
For Each item As DataRow In shieldblocks_dt.Rows
item("concat") = item("PartNumber").ToString & ", " & item("PartDescription").ToString
Next
I simply added the .ToString and all was well
Me.BackshellComboBox.ValueMember = "concat"
doesn't seem to be right. This should be the field name of the actual value and not the concatenated values. Also, regarding theconcat
column, remove theFor Each
loop and set theExpression
property. i.e.backshells_dt.Columns.Add(New DataColumn("concat", GetType(String), "PartNumber, PartDescription")
. Also 2, you should setOption Strict On
. Visit the docs and read about this option to understand why you should. – dr.null Commented Mar 28 at 22:37item("concat") = item("PartNumber") & ", " & item("PartDescription")
does not compile. Read my first comment again and try to digest it. Especially theOption Strict
part which disabling it IS causing that silly problem. No? Also 3, do not concatenate strings like that, UseString.Concat
or interpolated strings. Also 4, you should dispose ofOleDbDataAdapter
. Good day. – dr.null Commented Apr 1 at 7:39