Answered by:
Input string was not in a correct format

Question
-
User-642154842 posted
Hi there,
I have i class that retrives order information from DB, When i click the Go button I get this error :
System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Int32.Parse(String s) at CommerceLibOrderInfo..ctor(DataRow orderRow)
This is the function that generates the error:
Public Shared Function GetOrder(ByVal orderID As String) As OrderInfo ' get a configured DbCommand object Dim comm As DbCommand = GenericDataAccess.CreateCommand() ' set the stored procedure name comm.CommandText = "OrderGetInfo" ' create a new parameter Dim param As DbParameter = comm.CreateParameter() param.ParameterName = "@OrderID" param.Value = orderID param.DbType = DbType.Int32 comm.Parameters.Add(param) ' obtain the results Dim table As DataTable = GenericDataAccess.ExecuteSelectCommand(comm) Dim orderRow As DataRow = table.Rows(0) ' save the results into an OrderInfo object Dim orderInfo As New OrderInfo(orderRow) Return orderInfo End Function
I tryed OrderId as int32 but the same error came upthanks for looking
Monday, November 16, 2009 5:57 AM
Answers
-
User1037547548 posted
Line 105: Status = Int32.Parse(orderRow("Status").ToString())What does Status field contain? Does it return in INT? check your stored procedure in query analyzer. I thing it contains null value so it can parse into int.
Or use Convert.ToInt32(). It handles null value too. If you still getting an error then it should be string or char or space in status field.
you can also use this way.
int Status = 0; int.TryParse(orderRow("Status").ToString(),out Status)
assign 0 value to status first and then use int.tryparse method if it is not in int then int.tryparse return false and status will be 0 other then its value.
or
run in debug mode and check value of status in quickwatch window
Regard
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 17, 2009 2:05 AM
All replies
-
User1037547548 posted
check your stored procedure "OrderGetInfo". I think error occurs from sp.
what variable declared in stored procedure for order id.
regard
Monday, November 16, 2009 6:17 AM -
User1522843514 posted
May be the problem is with the resultset of the Storedprocedures. It doesnot matches with the orderInfo class. Try to convert the type from the sp or in the orderInfo class.
Monday, November 16, 2009 6:25 AM -
User-231977777 posted
Hi
THe Problem is Param CIs Not the Same Data Type In DB
You Can Make This
param.Value = Convert .Toint32(orderID )
Hope THis Help
Monday, November 16, 2009 6:27 AM -
User-1360095595 posted
You might want to step through your code to determine what line specifically is returning the error. Are you setting the CommandType to StoredProcedure? Is your command execurting all right, and then you're getting an error?
Monday, November 16, 2009 6:28 AM -
User-642154842 posted
When i run the SP in SQL2005 it returns the data,
this is the SP:
CREATE PROCEDURE [dbo].[OrderGetInfo] (@OrderID INT) AS SELECT OrderID, (SELECT ISNULL(SUM(Subtotal), 0) FROM OrderDetail WHERE OrderID = @OrderID) AS TotalAmount, Date, Shipped, Verified, ShippingAddress, CustomerEmail FROM Orders WHERE OrderID = @OrderID
Monday, November 16, 2009 6:34 AM -
User-642154842 posted
I convert it to int32 but still the same error
Monday, November 16, 2009 6:39 AM -
User1037547548 posted
execute your stored procedure in query analyzer first with "exec spName 1"
or
check your table structure datatype for orderid
regard
Monday, November 16, 2009 7:04 AM -
User-642154842 posted
I run this :
EXEC OrderGetInfo 1
and it returns the first row of data.
and the DataType in SQL for orderID is Int
Monday, November 16, 2009 7:18 AM -
User1037547548 posted
In which line are you getting an error.? please highlight it
Monday, November 16, 2009 8:00 AM -
User-642154842 posted
well i do not get an error i just dont get any data displayed .
i used a catch blok to get the error
Try Dim orderInfo As CommerceLibOrderInfo = CommerceLibAccess.GetOrder(orderIDBox.Text) resultLabel.Text = "Order found." addressLabel.Text = orderInfo.CustomerAddressAsString.Replace(vbLf, "<br />") Catch ex As Exception resultLabel.Text = "No order found, or order is in old format." & ex.ToString addressLabel.Text = "" orderLabel.Text = "" End Try
I removed the Catch Blok and got the erronr here:
Line 103: DateShipped = orderRow("DateShipped").ToString()
Line 104: Comments = orderRow("Comments").ToString()
Line 105: Status = Int32.Parse(orderRow("Status").ToString())
Line 106: AuthCode = orderRow("AuthCode").ToString()
Line 107: Reference = orderRow("Reference").ToString()
Monday, November 16, 2009 8:09 AM -
User1037547548 posted
Line 105: Status = Int32.Parse(orderRow("Status").ToString())What does Status field contain? Does it return in INT? check your stored procedure in query analyzer. I thing it contains null value so it can parse into int.
Or use Convert.ToInt32(). It handles null value too. If you still getting an error then it should be string or char or space in status field.
you can also use this way.
int Status = 0; int.TryParse(orderRow("Status").ToString(),out Status)
assign 0 value to status first and then use int.tryparse method if it is not in int then int.tryparse return false and status will be 0 other then its value.
or
run in debug mode and check value of status in quickwatch window
Regard
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 17, 2009 2:05 AM -
User-642154842 posted
thanks it worked
Int23.tryparse...
I left out int status=0
thanks again
Tuesday, November 17, 2009 4:04 AM