Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Label13.Text = Session["Price"].ToString();

int Price1 = int.Parse(Label13.Text);  // Error is coming here.. isn't it possible?? i want place the session price value into Price1 integer variable.

int qty = DropDownList1.SelectedIndex;
int subtotal;

subtotal = qty * Price1 ;

[Edit]Code block added[/Edit]
Posted
Updated 8-Mar-13 3:40am
v2
Comments
Thomas Daniels 8-Mar-13 9:40am    
What's the value of Label13.Text?
iamsuntosh 8-Mar-13 9:42am    
It is price of a product.. session variable
Thomas Daniels 8-Mar-13 9:44am    
Yes, but what's the value of Session["Price"].ToString();
I know it's a price of a product, but what's the exact value?
iamsuntosh 8-Mar-13 9:45am    
1500
Thomas Daniels 8-Mar-13 9:46am    
What's the error?

Does it have a "$" in the field? If so, you can use:

int Price1 = int.Parse(Label13.Text, System.Globalization.NumberStyles.Currency);
 
Share this answer
 
Comments
iamsuntosh 8-Mar-13 9:58am    
no $ symbol is not there
must value of your label (actually your session) be a type which could cast to int!
this types can convert to int type!
decimal , float, double ,uint , long , ulong
must be in a correct format for casting to int!
its mean this cant parse
s22
but this can :
22
check this links :
conversion table
numeric-casts
 
Share this answer
 
Try using Convert.ToDouble

if this still doesn't work, try applying regex to extract the digits in string format.
and then Convert.ToDouble()
 
Share this answer
 
personally I would check that the conversion can happen first

C#
int Price = 0;
if(int.tryparse(label13.Text, out Price) == true)
{
  //do whats needed here
}


Further Reading:
int.parse[^]
int.tryparse[^]
 
Share this answer
 
Why not do
C#
int Price1 = int.Parse(Session["Price"]);

Maybe the label text havent been set depending on what event you are parsing the int from?
 
Share this answer
 

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