none
The conversion from string "Select" to type 'Integer' is not valid. RRS feed

  • Question

  • Hi, 

    I need your help,

    I get  "The conversion from string "Select" to type 'Integer' is not valid." error,

    When selecting the combobox items:

    Here is the code:

        Private Sub ComboTest_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboTest.SelectedIndexChanged
            If Not IsNothing(_bs1) Then
                _bs1.Position = ComboTest.SelectedIndex
            End If
            ShowRacesTest()
        End Sub
        Private Sub ShowRacesTest()
            If Not String.IsNullOrEmpty(ComboTest.Text) Then
                _bs2.DataSource = opsd.DataALLPontes(ComboTest.Text)
                Dgv.DataSource = _bs2
                Dgv.ExpandColumns()
            End If
        End Sub
    Thank you very Much and Best Regards.


    • Edited by Bajtitou Wednesday, March 27, 2019 9:49 AM
    Wednesday, March 27, 2019 9:39 AM

Answers

  • Hello,

    In my example I set ValueMember to the Id field of the underlying table so by casting SelectedValue from Object to Integer works as per my example.

    If you didn't set ValueMember then yes SelectedValue is a DataRowView.

    Revised code for extra lines I added

    Public Class Form1
        Private opsd As Operations = New Operations()
        Private _bs2 As BindingSource = New BindingSource()
        Private Sub Form1_Load(sender As Object, e As EventArgs) _
            Handles MyBase.Load
    
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            Dim dt = LoadMockedData()
            ComboBox1.DataSource = dt
            ComboBox1.DisplayMember = "Name"
            ComboBox1.ValueMember = "Id"
    
        End Sub
        Private Function LoadMockedData() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With
                              {.ColumnName = "Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With
                              {.ColumnName = "Name", .DataType = GetType(String)})
            ' the follow data would come from a database table
            dt.Rows.Add(New Object() {1, "Apple"})
            dt.Rows.Add(New Object() {2, "Orange"})
            dt.Rows.Add(New Object() {3, "Grape"})
    
            ' now we inject a fake row
            Dim datarow As DataRow
            datarow = dt.NewRow()
            datarow.SetField("Id", -1)
            datarow.SetField("Name", "Select")
            dt.Rows.InsertAt(datarow, 0)
    
            Return dt
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' get selected value which is the Id field
            Dim currentIdentifierFromSelectedValue As Integer = CInt(ComboBox1.SelectedValue)
            ' get the current DataRow
            Dim row = CType(ComboBox1.SelectedItem, DataRowView).Row
            ' get the current id, same as currentIdentifierFromSelectedValue value
            Dim currentRowFromSelectedItem As Integer = row.Field(Of Integer)("Id")
            Dim selectedIdentifier = CInt(ComboBox1.SelectedValue)
    
            If selectedIdentifier > -1 Then
                _bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
            Else
                MessageBox.Show("Please make a selection")
            End If
        End Sub
    End Class
    Public Class Operations
        Public Function DataALLPontes(sender As String) As DataTable
            Dim dt As New DataTable
            ' TODO
            Return dt
        End Function
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:57 AM
    Wednesday, March 27, 2019 1:33 PM
    Moderator
  • Yes, I simply took the code presented in my last reply, dropped it into Notepad++, tweak it as shown below. I use less lines as in my first reply I could had done the same but wanted to break things apart for learning.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	If CType(ComboBox1.SelectedItem, DataRowView).Row.Field(Of Integer)("Id") > -1 Then
    		_bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
    	Else
    		MessageBox.Show("Please make a selection")
    	End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:57 AM
    Wednesday, March 27, 2019 4:43 PM
    Moderator
  • Greetings,

    I'm going to change things just a little, instead of using a DataTable for the ComboBox I will use a list.

    The entire project using MS-Access is in a GitHub repository of mine. I used MS-Access because of peeking at one of your recent questions.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:56 AM
    Wednesday, March 27, 2019 11:03 PM
    Moderator
  • In this regards, remove Handles ComboTest.SelectedIndexChanged then as the last line in form shown, add the following

    AddHandler ComboTest.SelectedIndexChanged, AddressOf ComboTest_SelectedIndexChanged

    (I wrote the above directly in this reply so hopefully it is syntactically correct) 

    • Removal of the handles clause ensures the SelectedIndexChanged is not fired
    • Adding the event as the last line in form shown goes with the above  but now you are subscribed to the event.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:41 PM
    Thursday, March 28, 2019 2:32 PM
    Moderator

