FAILED to convert parameter value from a String to a Int32 in ASP.NET 2.0?

My name is Vinh Huynh, and I have a problem when I try to INSERT a record to a databse (SQL Server Express 05 edition)

A) Here is an ERROR MESSAGE shows on line 2 and line 4 below:

“FAILED to convert parameter value from a string to Int32”

  1. comm.Parameters.Add(“@StationNumber”, System.Data.SqlDbType.Int)

  2. comm.Parameters(“@StationNumber”).Value = stationTextBox.Text

  3. comm.Parameters.Add(“@CategoryID”, System.Data.SqlDbType.Int)

  4. comm.Parameters(“@CategoryID”).Value = categoryList.SelectedItem.Value

B) If I try to assign “stationTextBox.Text = an integer”, it WORKS great

For example, they are working with "an integer and Integer.Parse cast"

comm.Parameters("@StationNumber").Value = 45 or
comm.Parameters("@StationNumber").Value = Integer.Parse(stationTextBox.Text)

C) I have a PROBLEM with line 4 like this:

 comm.Parameters("@CategoryID).Value = categoryList.SelectedItem.Value

Wow, I can NOT use Integer.Parse or Int32(“string”) cast, Please tell me why?

Otherwise, if we assigned to an integer, it work. WE don’t want to hard-code:

   comm.Parameters("@CategoryID).Value = 2

Anyone out there with any suggestions or comments are welcome. Thank you so much for your valueable time. Have a nice day!!


The error states the problem:

FAILED to convert parameter value from a string to Int32

You are trying to put a string value into an integer field. You need to convert it over first.

Moved to the .NET forum.

Try using something like:

comm.Parameters("@CategoryID").Value = int.Parse(categoryList.SelectedValue)

If it might not parse correctly, check out the Int32.TryParse method.

comm.Parameters("@CategoryID).Value = Convert.ToInt32(categoryList.SelectedValue)