0

Basically I am working on a project which is very much similar to inventory management system. Uptill now I have made users form, category form and I was working on products form when this error came I have tried different methods but none of them seems to work.

In all the forms I am using stored procedures and some classes like insertion to add,updation to update,deletion to delete and retrieval to view the data.The exception comes to be "input string is not in correct format" Need help!

Insertion.insertProductItem=(pitemtext.Text, Convert.ToSingle(priceTxt.Text),Convert.ToInt32(catDD.SelectedValue.ToString()),status)
  • 4
    You have *two* conversions going on in that line of code - `priceTxt` to single and `catDD.SelectedValue.ToString()` to int32 - so how do you know which one is failing? Break up the line of code and debug it. **[Here's how](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Jul 05 '20 at 19:48

1 Answers1

0

Most probably the error message is coming from Convert.ToSingle(priceTxt.Text) or from Convert.ToInt32(catDD.SelectedValue.ToString().

This error message simply means that the string you are trying to convert into int or single doesn't contain a valid string that can be converted to int or single. For example, following code will issue Input string is not in a correct format exception.

int foo = Convert.ToInt32(" "); //or even null
int foo = Convert.ToInt32("5p");
//and there can be many such cases. Browse the last link at the bottom.

I would suggest, check out the Int.TryParse method - it's much easier to use if the string might not actually contain an integer - it doesn't throw an exception and you can simply write an if statement to see if it works or not. Like,

int foo = 0;
if(int.TryParse(textBox1.Text,out foo))

You can even use Single.TryParse() method, read here

Now one last thing. This exception could also be thrown when you try to Convert.ToSingle() value that does not belong to the CurrentCulture. So you could instead use Convert.ToSingle(String, IFormatProvider). You can read more here.

A similar question has been asked earlier that can be helpful. Read here.

Salar Askar
  • 126
  • 6