All replies

  • Hi,

    Is the argument to the opsd.DataALLPontes method an integer?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 27, 2019 9:50 AM
  • Maybe use this: ‘…opsd.DataALLPontes(ComboTest.SelectedValue)’.

    Wednesday, March 27, 2019 9:55 AM
  • Hi,

    Is the argument to the opsd.DataALLPontes method an integer?

    Best Regards,


    Hi , 

    This is the argument opsd.DataALLPontes method:

      Public Function DataALLPontes() As DataTable
    
            Dim selectStatement As String =
                  <SQL>
     SELECT         ...........
                    ...........
                    </SQL>.Value
    
    end function 

    Thank you very Much, best regards

    Wednesday, March 27, 2019 10:07 AM
  • Hello,

    Here is a mock up to see if this might help. I use a DataTable as the DataSource for the ComboBox but could be say a List(Of T).

    Public Class Form1
        Private opsd As Operations = New Operations()
        Private _bs2 As BindingSource = New BindingSource()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            Dim dt = LoadMockedData()
            ComboBox1.DataSource = dt
            ComboBox1.DisplayMember = "Name"
            ComboBox1.ValueMember = "Id"
        End Sub
        Private Function LoadMockedData() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Name", .DataType = GetType(String)})
            ' the follow data would come from a database table
            dt.Rows.Add(New Object() {1, "Apple"})
            dt.Rows.Add(New Object() {1, "Orange"})
            dt.Rows.Add(New Object() {1, "Grape"})
    
            ' now we inject a fake row
            Dim datarow As DataRow
            datarow = dt.NewRow()
            datarow.SetField("Id", -1)
            datarow.SetField("Name", "Select")
            dt.Rows.InsertAt(datarow, 0)
    
            Return dt
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim selectedIdentifier = CInt(ComboBox1.SelectedValue)
            If selectedIdentifier > -1 Then
                _bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
            Else
                MessageBox.Show("Please make a selection")
            End If
        End Sub
    End Class
    Public Class Operations
        Public Function DataALLPontes(sender As String) As DataTable
            Dim dt As New DataTable
            ' TODO
            Return dt
        End Function
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 27, 2019 10:28 AM
    Moderator
  •     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim selectedIdentifier = CInt(ComboBox1.SelectedValue)
            If selectedIdentifier > -1 Then
                _bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
            Else
                MessageBox.Show("Please make a selection")
            End If
        End Sub
    End Class
    


    Hi ,

    Thank you for you answer ,

    I tried to take advantage of your example and tested with this code:

      Private Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click
            Dim selectedIdentifier = CInt(ComboTest.SelectedValue)
            If selectedIdentifier > -1 Then
                _bs2.DataSource = ops.DataALLPontes(ComboTest.Text)
            Else
                MessageBox.Show("Please make a selection")
            End If
        End Sub

    But I get an other Error :" Conversion from 'DataRowView' to 'Integer' type is invalid."  at this line :

     Dim selectedIdentifier = CInt(ComboTest.SelectedValue)

    So what is wrong?

    Thank you very mutch.

    Best Regards.

    Wednesday, March 27, 2019 12:47 PM
  • Hello,

    In my example I set ValueMember to the Id field of the underlying table so by casting SelectedValue from Object to Integer works as per my example.

    If you didn't set ValueMember then yes SelectedValue is a DataRowView.

    Revised code for extra lines I added

    Public Class Form1
        Private opsd As Operations = New Operations()
        Private _bs2 As BindingSource = New BindingSource()
        Private Sub Form1_Load(sender As Object, e As EventArgs) _
            Handles MyBase.Load
    
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            Dim dt = LoadMockedData()
            ComboBox1.DataSource = dt
            ComboBox1.DisplayMember = "Name"
            ComboBox1.ValueMember = "Id"
    
        End Sub
        Private Function LoadMockedData() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With
                              {.ColumnName = "Id", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With
                              {.ColumnName = "Name", .DataType = GetType(String)})
            ' the follow data would come from a database table
            dt.Rows.Add(New Object() {1, "Apple"})
            dt.Rows.Add(New Object() {2, "Orange"})
            dt.Rows.Add(New Object() {3, "Grape"})
    
            ' now we inject a fake row
            Dim datarow As DataRow
            datarow = dt.NewRow()
            datarow.SetField("Id", -1)
            datarow.SetField("Name", "Select")
            dt.Rows.InsertAt(datarow, 0)
    
            Return dt
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' get selected value which is the Id field
            Dim currentIdentifierFromSelectedValue As Integer = CInt(ComboBox1.SelectedValue)
            ' get the current DataRow
            Dim row = CType(ComboBox1.SelectedItem, DataRowView).Row
            ' get the current id, same as currentIdentifierFromSelectedValue value
            Dim currentRowFromSelectedItem As Integer = row.Field(Of Integer)("Id")
            Dim selectedIdentifier = CInt(ComboBox1.SelectedValue)
    
            If selectedIdentifier > -1 Then
                _bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
            Else
                MessageBox.Show("Please make a selection")
            End If
        End Sub
    End Class
    Public Class Operations
        Public Function DataALLPontes(sender As String) As DataTable
            Dim dt As New DataTable
            ' TODO
            Return dt
        End Function
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:57 AM
    Wednesday, March 27, 2019 1:33 PM
    Moderator
  • Hello,

    In my example I set ValueMember to the Id field of the underlying table so by casting SelectedValue from Object to Integer works as per my example.

    If you didn't set ValueMember then yes SelectedValue is a DataRowView.

    Hi Karen,

    Thank you very Much for your help and assistance ,

    Is it possible to make the code work without setting the ValueMember? If yes , how to cast it?

    Thank you very very Much.


    • Edited by Bajtitou Wednesday, March 27, 2019 5:41 PM
    Wednesday, March 27, 2019 4:13 PM
  • Yes, I simply took the code presented in my last reply, dropped it into Notepad++, tweak it as shown below. I use less lines as in my first reply I could had done the same but wanted to break things apart for learning.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	If CType(ComboBox1.SelectedItem, DataRowView).Row.Field(Of Integer)("Id") > -1 Then
    		_bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
    	Else
    		MessageBox.Show("Please make a selection")
    	End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:57 AM
    Wednesday, March 27, 2019 4:43 PM
    Moderator

  • Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	If CType(ComboBox1.SelectedItem, DataRowView).Row.Field(Of Integer)("Id") > -1 Then
    		_bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
    	Else
    		MessageBox.Show("Please make a selection")
    	End If
    End Sub

    Hi Karen,

    I did some change and I take the last code for Button1 , there is no error No Exception ,

    But How to Filter the DatagridView?


    Public Class Form1
       Private opsd As Operations = New Operations()
        Private _bs1 As BindingSource = New BindingSource()
        Private _bs2 As BindingSource = New BindingSource()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
           
            Dim dt = LoadMockedData()
            ComboBox1.DisplayMember = "Race"
            ComboBox1.ValueMember = "CID"
            ComboBox1.DataSource = dt
    
        End Sub
        Private Function LoadMockedData() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn() With {.ColumnName = "CID", .DataType = GetType(Integer)})
            dt.Columns.Add(New DataColumn() With {.ColumnName = "Race", .DataType = GetType(String)})
            ' the follow data would come from a database table
            dt.Rows.Add(New Object() {1, "Glosters"})
            dt.Rows.Add(New Object() {1, "Ordinaire"})
            dt.Rows.Add(New Object() {1, "Malinois"})
            dt.Rows.Add(New Object() {1, "Chardonneret"})
            dt.Rows.Add(New Object() {1, "Agate"})
    
    
            ' now we inject a fake row
            Dim datarow As DataRow
            datarow = dt.NewRow()
            datarow.SetField("CID", -1)
            datarow.SetField("Race", "Select")
            dt.Rows.InsertAt(datarow, 0)
    
            Return dt
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If CType(ComboBox1.SelectedItem, DataRowView).Row.Field(Of Integer)("CID") > -1 Then
                _bs2.DataSource = opsd.DataALLPontes(ComboBox1.Text)
              
            Else
                MessageBox.Show("Please make a selection")
            End If
        End Sub
    End Class

    So Thank you very Much.

    Best Regards.

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:57 AM
    • Unmarked as answer by Bajtitou Thursday, March 28, 2019 9:57 AM
    Wednesday, March 27, 2019 9:34 PM
  • Greetings,

    I'm going to change things just a little, instead of using a DataTable for the ComboBox I will use a list.

    The entire project using MS-Access is in a GitHub repository of mine. I used MS-Access because of peeking at one of your recent questions.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:56 AM
    Wednesday, March 27, 2019 11:03 PM
    Moderator
  • Greetings,

    I'm going to change things just a little, instead of using a DataTable for the ComboBox I will use a list.

    The entire project using MS-Access is in a GitHub repository of mine. I used MS-Access because of peeking at one of your recent questions.

    Hi Karen ,

    Is It possible To Filter With "SelectedIndexCanged " Event  without this

    cast

    Message getting up before the form shows?

      Private Sub ComboTest_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboTest.SelectedIndexChanged
    
            Dim Races As LesRaces = CType(ComboTest.SelectedItem, LesRaces)
    
            If Races.RaceID > -1 Then
                    bsRace.DataSource = ops.AllData(Races.RaceID)
                    For Each column As DataGridViewColumn In Dgv.Columns
                        column.HeaderText = column.HeaderText.SplitCamelCase()
                    Next
                    Dgv.ExpandColumns()
                Else
                    MessageBox.Show("Please select a Race")
                End If
    
        End Sub
    So Thank you Very Much and Best regards.

    Bajtitou.

    Thursday, March 28, 2019 2:04 PM
  • In this regards, remove Handles ComboTest.SelectedIndexChanged then as the last line in form shown, add the following

    AddHandler ComboTest.SelectedIndexChanged, AddressOf ComboTest_SelectedIndexChanged

    (I wrote the above directly in this reply so hopefully it is syntactically correct) 

    • Removal of the handles clause ensures the SelectedIndexChanged is not fired
    • Adding the event as the last line in form shown goes with the above  but now you are subscribed to the event.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Bajtitou Thursday, March 28, 2019 9:41 PM
    Thursday, March 28, 2019 2:32 PM
    Moderator
  • In this regards, remove Handles ComboTest.SelectedIndexChanged then as the last line in form shown, add the following

    AddHandler ComboTest.SelectedIndexChanged, AddressOf ComboTest_SelectedIndexChanged

    (I wrote the above directly in this reply so hopefully it is syntactically correct) 

    • Removal of the handles clause ensures the SelectedIndexChanged is not fired
    • Adding the event as the last line in form shown goes with the above  but now you are subscribed to the event.

    Hi Karen,

    Your sugest to addHandler /RemveHandler Drive To

    Solve the Question By Using "SelectionChangeCommitted event." . Is it better ?

    Private Sub ComboTest_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboTest.SelectionChangeCommitted 'Todo...

    End Sub

    So Thank you very Much for Your Assistance and Best Regards.



    • Edited by Bajtitou Thursday, March 28, 2019 9:54 PM
    Thursday, March 28, 2019 9:49 PM
  • Using the Handle clause is preferred until there are situations such as this one. 

    VB.NET is fortunate to allows both handles and AddHandler while C# uses the following syntax

    ComboBoxTest.SelectionChangeCommitted += ....

    There is no Handles clause, instead EventHandler.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, March 28, 2019 10:14 PM
    Moderator
  • Using the Handle clause is preferred until there are situations such as this one. 

    VB.NET is fortunate to allows both handles and AddHandler while C# uses the following syntax

    ComboBoxTest.SelectionChangeCommitted += ....

    There is no Handles clause, instead EventHandler.


    Hi, 

    Thank you very Much..

    Good Night.

    Thursday, March 28, 2019 10:28 PM