Input string was not in a correct format
-
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
-
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
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; }
-
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; }
I am lost, still not sure what values I need to correct to the right data type?
-
I am lost, still not sure what values I need to correct to the right data type?
-
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; }
Where do I use that double.tryparse? Where is that put in the code
-
Where do I use that double.tryparse? Where is that put in the code
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; }