Solved

"Cast from type 'DataRowView' to type 'Integer' is not valid" error

Posted on 2004-08-03
17
1,071 Views
Last Modified: 2012-06-21
I am trying to teach myself VB.Net database programming. I have a working VB.Net application that works with an Access database.

What I am doing is taking each line of code from the working program and putting it into a new project and analyzing what it is doing to try to understand it.

I copied and pasted this piece of code from the working program to the practice one:

>>>>>>>>>>>>>>>>>

Private Sub drpWorkStation_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
                                        System.EventArgs) Handles drpWorkStation.SelectedIndexChanged
        Dim WorkStationID As Int32
       
       WorkStationID = drpWorkStation.SelectedValue

       LoadSoftware(WorkStationID)
End Sub

>>>>>>>>>>>>>>>>>>

And I get this error in the practice program:

"Cast from type 'DataRowView' to type 'Integer' is not valid"


The code works fine in the working program but not in the practice program. I have beat myself on the head for half a day and can't figure it out.

Any suggestions.


0
Comment
Question by:awilson11
  • 7
  • 7
  • 3
17 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
do you have the code that initializes the drop down ? it looks like you are binding and are ending up with a datarowview in your selected value.
0
 
LVL 7

Expert Comment

by:natloz
Comment Utility
Try:

If IsNumeric(CInt(drpWorkStation.SelectedValue)) then
   WorkStationID = CInt(drpWorkStation.SelectedValue)
   LoadSoftware(WorkStationID)
else
  msgBox(CStr(drpWorkStation.SelectedValue))
endif
0
 

Author Comment

by:awilson11
Comment Utility
gregoryyoung:

Here is the code that initialied the workstation combo:


Public Sub LoadWorkStations()
        Try
           
            Dim Conn As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
            Conn.Open()
           
            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select WorkStationID As
            ID,WorkStation As Name,UserID from [WorkStation] Order by WorkStation", Conn)
             
            da.Fill(ds, "WorkStation")

            drpWorkStation.DisplayMember = "Name"
            drpWorkStation.ValueMember = "ID"
            drpWorkStation.DataSource = ds.Tables("WorkStation")

            If drpWorkStation.Items.Count > 0 Then
                drpWorkStation.SelectedIndex = 0
            End If
           
            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("Error Occurred : " & ex.Message)
        End Try
    End Sub



But what I don't get is that the exact same code (copied and pasted) works in one project but not the other.



natloz:

I will first try to solve the problem and if that doesn't work I will try your workaround.

0
 
LVL 7

Expert Comment

by:natloz
Comment Utility
My code is not for a workaround, but rather a test to see what the value is of the combobox if it does not come through as a number.
0
 

Author Comment

by:awilson11
Comment Utility
natloz:

I tried your code and on this line:

             If IsNumeric(CInt(cboWorkstation.SelectedValue))

I got the exact same original error:

            "Cast from type 'DataRowView' to type 'Integer' is not valid"
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
well the code above should be working ...

you are setting the value member to the ID column which should be an integer ... is there any other code within the context which could be causing an issue ?

yeah "If IsNumeric(CInt(cboWorkstation.SelectedValue))" this will fail on a dataviewrow ... What I am finding intresting is that there is a DataViewRow in the item ... you don't get these from Tables you get them from DataViews .. is it possible that this combo is being alterred somewhere else ?
0
 
LVL 7

Expert Comment

by:natloz
Comment Utility
Where do you define: ds
and can you post the defninition...
0
 

Author Comment

by:awilson11
Comment Utility
natloz

ds is defined here:

Public Class SoftwareInventoryForm
    Inherits System.Windows.Forms.Form
    'Connection String to Connect to MS Access.
    Public Const CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0; Data
                                                                    Source=Software Inventory_be.mdb;"
    Private ds As DataSet



gregoryyoung


I know it works in the original program, but not when I copy it over to the practice program, here is some more of the code. Here is the only code I can find concerning DataRow:

>>>>>>>>>>>>>>>

Private Sub BindSoftwareDetailData()
        If ds.Tables("SoftwareDetail").Rows.Count > 0 Then
            Dim dr As DataRow = ds.Tables("SoftwareDetail").Rows(0)

            txtCost.Text = dr("Cost")
            txtPurchaseFrom.Text = FixString(dr("PurchasedFrom"))
            txtSerialNum1.Text = FixString(dr("SerialNum1"))
            txtSerialNum2.Text = FixString(dr("SerialNum2"))
            drpMedia.SelectedValue = FixString(dr("Media"))
            drpStorage.SelectedValue = FixString(dr("Storage"))
            drpUser.SelectedValue = dr("InstallerID")
            dtDateInstalled.Value = dr("DateInstalled")
            dtPurchase.Value = dr("DatePurchased")
            txtVersion.Text = FixString(dr("Version"))
            txtNotes.Text = FixString(dr("Note"))
            lblRecord.Text = (drpSoftware.SelectedIndex + 1) & " of " & drpSoftware.Items.Count
        End If
    End Sub


Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        If drpSoftware.Items.Count > 0 Then
            drpSoftware.SelectedIndex = 0
        End If
    End Sub

