472,127 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Convert string to int

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
Nov 26 '05 #1
5 12334
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" <nb*****@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
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

Nov 26 '05 #2
nbohana wrote:
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' <snip> 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??


"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:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 26 '05 #3
"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
Nov 26 '05 #4
lcost = float.Parse(variable);

"Anders Norås" <an**********@objectware.no> wrote in message
news:66*************************@news.microsoft.co m...
"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

Nov 27 '05 #5
Just parse it using things like Double.Parse. It you are sure it is an
int, cast to int.

Nov 28 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nathan | last post: by
3 posts views Thread by news.hku.hk | last post: by
5 posts views Thread by sarab | last post: by
3 posts views Thread by Petr Jakes | last post: by
5 posts views Thread by XML newbie: Urgent pls help! | last post: by
3 posts views Thread by Ursula | last post: by
3 posts views Thread by Shawn Ferguson | last post: by
12 posts views Thread by Peter | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.