Conversion from string "System.Data.DataRowView" to type Integ

InvalidCastException was unhandled

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 9974 Views - Last Post: 04 March 2010 - 09:59 AM Rate Topic: -----

#1 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Conversion from string "System.Data.DataRowView" to type Integ

Posted 01 March 2010 - 09:53 AM

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:

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? :unsure:

Is This A Good Question/Topic? 0
  • +

Replies To: Conversion from string "System.Data.DataRowView" to type Integ

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 01 March 2010 - 09:57 AM

You're going to have to show us the constructor for form, that seems to be where the exception is happening.
Was This Post Helpful? 0
  • +
  • -

#3 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 01 March 2010 - 10:30 AM

Hi PsychoCoder,

Thanks for your response. Here is the constructor on frm2:

Public Sub New(ByVal site_refnbr As Integer)
        _LocationID = site_refnbr
        'Call required by Windows Form Designer
        InitializeComponent()

        'Me.cboOffices.Text = CStr(site_refnbr)
    End Sub


Please let me know if this is what you're looking for.
Was This Post Helpful? 0
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 01 March 2010 - 10:57 PM

Hi Daniness,

I presume you have a datagridview on your second form, so can you post us the rest of Form2...
Was This Post Helpful? 0
  • +
  • -

#5 myork  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 129
  • Joined: 04-March 09

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 01 March 2010 - 11:14 PM

Your cboLocations.SelectedValue is a DataRowView and not the number that you expect. What are you setting the combobox's ValueMember to? That dictates what is returned by the SelectedValue property, and will be what is causing the problem if it is not an integer.

This post has been edited by myork: 01 March 2010 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 06:03 AM

View Postmotcom, on 01 March 2010 - 09:57 PM, said:

Hi Daniness,

I presume you have a datagridview on your second form, so can you post us the rest of Form2...


Hi Motcom. Here is the rest of my form2:

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
       '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)
       
        Me.LocationsTableAdapter.FillbySiteName(Me.Dos_trackDataSet.locations)
        'TODO: This line of code loads data into the 'Dos_trackDataSet.locations' 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)
     
        '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

'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 = site_refnbr.ToString
    End Sub


    Private Sub btnSaveClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveClose.Click
        Me.Close()
    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



Was This Post Helpful? 0
  • +
  • -

#7 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 06:14 AM

View Postmyork, on 01 March 2010 - 10:14 PM, said:

Your cboLocations.SelectedValue is a DataRowView and not the number that you expect. What are you setting the combobox's ValueMember to? That dictates what is returned by the SelectedValue property, and will be what is causing the problem if it is not an integer.


Hi myork,


I'm setting the combobox's ValueMember to site_refnbr (Location ID), which are showing as integers. I even tried changing the data type for the site_refnbr column of my locations table (in the dataset) from string to int32, but this made no difference. Any ideas? :/
Was This Post Helpful? 0
  • +
  • -

#8 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 06:54 AM

Hi,

Is your locations binding set to the following? (presumably from your previous post)

Me.LocationsTableAdapter.Fill(Me.DataSet1.Locations)
Me.cboLocations.DataSource = Me.LocationsBindingSource
Me.cboLocations.DisplayMember = "location_name"
Me.cboLocations.ValueMember = "location_refnbr"



and your constructor set to this

[code]
Dim MyOffices As New Form2(cboLocations.SelectedValue)
MyOffices.ShowDialog()
[/code
Was This Post Helpful? 0
  • +
  • -

#9 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 07:06 AM

Hi motcom,

I actually do have what you've suggested all on my form1:

Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration
Imports System.Configuration.ConfigurationSettings

Public Class frmEditDel

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    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)
        frmLocation.ShowDialog()

    End Sub

    Private Sub LocationsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.LocationsBindingSource.EndEdit()
    End Sub


    Private Sub frmEditDel_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.locations' table. You can move, or remove it, as needed.
        Me.LocationsTableAdapter.FillbySiteName(Me.Dos_trackDataSet.locations)

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


        Dim connectionString As String = "Data Source=F03D3S-DEV01; Initial Catalog=dos_track;" _
        & "Integrated Security=True"

        Dim connection As New SqlConnection()
        connection.ConnectionString = connectionString

        connection.Open()

        connection.Close()

    End Sub


Any thoughts, please?

This post has been edited by daniness: 02 March 2010 - 07:07 AM

Was This Post Helpful? 0
  • +
  • -

#10 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 07:19 AM

Hi,

Does the error happen when you try to open form2 or on another line?
Was This Post Helpful? 0
  • +
  • -

#11 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 07:42 AM

Hey Motcom,

The Invalid Cast Exception error is actually happening on the button click event, before form2 can even open. It's happening on the line, "Dim frmLocation As New form(cboSite.SelectedValue".
Was This Post Helpful? 0
  • +
  • -

#12 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 07:52 AM

Well somewhere it does not bind correctly to the comboboxes' value member.

Lets try something else, i suggest you do in on a new Form, as we do not want to mingle with the existing one yet..

So..

If you open the datasources window (Shift+Alt+D) you should see your dataset(s) there, expand the "Dos_trackDataSet" node, then if you click on the locations node, a little dropdown should appear to the ight hand side of it, click on it and, select "Combobox" and drag the "locations" to your form.

Now a few things will be created, a Combobox bound to the locaion, a dataset gets added, a table adapter (table adapter manager maybe too) and a binding source.

Now click check the "valuemember" and "displaymemeber" properties and confirm that they are correct, also check the "datasource" property of the combobox to see if it binds correctly to the binding source..

Then try and repeat the following code from your new form and see if that works...

 Dim frmLocation As New form(cboSite.SelectedValue)
 frmLocation.ShowDialog()



btw the way I showed you here is my prefered way of databinding, you can bind to almost any control like this. On existing controls, you can just drag the field (from the datasources window) onto the control and it should bind.
Was This Post Helpful? 0
  • +
  • -

#13 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 10:11 AM

Motcom,

I tried creating a new form and binding it as you suggested, but I'm still receiving the Invalid Cast Exception error, but this time the error reads, "Converstion from type 'DBNull' to type 'Integer' is not valid". :o
Was This Post Helpful? 0
  • +
  • -

#14 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 02 March 2010 - 11:23 PM

Hi,

What is your Value member bound to?

You can check for DB Null by using "IsDBNull()"

If IsDBNull(cboLocations.SelectedValue) = False Then

End If



But I don't understand why it should not work (what you have done) the code looks all ok..

Maybe you can zip your project, and database and attach it to this post so one can have a look as to whats wrong..

This post has been edited by motcom: 02 March 2010 - 11:25 PM

Was This Post Helpful? 0
  • +
  • -

#15 daniness  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-January 10

Re: Conversion from string "System.Data.DataRowView" to type Integ

Posted 03 March 2010 - 07:39 AM

Hi Motcom,

Now I'm back to the original conversion error. As you can tell, I'm a novice programmer and feel like pulling my hair out. I'm attaching my code thus far. Your assistance is really appreciated. Thanks.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2