Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table one field is price and datatype is varchar(50),

how can i use that value as integer in my code....
Posted

In asp.net you can use
Convert.Toint32("varchar value");


OR

int.Parse("varchar value");
 
Share this answer
 
v3
C#
txtPrice.text=databasePriceValue;

Int intPrice=Convet.Toint32(txtPrice.text)
 
Share this answer
 
v2
Use Cast or Convert to get the value in interger and then use

SQL
SELECT CAST(price AS INT) FROM Table
SELECT CONVERT(INT, price ) FROM Table
 
Share this answer
 
Hello friend,

for using a varchar value as integer there are many way to do that...

1) Using c# code
C#
int price = Convert.ToInt32(price.toString());

or

int price = int.Parse(price.toString());

and it will be converted into Integer value


2) Using SQL Syntax
SQL
SELECT CAST(Price AS INT) FROM yourtablename
OR
SELECT CONVERT(INT,PRICE)FROM yourtablename
 
Share this answer
 
v2
You will retrieve your data using SqlDataReader / SqlCommand Object then you will write your code as:
int price=0;
SqlCommand cmd=new SqlCommand("your query",'SqlConnection Object');
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
   price=Convert.ToInt32(dr[0].ToString());
}
dr.Close();


And you're done!
You have the integer 'price' ready to use in your code. Enjoy!
 
Share this answer
 
use keyword
SQL
Cast(column_name as data-type)


Ex.
SQL
Cast(price as int) as Price
 
Share this answer
 
v2

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