Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Input string was not in a correct format

Input string was not in a correct format

Scheduled Pinned Locked Moved Visual Basic
designdata-structuresdebugginghelp
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kenn_rosie
    wrote on last edited by
    #1

    Input string was not in a correct format. Line 94 seems to be the culprit but I don't know why Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was not in a correct format. Source Error: Line 92: SqlConnection1.Open() Line 93: Dim dreader As SqlClient.SqlDataReader *********************************************************************** *********************************************************************** Line 94: dreader = cmdCategoriesById.ExecuteReader(CommandBehavior.SingleRow) ********************************************************************************** ********************************************************************************** Line 95: If dreader.Read() Then Line 96: txtCategoryName.Text = dreader(1) Source File: c:\inetpub\wwwroot\VBNetUnleashed\WebApplication3\WebForm1.aspx.vb Line: 94 Stack Trace: [FormatException: Input string was not in a correct format.] System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) WebApplication3.WebForm1.ddlCategoryID_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\VBNetUnleashed\WebApplication3\WebForm1.aspx.vb:94 System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() System.Web.UI.Page.RaiseChangedEvents() System.Web.UI.Page.ProcessRequestMain() Here is my code: Private Sub ddlCategoryID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCategoryID.SelectedIndexChanged Dim categoryid As String categoryid = ddlCategoryID.SelectedItem.Text cmdCategoriesById.Parameters("@categoryid").Value = categoryid SqlConnection1.Open() Dim dreader As SqlClient.SqlDataReader dreader = cmdCategoriesById.ExecuteReader(CommandBehavior.SingleRow) If dreader.Read() Then txtCategoryName.Text = dreader(1) txtCategoryDescription.Text = dreader(2) End If dreader.Close() SqlConnection1.Close() End Sub

    G 1 Reply Last reply
    0
    • K kenn_rosie

      Input string was not in a correct format. Line 94 seems to be the culprit but I don't know why Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was not in a correct format. Source Error: Line 92: SqlConnection1.Open() Line 93: Dim dreader As SqlClient.SqlDataReader *********************************************************************** *********************************************************************** Line 94: dreader = cmdCategoriesById.ExecuteReader(CommandBehavior.SingleRow) ********************************************************************************** ********************************************************************************** Line 95: If dreader.Read() Then Line 96: txtCategoryName.Text = dreader(1) Source File: c:\inetpub\wwwroot\VBNetUnleashed\WebApplication3\WebForm1.aspx.vb Line: 94 Stack Trace: [FormatException: Input string was not in a correct format.] System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) WebApplication3.WebForm1.ddlCategoryID_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\VBNetUnleashed\WebApplication3\WebForm1.aspx.vb:94 System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() System.Web.UI.Page.RaiseChangedEvents() System.Web.UI.Page.ProcessRequestMain() Here is my code: Private Sub ddlCategoryID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCategoryID.SelectedIndexChanged Dim categoryid As String categoryid = ddlCategoryID.SelectedItem.Text cmdCategoriesById.Parameters("@categoryid").Value = categoryid SqlConnection1.Open() Dim dreader As SqlClient.SqlDataReader dreader = cmdCategoriesById.ExecuteReader(CommandBehavior.SingleRow) If dreader.Read() Then txtCategoryName.Text = dreader(1) txtCategoryDescription.Text = dreader(2) End If dreader.Close() SqlConnection1.Close() End Sub

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      The category id could not be converted to a number. You should convert the values to the correct data type before putting them in the parameters. Also, you should specify the data type of the parameters, or the command object has to ask the database for the data types. This causes an extra round trip to the database. --- b { font-weight: normal; }

      K 1 Reply Last reply
      0
      • G Guffa

        The category id could not be converted to a number. You should convert the values to the correct data type before putting them in the parameters. Also, you should specify the data type of the parameters, or the command object has to ask the database for the data types. This causes an extra round trip to the database. --- b { font-weight: normal; }

        K Offline
        K Offline
        kenn_rosie
        wrote on last edited by
        #3

        I am lost, still not sure what values I need to correct to the right data type?

        G 1 Reply Last reply
        0
        • K kenn_rosie

          I am lost, still not sure what values I need to correct to the right data type?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          You have to make sure that the category id value can be converted to a number. If there is a possibility that it's not a number, use Double.TryParse to parse the string. --- b { font-weight: normal; }

          K 1 Reply Last reply
          0
          • G Guffa

            You have to make sure that the category id value can be converted to a number. If there is a possibility that it's not a number, use Double.TryParse to parse the string. --- b { font-weight: normal; }

            K Offline
            K Offline
            kenn_rosie
            wrote on last edited by
            #5

            Where do I use that double.tryparse? Where is that put in the code

            G 1 Reply Last reply
            0
            • K kenn_rosie

              Where do I use that double.tryparse? Where is that put in the code

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Now you are getting the string from the form field, then putting the string into the parameter. After you have gotten the string, try to parse it. If the parsing was successfull, you can continue by converting the double to an int (as I assume that is the datatype in the database) and put it in the parameter. If the parsing fails, you can either set the value to a default value, or display an error message. --- b { font-weight: normal; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups