Issue with Update String and Parameters....

  • Thread starter Thread starter Manuel Canas
  • Start date Start date
M

Manuel Canas

Hello there,

this is the piece of code that if giving problems right now;

cnSQL = New SqlConnection(ConnectionString)

**** strSQL = "UPDATE tb_product SET (ProductID = @cProductID, Code = @Code,
ServiceName = @ServiceName, Price = @Price " & _

"WHERE ProductID = @cProductID)" ****

cmSQL = New SqlCommand(strSQL, cnSQL)

cmSQL.Parameters.Add("@cProductID", SqlDbType.Int).Value = txtProductID.Text

cmSQL.Parameters.Add("@Code", SqlDbType.Char).Value = txtServiceCode.Text

cmSQL.Parameters.Add("@ServiceName", SqlDbType.Char).Value =
txtServiceName.Text

cmSQL.Parameters.Add("@Price", SqlDbType.Money).Value = txtPrice.Text

Can anybody tell me please why am I getting an general error saying that the
input String is not in the correct format?

Thanks very much,

Manny
 
You need to cast your text boxes/strings to integers, i.e.,

Cint(txtProductID.Text)

You may also need to validate that none of those are empty strings.
 
From what I can tell the error is occurring on the line that reads:
cmSQL.Parameters.Add("@cProductID", SqlDbType.Int).Value =
txtProductID.Text
and would also occur on the line that reads
cmSQL.Parameters.Add("@Price", SqlDbType.Money).Value = txtPrice.Text

Essentially, you're trying to force a string (txtProductID.Text) into an int
and another string (txtPrice.Text) into a double (the money type in Sql
Server is really a decimal with the precision and scale set). Because there
is no implicit conversion defined for string to any numeric value you'll
need to use the parse method of the appropriate target data type (ie:
Int32.Parse(txtProductID.Text)).

Read the MSDN documentation on the parse methods for more information
including how to parse currency formatted values. I've included a link for
the Int32 parse method:
http://msdn.microsoft.com/library/d...pref/html/frlrfsystemint32classparsetopic.asp
 
Back
Top