Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert string to int

nbohana
Guest
 
Posts: n/a
#1: Nov 26 '05
I have tried several methods in converting my string data to an int. I am
getting a message back 'Input string was not in a correct format'

strSQL1 = "SELECT [service-cost] FROM [task] WHERE [service-order-number] =
" + Convert.ToInt32(txtSON.Text);

cmSQL1 = new SqlCommand(strSQL1,cnKennel);
if (cnKennel.State != ConnectionState.Open)
cnKennel.Open();
drSQL1 = cmSQL1.ExecuteReader();

if(drSQL1.Read())
{
lcost = drSQL1["service-cost"].ToString();
}

cnKennel.Close();

cost1 = int.Parse(lcost);

where cost1 is defined as a int, and lcost is a string. the data in lcost =
"35.00".

How do I fix this problem, I need help??

--
Norm Bohana

Lebesgue
Guest
 
Posts: n/a
#2: Nov 26 '05

re: Convert string to int


35.00 is not valid for an int
Parse it to float and then cast to int
Take care to use appropriate CultureInfo too.

int parsed = (int)float.Parse("35.00", yourCultureInfo.NumberFormat);


"nbohana" <nbohana@discussions.microsoft.com> wrote in message
news:C4C8F28A-C4D3-4559-A2F9-A3E8CD05C84B@microsoft.com...[color=blue]
>I have tried several methods in converting my string data to an int. I am
> getting a message back 'Input string was not in a correct format'
>
> strSQL1 = "SELECT [service-cost] FROM [task] WHERE [service-order-number]
> =
> " + Convert.ToInt32(txtSON.Text);
>
> cmSQL1 = new SqlCommand(strSQL1,cnKennel);
> if (cnKennel.State != ConnectionState.Open)
> cnKennel.Open();
> drSQL1 = cmSQL1.ExecuteReader();
>
> if(drSQL1.Read())
> {
> lcost = drSQL1["service-cost"].ToString();
> }
>
> cnKennel.Close();
>
> cost1 = int.Parse(lcost);
>
> where cost1 is defined as a int, and lcost is a string. the data in lcost
> =
> "35.00".
>
> How do I fix this problem, I need help??
>
> --
> Norm Bohana[/color]


Lasse Vågsæther Karlsen
Guest
 
Posts: n/a
#3: Nov 26 '05

re: Convert string to int


nbohana wrote:[color=blue]
> I have tried several methods in converting my string data to an int. I am
> getting a message back 'Input string was not in a correct format'[/color]
<snip>[color=blue]
> cost1 = int.Parse(lcost);
>
> where cost1 is defined as a int, and lcost is a string. the data in lcost =
> "35.00".
>
> How do I fix this problem, I need help??
>[/color]

"35.00" is not an integer, it's a floating point value. Arguably you
could say it's an integer because the part after the decimal point is
".00", but it's still formatted as a floating point value so Int32.Parse
will definitely not like it.

If you know it's always ".00", you could either:

- remove ".00" by using .Replace(".00", ""), before running it through
Int32.Parse
- use Double.Parse, and Convert.ToInt32 afterwards


--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Anders Norås
Guest
 
Posts: n/a
#4: Nov 26 '05

re: Convert string to int


"35.00" is a floating point value so it cannot be parsed to an int. You can
use double.Parse to parse the value to a Double instead of an int. Still,
since you're reading the value from a SqlDataReader, a better option is to
use the GetDouble method of the SqlDataReader.

Anders Norås
http://dotnetjunkies.com/weblog/anoras


MacKenzieMouse
Guest
 
Posts: n/a
#5: Nov 27 '05

re: Convert string to int


lcost = float.Parse(variable);

"Anders Norås" <anders.noras@objectware.no> wrote in message
news:66589407749a8c7c0f7cb796b12@news.microsoft.co m...[color=blue]
> "35.00" is a floating point value so it cannot be parsed to an int. You
> can use double.Parse to parse the value to a Double instead of an int.
> Still, since you're reading the value from a SqlDataReader, a better
> option is to use the GetDouble method of the SqlDataReader.
>
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras
>
>[/color]


Truong Hong Thi
Guest
 
Posts: n/a
#6: Nov 28 '05

re: Convert string to int


Just parse it using things like Double.Parse. It you are sure it is an
int, cast to int.

Closed Thread


Similar C# / C Sharp bytes