Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I want declare a variable and compare the same with few integer value,
but i have declared as

string Basic = get_data.Tables[0].Rows[0]["BASIC"].ToString();

While I am comparing the same it say that, string cannot be compared with integer.
I am new to .Net, Kindly suggest how to declare and as what should I declare.
I know it is a simple question, but sorry kindly suggest.
Posted

1 solution

You can't compare a string to an integer in C# - they are not compatible. As far as the compiler is concerned, you might be trying to compare the word "Dolphins" with the number 42 - which is just silly.

There are ways to do what you want, but they will depend to a large extent on what kind of data you have to play with. For example, is the data you are retrieving from your database numeric in the first place? Have a look at the table definition, and find out. If it is a number, then don't convert it to a string, convert it to a number instead (which is pretty must a case of placing (int) or (double) in front of the get_data call and changing the type of "Basic" to an int or double as needed), and then use that.

If it is a string, then you need to Parse it to a number before you can use it, but if it is a number-in-a-string then why are you storing it as a string in the first place?
 
Share this answer
 
Comments
_Amy 26-Aug-12 6:26am    
5'ed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900