locked
Input string was not in a correct format. RRS feed

  • Question

  • User-433155258 posted

    Hi I keep getting this error, I don't know what I'm doing wrong but I'm sure it's something small

    Imports System.Data

    Imports System.Data.SqlClient

    Partial Class SecurePrinSecPages_AddBeoordelingInherits System.Web.UI.Page

     

    Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

    Dim conn As New System.Data.SqlClient.SqlConnection(conString)

    Dim studentID As Integer

     

    Protected Sub Nametextbos_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StudentIDtextbox.TextChanged

    End Sub

    Protected Sub Submitbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submitbutton.Click

    'Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("StudentSystemConnectionString").ConnectionString)

     

    Dim cmdString As String = _

    "INSERT INTO beoordeling " + _

    "(studentID,schoolID,grade_id,ae_id,grstatus_id, b_datum, b_commentaar,teacherID)" + _

    "VALUES (@studentID,@schoolID,@grade_id,@ae_id,@grstatus_id,@b_datum,@b_commentaar,@teacherID)"

    Dim cmd As New SqlCommand(cmdString, conn)

    studentID = Integer.Parse(StudentIDtextbox.Text)

    Session("StudentID") = studentID

     

     

    Dim p1 As New SqlParameter

    p1.ParameterName = "@studentID"

    p1.Value = Integer.Parse(StudentIDtextbox.Text)

     

    Dim p2 As New SqlParameter

    p2.ParameterName = "@schoolID"

    p2.Value = SchoolIDtextbox.Text

    Dim p3 As New SqlParameter

    p3.ParameterName = "@grade_id"

    p3.Value = GradeIDdropdownlist.Text

     

    Dim p4 As New SqlParameter

    p4.ParameterName = "@ae_id"

    p4.Value = aetexbox.Text

    Dim p5 As New SqlParameter

    p5.ParameterName = "@grstatus_id"

    p5.Value = statusdropdownlist.Text

     

    Dim p6 As New SqlParameter

    p6.ParameterName = "@b_datum"

    p6.Value = DropDownList3.Text

     

     

    Dim p7 As New SqlParameter

    p7.ParameterName = "@b_commentaar"

    p7.Value = TextBox5.Text

    Dim p8 As New SqlParameter

    p8.ParameterName = "@teacherID"

    p8.Value = TeacherIDtextbox.Text

    cmd.Parameters.Add(p1)

    cmd.Parameters.Add(p2)

    cmd.Parameters.Add(p3)

    cmd.Parameters.Add(p4)

    cmd.Parameters.Add(p5)

    cmd.Parameters.Add(p6)

    cmd.Parameters.Add(p7)

    cmd.Parameters.Add(p8)

    conn.Open()

    cmd.ExecuteNonQuery()

    conn.Close()

    MsgBox(
    " Student has been submitted into the database", MsgBoxStyle.Information)Response.Redirect("ViewBeoordeling.aspx?studentID=StudentID")

     

    End Sub

     

    End Class

     

    Tuesday, June 24, 2008 10:17 PM

Answers

  • User1104273091 posted

    Check the SQLParameeters properly. Specialy ID Columns can be Numaric Values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 24, 2008 11:30 PM
  • User-1381981065 posted

    Hi,

    You are getting this error because when you try to parse StudentID a string as integer and the string is either empty or null, the parse operation fails.

    You can either use integer.TryParse() method or

    Use a nullable integer like this:-

    Nullable <integer> intstudentId = null

    if ( studentIdTextbox.text != "" && studentIdTextbox.text != null )

    {

        intstudentid = integer.parse(studentIdTextbox.text); 

    }

     

    regards,

    varun

    _________________________________________________________________________________________

    If this post helped you, do not forget to mark this as answer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 25, 2008 12:47 AM

All replies

  • User1104273091 posted

    Check the SQLParameeters properly. Specialy ID Columns can be Numaric Values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 24, 2008 11:30 PM
  • User-1381981065 posted

    Hi,

    You are getting this error because when you try to parse StudentID a string as integer and the string is either empty or null, the parse operation fails.

    You can either use integer.TryParse() method or

    Use a nullable integer like this:-

    Nullable <integer> intstudentId = null

    if ( studentIdTextbox.text != "" && studentIdTextbox.text != null )

    {

        intstudentid = integer.parse(studentIdTextbox.text); 

    }

     

    regards,

    varun

    _________________________________________________________________________________________

    If this post helped you, do not forget to mark this as answer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 25, 2008 12:47 AM
  • User1306396600 posted

    hi ,

    I am also getting the same error for this code.Please help. 

    cmd.Parameters(

    "@tprice").Value = (System.Convert.ToInt16(cmd.Parameters("@quantity").Value.ToString())) * (System.Convert.ToInt16(cmd.Parameters("@uprice").Value.ToString()))

    Friday, December 17, 2010 6:49 AM