>>>>>>>>>>>>>>>>>


If you could tell me what I need to look for to find a DataRowView I could search the code.

Also would there be some setting in Visual Studio that would change by switching from the working project to the non-working practice project?


0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
can you just post the full form code of your non-working practice project ?
0
 

Author Comment

by:awilson11
Comment Utility
Here is all the code from the practice project, about half the code for the working project:

Imports System.Data.OleDb
Public Class frmPractice
    Inherits System.Windows.Forms.Form
    Public Const CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Software Inventory_be.mdb;"
    Private ds As DataSet


Public Sub LoadWorkstation()
        Try

            Dim Conn As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
            Conn.Open()

            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select WorkStationID As
            ID,WorkStation As Name,UserID from [WorkStation] Order by WorkStation", Conn)

            da.Fill(ds, "WorkStation")

            cboWorkstation.DataSource = ds.Tables("WorkStation")             cboWorkstation.DisplayMember = "Name"                            cboWorkstation.ValueMember = "ID

            If cboWorkstation.Items.Count > 0 Then
                cboWorkstation.SelectedIndex = 0
            End If

            Conn.Close()

        Catch ex As Exception
            MessageBox.Show("Error Occurred : " & ex.Message)
        End Try

    End Sub
   


    Private Sub frmPractice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ds = New DataSet
        LoadWorkstation()
        LoadMedia()
        LoadStorage()
        LoadUser()

    End Sub
    Public Sub LoadMedia()
        Try

            Dim Conn As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
            Conn.Open()

            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Media Order by Media", Conn)

            da.Fill(ds, "Media")

            cboMedia.DisplayMember = "Media"
            cboMedia.ValueMember = "MediaID"
            cboMedia.DataSource = ds.Tables("Media")


            If cboMedia.Items.Count > 0 Then
                cboMedia.SelectedIndex = 0
            End If

            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("Error Occurred : " & ex.Message)
        End Try

    End Sub

    Public Sub LoadStorage()
        Try

            Dim Conn As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
            Conn.Open()

            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Storage Order by Storage", Conn)

            da.Fill(ds, "Storage")


            cboStorage.DisplayMember = "Storage"
            cboStorage.ValueMember = "StorageID"
            cboStorage.DataSource = ds.Tables("Storage")

            If cboStorage.Items.Count > 0 Then
                cboStorage.SelectedIndex = 0
            End If


            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("Error Occurred : " & ex.Message)
        End Try

    End Sub
    Public Sub LoadUser()
        Try
            Dim Conn As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
            Conn.Open()

            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from [User] Order by [User]", Conn)

            da.Fill(ds, "User")

            cboUser.DisplayMember = "User"
            cboUser.ValueMember = "UserID"
            cboUser.DataSource = ds.Tables("User")

            If cboUser.Items.Count > 0 Then
                cboUser.SelectedIndex = 0
            End If

            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("Error Occurred : " & ex.Message)
        End Try

    End Sub


    Public Sub LoadSoftware(ByVal WorkStationID As Int32)
        Try
            Dim StrQuery As String = "Select * from Software Where SoftwareID In(Select SoftwareID From SoftwareDetail Where WorkStationID=" & WorkStationID & ") Order by Software"

            Dim Conn As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
            Conn.Open()


            Dim da As OleDbDataAdapter = New OleDbDataAdapter(StrQuery, Conn)

            'If the table "software" in the dataset has data in it
            'then clear it

            If Not IsNothing(ds.Tables("Software")) Then
                ds.Tables("Software").Rows.Clear()
            End If

            da.Fill(ds, "Software")

            cboSoftware.DisplayMember = "Software"
            cboSoftware.ValueMember = "SoftwareID"
            cboSoftware.DataSource = ds.Tables("Software")


            If cboSoftware.Items.Count > 0 Then
                cboSoftware.SelectedIndex = 0
                LoadSoftwareDetail(cboWorkstation.SelectedValue, cboSoftware.SelectedValue)
            Else
                lblRecord.Text = "0 of 0"
            End If

            'Always Close the Connection
            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("Error Occurred : " & ex.Message)
        End Try

    End Sub

    Private Sub LoadSoftwareDetail(ByVal WorkStationID As Int32, ByVal SoftwareID As Int32)
        Try
            Dim Conn As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
            Conn.Open()

            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from SoftwareDetail Where WorkStationID=" & WorkStationID & " AND SoftwareID=" & SoftwareID, Conn)


            If Not IsNothing(ds.Tables("SoftwareDetail")) Then
                ds.Tables("SoftwareDetail").Rows.Clear()
            End If

            da.Fill(ds, "SoftwareDetail")

            BindSoftwareDetailData()

            Conn.Close()
        Catch ex As Exception
            MessageBox.Show("Error Occurred : " & ex.Message)
        End Try
    End Sub


    Private Sub BindSoftwareDetailData()
        If ds.Tables("SoftwareDetail").Rows.Count > 0 Then

            Dim dr As DataRow = ds.Tables("SoftwareDetail").Rows(0)

            txtCost.Text = dr("Cost")
            txtPurchaseFrom.Text = FixString(dr("PurchasedFrom"))
            txtSerialNum1.Text = FixString(dr("SerialNum1"))
            txtSerialNum2.Text = FixString(dr("SerialNum2"))
            cboMedia.SelectedValue = FixString(dr("Media"))
            cboStorage.SelectedValue = FixString(dr("Storage"))
            cboUser.SelectedValue = dr("InstallerID")
            dtDateInstalled.Value = dr("DateInstalled")
            dtPurchase.Value = dr("DatePurchased")
            txtVersion.Text = FixString(dr("Version"))
            txtNotes.Text = FixString(dr("Note"))

           
            lblRecord.Text = (cboSoftware.SelectedIndex + 1) & " of " & cboSoftware.Items.Count
        End If
    End Sub



    Private Function FixString(ByVal ValToFix As Object) As String
        If IsDBNull(ValToFix) Then
            Return ""
        End If
        Return ValToFix
    End Function

    Private Sub cboWorkStation_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWorkstation.SelectedIndexChanged
        Dim WorkStationID As String

   
        WorkStationID = cboWorkstation.SelectedValue

        LoadSoftware(WorkStationID)
    End Sub



