954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please help :Input string was not in a correct format

I searched in this forum, but it didnt help my solve my problem. Please help me.
This is my code

int intOrderNo = (int) Session["sOrderNo"];
DetailsViewRow row0 = DetailsView1.Rows[0];
string strProductId = row0.Cells[1].Text;
DetailsViewRow row6 = DetailsView1.Rows[6];
string strUnitPrice = row6.Cells[1].Text;
float floUnitPrice = float.(strUnitPrice);
string strQty = DropDownList1.Items[DropDownList1.SelectedIndex].ToString();
int intQty = int.Parse(strQty);

string strSQL = "INSERT INTO ItemTable(bOrderNo, bProductId, bQty, bUnitPrice)"
+ "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
cmd = new OleDbCommand(strSQL, mDB);
cmd.Parameters.AddWithValue("@OrderNo", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", strProductId);
cmd.Parameters.AddWithValue("@Qty", intQty);
cmd.Parameters.AddWithValue( "@UnitPrice" , floUnitPrice );
mDB.Open();
cmd.ExecuteNonQuery();
mDB.Close();
Response.Redirect("ShoppingCart.aspx");

But i got an error saying that Input String was not in a correct format. Below is the stack trace

[FormatException: Input string was not in a correct format.]
System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) +9415395
System.Single.Parse(String s) +23
ProductDetails.BuyBtn_Click(Object sender, EventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2010\WebSites\WebSite4\ProductDetails.aspx.cs:39
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

fadeterra
Newbie Poster
4 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Where, on which line of code this error appears? Please use a break point.

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

Oh thanks for your input.The error begins at the following line:
float floUnitPrice = float.(strUnitPrice);

fadeterra
Newbie Poster
4 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

If any conversion shoult take pleace, always is recommended to use a try/catch structure to catch any exception and be able to process as expected.

Most of the problems with number conversions came of not testing, with some regex, that the input value is a valid number.

Also, many times is needed to convert the number format from local culture to invariant culture, using the System.Globalization, in order to process smoothly.

Hope this helps

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

It means your value can NOT be converted to float. Its just not that object to cast.
Set break point, and go to check what kind of value has "strUntitPrice".

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

strUnitPrice is supposed to be the price of a product.
If float cannot be used, can someone tell me what to use for this?

fadeterra
Newbie Poster
4 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Right I found the solution to it.
Turns out it was a honest mistake by me >.<
It should have been DetailsViewRow row5 = DetailsView1.Rows[5]; instead

So, to anyone who is having the same problem as me, try to check if the rows where you are reading the values off of is the correct row

And thanks to anyone who replied to thread

Cheers,
FadeTerra

fadeterra
Newbie Poster
4 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: