Question Conversion from string "System.Data.DataRowView" to type 'Integer' is not valid

daniness

Well-known member
Joined
Feb 12, 2010
Messages
49
Programming Experience
Beginner
Conversion from string "System.Data.DataRowView" to type 'Integer' is not valid

Hello All,

Could someone please help me with this? I have 2 forms, frm1 and frm2. frm1 has a combobox, cboLocations and based on what selection is made here, frm2 is loaded and its combobox, cboOffices is populated. I currently have the following code on frm1:

VB.NET:
Dim frm2 as New form(cboLocations.SelectedValue.ToString)
frm2.ShowDialog()

When running, I'm getting an InvalidCastException was unhandled...Conversion from string "System.DataDataRowView" to type 'Integer' is not valid." error. Could someone please advise? :(
 
Is this a numeric value you are trying to retrieve and pass?

VB.NET:
CInt(cboLocations.SelectedValue)

Also your form declaration isnt using any specific form names. Since your passing a parameter to a form I assume you want to call the specific form that you coded to accept this parameter.
 
You're declaring a new form (of just System.Windows.Forms.Form) which doesn't have an overloaded constructor to receive any parameters which should cause a build error in this case. So obviously there's more to it than what you've posted.

Now to the issue you've posted about, conversion from DataRowView to Integer means you're probably wanting to convert a cell of a row in the DataGridView to an integer, DGV's have a Rows collection and the rows have columns. The rows are indexed and the you can get the column either by index or by name.
 
Hi Tom, JuggaloBrotha, and All:

Tom, the CInt(cboLocations.SelectedValue), is indeed a numeric value that I'm trying to pass. Not sure if it makes a difference, but it's actually a foreign key.

JuggaloBrotha, I'm actually using a combobox, instead of a data gridview.

Also, I just tried to make my code generic but here it is with the form name. Please substitute cboOffices with cboDepot, frm1 with frmEditDel, and frm2 with frmLocation:

VB.NET:
Private Sub btnEditDelOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditDelOK.Click

        'Pass SelectedValue which is actually the ValueMember
        'but make sure user has made a selection
        If Me.cboSite.SelectedIndex = -1 Then
            MessageBox.Show("No Location Selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'Exit here since there is no location selection
            Exit Sub
        End If
        'Open frmLocation
        Dim frmLocation As New form(cboSite.SelectedValue.ToString)
        frmLocation.ShowDialog()
End Sub

Here is the code for frmLocation:
VB.NET:
Public Class form
    Private _LocationID As Integer
    Private FormIsLoading As Boolean

    Private Sub frmLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Private Sub frmLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Dos_trackDataSet.depot' table. You can move, or remove it, as needed.
        Me.DispatchTableAdapter.Fill(Me.Dos_trackDataSet.dispatch)
        'TODO: This line of code loads data into the 'Dos_trackDataSet.freight' table. You can move, or remove it, as needed.
        Me.FreightTableAdapter.FillFreightInfo(Me.Dos_trackDataSet.freight)
        'TODO: This line of code loads data into the 'Dos_trackDataSet.depot' table. You can move, or remove it, as needed.
            Me.LocationsTableAdapter.FillbySiteName(Me.Dos_trackDataSet.locations)
       
        Me.txtLocation.DataBindings.Add("text", frmEditDel.LocationsBindingSource, "site")
        Me.txtAAC.DataBindings.Add("text", frmEditDel.LocationsBindingSource, "aac")
        Me.txtPhone.DataBindings.Add("text", frmEditDel.LocationsBindingSource, "telephone_nbr")
        Me.txtFax.DataBindings.Add("text", frmEditDel.LocationsBindingSource, "fax_nbr")
        Me.cboFreight.DataBindings.Add("text", frmEditDel.LocationsBindingSource, "freight_refnbr")
        Me.cboDispatcher.DataBindings.Add("text", frmEditDel.LocationsBindingSource, "dispatch_refnbr")
        Me.txtEmail.DataBindings.Add("text", frmEditDel.LocationsBindingSource, "Email")

 Me.cboDepot.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.LocationsBindingSource, "depot_refnbr", True))


        ''TODO: This line of code loads data into the 'Dos_trackDataSet.freight' table. You can move, or remove it, as needed.
        Me.FreightTableAdapter.FillFreightInfo(Me.Dos_trackDataSet.freight)
        'Me.FreightTableAdapter.FillFreightInfo(frmEditDel.Dos_trackDataSet.freight)


        'TODO: This line of code loads data into the 'Dos_trackDataSet.dispatch' table. You can move, or remove it, as needed.
        Me.DispatchTableAdapter.Fill(Me.Dos_trackDataSet.dispatch)


        'Find out if the form is loading, if yes, ignore "SelectedIndexChanged"
        'event of cboDepot
        FormIsLoading = True

        'TODO: This line of code loads data into the 'Dos_trackDataSet.locations' table. You can move, or remove it, as needed.
        Me.DepotTableAdapter.FillByMatchDepotRefNbr(Me.Dos_trackDataSet.depot)

        'Set combobox to datasources
        Me.cboDepot.ValueMember = "depot_refnbr"
        Me.cboDepot.DisplayMember = "depot_name"
        Me.cboDepot.DataSource = Me.DepotBindingSource

        'Get all locations
        Me.LocationsTableAdapter.FillbySiteName(Dos_trackDataSet.locations)

        'Bind locations to combobox's "SelectedValue"
        Me.cboDepot.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.LocationsBindingSource, "depot_refnbr", True))


        'Find the Location from the previous form
        Dim Position As Integer = Me.LocationsBindingSource.Find("site_refnbr", _LocationID)
        If Position <> -1 Then
            Me.LocationsBindingSource.Position = Position
        End If

        'Allow the combobox to do its work again
        FormIsLoading = False
    End Sub

'Constructor you will see in frmEditDel we will call frmLocation with a parameter 
    Public Sub New(ByVal site_refnbr As Integer)
        _LocationID = site_refnbr
        'Call required by Windows Form Designer
        InitializeComponent()

        Me.cboDepot.Text = CStr(site_refnbr)
    End Sub

 Private Sub cboDepot_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDepot.SelectedIndexChanged
        'If the form is not loading and the user changes the depot, we want to save it
        If FormIsLoading = False Then
            Try
                'First validate all controls

                Me.Validate()
                'Tell bindingsource to go to end edit mode
                Me.LocationsBindingSource.EndEdit()
                'Update the locations
                Me.LocationsTableAdapter.FillByUpdate(Me.Dos_trackDataSet.locations)


            Catch ex As Exception
                MsgBox("Update failed")
            End Try

        End If
    End Sub

All your assistance is appreciated very much. Sorry for the long post.
 
Where are you binding cboSite? It's cboSite that you are incorrectly getting the data from so we need to see how that's bound. I would guess that your incorrectly, or not at all, setting the ValueMember.
 
Hi jm,

I have cboSite bound as follows on the presentation layer:

Data Source: LocationsBindingSource
Display Member: site
Value Member: site_refnbr
Selected Value: none

In the code behind I have it set as:

Me.LocationsTableAdapter.FillbySiteName(Me.Dos_trackDataSet.locations)

Me.cboSite.DataSource = Me.LocationsBindingSource
Me.cboSite.DisplayMember = "site"
Me.cboSite.ValueMember = "site_refnbr"
Me.cboSite.SelectedValue = "site_refnbr"

I also tried changing the Selected Value to site_refnbr on the presentation layer, but when debugging, it just hung after the button click event. Any thoughts, please?
 
Back
Top