End Class
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
Comment Utility
cboWorkstation.DataSource = ds.Tables("WorkStation")             cboWorkstation.DisplayMember = "Name"                            cboWorkstation.ValueMember = "ID

is this the actual order here ?

sometimes databinding can get funny results if you do this ... (and its different from the other code you posted)
0
 

Author Comment

by:awilson11
Comment Utility
That fixed the problem.


I changed the sequence because I thought it was backward, and it seemed to have not effect on the program and didn't seem related to this problem.


But, here is a propositon to you, I will bump the points up to 500 if you can explain these three things:


1. If the sequence is wrong, can you explain why?

2. Why is it necessarry to use the FixString function in the BindSoftwareDetailData()
and what is it doing?


3. Can you explain what is hapening and why with this code from the LoadSoftwareDetail

 If Not IsNothing(ds.Tables("SoftwareDetail")) Then
                ds.Tables("SoftwareDetail").Rows.Clear()
End If

If you agree explain it as if explaining to somebody who is new to VB.Net, you won't offend my by being too simple as I don't understand what the logic is doing and why it is doing it.





0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
1. If the sequence is wrong, can you explain why?

well you are setting the ValueMember and DisplayMember (they are used in the databinding) when you call databind before setting these the databind will assume object based databinding (i.e. no value/display memeber)

2. Why is it necessarry to use the FixString function in the BindSoftwareDetailData()
and what is it doing?

Strigns that arecoming froma  database that allows nulls may be null ... a DBNull cannot be converted to a string, you have to check for it if you did not do that you would get an error when you first ran accross a null.

3. Can you explain what is hapening and why with this code from the LoadSoftwareDetail

 If Not IsNothing(ds.Tables("SoftwareDetail")) Then
                ds.Tables("SoftwareDetail").Rows.Clear()
End If

you are calling this function many times and it is using criteria in order to limit the number of rows it is pulling back. It is clearing the table before refilling it with the different criteria ..

example: the first time it is called with workstationid 10
the second time it is called with workstationid 15

the two result sets are different ... so it clears out the first result set before appending the second to the table. the isNothing test says that if the table doesnt exist, don't bother it will be creted later.
0
 

Author Comment

by:awilson11
Comment Utility
OK, that is great, you earned your points. Good work.
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
glad to help ... if you have any more questions, feel free. Learning .NET can be quite large undertaking.
0
 

Author Comment

by:awilson11
Comment Utility
That isn't the half of it. My comapny, with 6 IT staff, is in the beginning of a platform migration, from an ancient mini-computer using COBOL and RPG to Windows Sever 2003, SQL Server 2000 and VB.Net. In the next 9 months I will need to get up to speed on

Windows Server 2003 (I have MCSE in NT)
Active Directory
SQL Server 2000 ( I am studying for MCDBA)
Exchange 2003
ISA 2004
SharePoint Portal
Visual Basic .Net

The rest of the dept only have to learn VB.Net (but they will have to learn it much better than I as I will be doing a little of everything and they will concentrate on VB development.

Thanks for your help.
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
well if you need anything along the way feel free to ask. EE is a great resource for all of those items (including various integration topics).

Cheers,

Greg
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now