Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String being converted to Integer, causing System.InvalidCastException

Status
Not open for further replies.

joshbula

Technical User
Nov 19, 2004
45
US
Hello,
I am adapting this tutorial to my needs:

And I am receiving this error when it tries to take the Excel spreadsheet data and insert it into the SQL table:


System.InvalidCastException: Conversion from string "Percussion" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) --- End of inner exception stack trace --- at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) at Intra_Directors_ImportStudents.ImportIntoStudents(String FirstName, String LastName, Int32 GradYear, String ConcertInstrument, String MarchingInstrument, String JazzInstrument, String FSMASchoolID) in c:\inetpub\ 166


I don't understand why it is converting "Percussion" (which is the ConcertInstrument field) from a String to an Integer, because it is supposed to be a String.

Here is the vb code that calls the Insert function from my DataSet:
Code:
            ' if Student ID is still 0, then the student doesn't already exist, so insert it into the table
            If StudentID = 0 Then
                ' retrieve the identity key StudentID from the InsertStudentQuery Function:
                StudentID = Convert.ToInt32(SSAdapter.InsertStudentQuery(FirstName, LastName, GradYear, ConcertInstrument, MarchingInstrument, JazzInstrument, FSMASchoolID))
                LabelImport.Text = LabelImport.Text & _
                    "<font color=green>Member Imported: " & _
                    " ID: " & StudentID & " " & FirstName & " " & _
                    LastName & " ConcertInstrument: " & ConcertInstrument & ".</font><br>"
            End If
            Return StudentID
        Catch ex As Exception
            LabelImport.Text = LabelImport.Text & "<font color=red>" & _
            ex.ToString & "</font><br>"
            Return 0
        End Try


Here's the entire function... I've tried changing the Return Type to string, but that didn't work either.
Code:
  Protected Function ImportIntoStudents(ByVal FirstName As String, _
    ByVal LastName As String, ByVal GradYear As Integer, _
    ByVal ConcertInstrument As String, ByVal MarchingInstrument As String, ByVal JazzInstrument As String, _
    ByVal FSMASchoolID As String) As Integer
        ' make sure values don't exceed column limits
        FirstName = Left(LastName, 50)
        LastName = Left(FirstName, 50)
        GradYear = Left(GradYear, 4)
        ConcertInstrument = Left(ConcertInstrument, 10)
        MarchingInstrument = Left(MarchingInstrument, 10)
        JazzInstrument = Left(JazzInstrument, 10)
        FSMASchoolID = Left(FSMASchoolID, 10)

        Dim StudentID As Integer = 0
        Try
            Dim SSAdapter As New StudentImportDataSetTableAdapters.tblStudentsTableAdapter
            Dim SSDataTable As StudentImportDataSet.tblStudentsDataTable = Nothing
            SSDataTable = SSAdapter.GetStudentsByFullNameSchool(FirstName, LastName, FSMASchoolID)
            ' see if the Student already exists in the table, if not insert it
            If Not SSDataTable Is Nothing Then
                If SSDataTable.Rows.Count > 0 Then
                    If Not SSDataTable(0).StudentID = Nothing Then
                        StudentID = SSDataTable(0).StudentID
                        LabelImport.Text = LabelImport.Text & _
                            "<font color=blue>Member Found, Not Imported: " & _
                            " ID: " & StudentID & " " & FirstName & " " & LastName & ".</font><br/>"
                    End If
                End If
            End If

            ' if Student ID is still 0, then the student doesn't already exist, so insert it into the table
            If StudentID = 0 Then
                ' retrieve the identity key StudentID from the InsertStudentQuery Function:
                StudentID = Convert.ToInt32(SSAdapter.InsertStudentQuery(FirstName, LastName, GradYear, ConcertInstrument, MarchingInstrument, JazzInstrument, FSMASchoolID))
                LabelImport.Text = LabelImport.Text & _
                    "<font color=green>Member Imported: " & _
                    " ID: " & StudentID & " " & FirstName & " " & _
                    LastName & " ConcertInstrument: " & ConcertInstrument & ".</font><br>"
            End If
            Return StudentID
        Catch ex As Exception
            LabelImport.Text = LabelImport.Text & "<font color=red>" & _
            ex.ToString & "</font><br>"
            Return 0
        End Try


    End Function

Any help would be greatly appreciated.
Thanks!
...Josh
 
Maybe your copy of the database is different than the one which is used in the tutorial. That's the only thing I can think of. You can take off the Convert.ToInt32 part, but then you'll have to change other stuff since your StudentID is still an integer.

If this tutorial turns out to have too many errors to continue, you might try a Microsoft tutorial. See here:
 
The database is different, but I double-checked to make sure all the data types were correct. Also, she used two tables and I only need to use one, so I took out the function that added to her second table. I am able to successfully add data to the table with a ListView with no problems, so I think my database is fine. I'm just trying to figure out why it's trying to convert Strings to Integers.

Thanks for the link, I'll look for something there that might help.
...Josh
 

Can you post the code for the SSAdapter.InsertStudentQuery function?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
That was the problem... the data types weren't correct in the DataSet. I didn't event think of that because the listview was working, but the listview was using an asp data source instead of the DataSet.

Thanks!
...Josh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top