Sorry, I am new to C#. I am getting a value for quantity from sqlserver. Usually, it comes as a decimal string. For example as "6.00". I have to store it as an int in a C# variable. I am doing the following which worked in a few rows of data I have tested:
int newVal =
Convert.ToInt32(Convert.ToDecimal(drUsers["quantity"].ToString()));
If I convert directly to int without converting it first to decimal then it throws "Input not in correct format" error. Is there a better way of doing it, especially to handle data errors such as null coming from the database.
Thanks
TryParsewill returnfalseif the conversion fails, which will allow you to handle invalid data without throwing an exception.