weird asp.net problem

Associate
Joined
18 Oct 2002
Posts
972
Location
Derby
Im writing a script for my ASP.NET project and I have ran into quite a strange problem. Basically I want to be able to create a page that allows someone to add a student to the database. They enter in the following:

Student Number: (this is unique but not an autonumber)
Name:
Course:

The script then takes the student ID and compares it with the existing database and acts accordingly. This works fine for both scenarios.

I have also implemented some Required Field Validator controls on the textboxes so if they omit say the student number, they will be told accordingly.

My problem is that testing the software on my local machine works just as I want it however when I upload it to my webspace I get the following:

Code:
System.FormatException: Input string was not in a correct format.

Also say if I enter the student number, the name but miss out the course the field validators do not work and it creates the database entry. Anyone has anything like this before? for reference here is my code that the webspace falls over on:

When the button to submit a student is clicked this method is invoked

Code:
Sub processValidation(ByVal s As Object, ByVal e As EventArgs)
        Dim studentID As String
        studentID = Convert.ToInt32(txtStudentID.Text)
        Dim studentCheck As New System.Data.DataSet
  error occurs here --> studentCheck = validateStudent(studentID)
        If studentCheck.Tables(0).Rows.Count = 1 Then
            lblError.Text = ("This student is already enrolled")
        Else
            addStudent()
        End If
    End Sub

The method then checks the id against the database

Code:
Function validateStudent(ByVal studentID As Integer) As System.Data.DataSet
        Dim queryString As String = "SELECT [STUDENT].[studentID] FROM [STUDENT] WHERE ([STUDENT].[studentID] = @stude" & _
            "ntID)"
        objCmd = New OleDbCommand
        objCmd.CommandText = queryString
        objCmd.Connection = objConn

        Dim dbParam_studentID As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_studentID.ParameterName = "@studentID"
        dbParam_studentID.Value = txtStudentID.Text
        dbParam_studentID.DbType = System.Data.DbType.Int32
        objCmd.Parameters.Add(dbParam_studentID)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = objCmd
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function

I am using Visual Studio 2005 but I doubt that would be the problem, anyone else got any ideas as I can't see it myself?

Thanks in advance
 
You've declared studentID as a string yet the value you're setting it is an int. You're then sending this string to a method that requires an int. Can't understand why that would work on your local machine though.
 
Last edited:
Just tried that and I have the same again. Just to clarify this is the correct way to change the textbox to an integer value? (I assume this has to be done as my studentID is a number in my database)

Code:
Dim studentID As String = txtStudentID.Text

studentID = Convert.ToInt32(txtStudentID.Text)
 
Dim it as in Int, not a string - then do a Convert.ToInt the value of the textbox directly. Really you should catch a FormatException and set a customvalidator to invalid if it's not a real number
 
I have got it like this now

Code:
Dim studentID As Integer
txtStudentID.Text = Convert.ToInt32(studentID)

I dont get the error anymore however it tells me a student already exists on the system even though they dont. Is this another simple mistake by me?
 
Vedder said:
I have got it like this now

Code:
Dim studentID As Integer
txtStudentID.Text = Convert.ToInt32(studentID)

I dont get the error anymore however it tells me a student already exists on the system even though they dont. Is this another simple mistake by me?

Yes, sadly it is. I think you need to go back to basics of programming as if you actually understood the logic of programming you wouldn't be making these mistakes.

It should be:

Code:
Dim studentID As Integer
studentID = Convert.ToInt32(txtStudentID.Text)
 
Altered Corpse said:
Yes, sadly it is. I think you need to go back to basics of programming as if you actually understood the logic of programming you wouldn't be making these mistakes.

It should be:

Code:
Dim studentID As Integer
studentID = Convert.ToInt32(txtStudentID.Text)

Thats the first thing I tried and I get a

Code:
input string was not in a correct format
error
 
Ok, but you should think about what you're doing. When you changed it in your last post you were trying to set the value of the textbox to a non existant value and also tried to convert something that was already an integer into an integer. The way i've stated above is the correct way. You've also got an error in the validateStudent function.

You've got:

Code:
dbParam_studentID.Value = txtStudentID.Text

It should be:

Code:
dbParam_studentID.Value = studentID
 
Seems like I needed to do a bit more testing than I thought. What I have found out is that the process works in Internet Explorer but I still get the error in Mozilla Firefox. This seems really odd, has anyone had a similar problem?

Thanks for your help
 
Back
Top Bottom