最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vb.net - conversion from type datarow to type string is invalid - Stack Overflow

programmeradmin0浏览0评论

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
  • This 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 the concat column, remove the For Each loop and set the Expression property. i.e. backshells_dt.Columns.Add(New DataColumn("concat", GetType(String), "PartNumber, PartDescription"). Also 2, you should set Option Strict On. Visit the docs and read about this option to understand why you should. – dr.null Commented Mar 28 at 22:37
  • Thank you for your help but the things you mention are NOT causing any issues (option strict is on) – Perry 59 Commented Mar 31 at 19:47
  • Seriously? If the option was on this item("concat") = item("PartNumber") & ", " & item("PartDescription") does not compile. Read my first comment again and try to digest it. Especially the Option Strict part which disabling it IS causing that silly problem. No? Also 3, do not concatenate strings like that, Use String.Concat or interpolated strings. Also 4, you should dispose of OleDbDataAdapter. Good day. – dr.null Commented Apr 1 at 7:39
Add a comment  | 

1 Answer 1

Reset to default 0

the 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

发布评论

评论列表(0)

  1. 暂无评论