Click here to Skip to main content
15,117,882 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Pls some one help to correct this error to convert int to string My code is in c# is


C#
Session["priceday1"] = Label2.Text ;

               Label4.Text = Convert.ToInt32(Session["priceday1"].ToString())  / 44;



In second line there is error to convert int to string so.Pls some one help that how to correct this erro.
Posted
Updated 26-May-11 1:09am
v2
Comments
Jonathan Cardy 26-May-11 6:03am
   
What is the string value of Session['priceday1']? Is it convertable to an int?
Kim Togo 26-May-11 7:55am
   
Repost: http://www.codeproject.com/Questions/202130/convert-string-to-int-in-session-calcu.aspx
Sergey Alexandrovich Kryukov 26-May-11 15:46pm
   
Look at my answer. I don't think any help can help, if you also look at all previous questions.
--SA

I would not use Convert at all. You would have to catch an ugly exception when the string cannot be converted. int.TryParse() is a much better choice:

int Result;


if (int.TryParse(Session["priceday1"], out Result))
{
    // parsing was successful
    Result /= 44;
    Label4.Text = Result.ToString();
}
else
{
    // conversion not successful, handle error here
}


Besides that, what sense does all this make? Why do you convert a string to int, just to take the int and convert it to a string again? Why don't you just assign the original string from the session directly to the label's text property? Ahh, I got it!. I overlooked the division.
   
v3
just do this

C++
Label4.Text = (Convert.ToInt32(Session["priceday1"])  / 44).ToString();
   
C#
try this
 
Session["priceday1"] = "5464";
              // Session["priceday1"] = 5464;
           int finalPrice;
           if (Int32.TryParse(Session["priceday1"].ToString(), out finalPrice))
           {
               finalPrice = finalPrice / 44;
               Label1.Text = finalPrice.ToString();
           }
           else
           {
               Label1.Text = "Invalid";
           }
   
v2
Try this,....


MIDL
Session["priceday1"] =Label2.Text ;
          Label4.Text =(Convert.ToInt32(Session["priceday1"]) / 44).ToString();
   
Here:

Label4.Text = Convert.ToInt32(Session["priceday1"]).ToString();

You have a bracket in the wrong place.
   
I never could imagine that someone would be able to do so: convert string to integer, immediately convert back to string and complain it's not integer as it fails to divide by 44!

Hard-coded immediate string constant, hard-coded 44!

Thank you very much. Unforgettable experience.

[EDIT #1]

Somebody always dislike harsh answers and down-vote. It cannot help OP. I say something which I really have to say, no more no less.

[EDIT #2]

Answering the question in the comment below:

C#
Label4.Text = Convert.ToInt32(Session["priceday1"].ToString())  / 44;


has another bug: the type on the left of "=" is string on the right — integer. I have no idea what tells you to assign one to another. You would nee to call ToString on all of it:

C#
Label4.Text = (Convert.ToInt32(Session["priceday1"].ToString())  / 44;).ToString()


And the naming of members/variables is no good. Never use auto-generated names, rename to something semantic. Auto-generated names violate (good) Microsoft naming conventions (in particular, because there is no an algorithm to name them better). And hard coded immediate constants like "procedays1" is no good.

—SA
   
v4
Comments
Member 9909575 11-Apr-13 8:19am
   
This is my Error
**** Cannot implicitly convert type 'string' to 'long' ****

What is the meaning of that error and how can i clear that
Sergey Alexandrovich Kryukov 11-Apr-13 11:40am
   
What part of this error could possible be unclear? Well, please see the update after [EDIT #2]...
—SA

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