I want to convert a session to int, but it didn't work for me!
I get error
Compiler Error Message: CS1928: 'object' does not contain a definition for 'AsInt' and the best extension method overload 'System.Web.WebPages.StringExtensions.AsInt(string)' has some invalid argument
What Im doing wrong? Im a newbie with C#
Session["test"] = 1;
var test=Session["test"];
test=test.AsInt();
Session.Add("abc", 2);
int nResult;
if (Int32.TryParse(Session["abc"].ToString(), out nResult))
{
// safe to use nResult
}
else
{
// the session was not int
}
Please Mark As Answer, if you find my post useful
Marked as answer by hocke_2005 on Feb 08, 2013 01:53 PM
var temp=Session.Add("abc", 2);
if (temp == null) {temp="";}
int nResult;
if (Int32.TryParse(temp.ToString(), out nResult))
{
// safe to use nResult
}
else
{
// the session was not int
}
Marked as answer by hocke_2005 on Feb 12, 2013 06:35 AM
The actual answer to your question that nobody addressed is that session variables are an object type, whereas AsInt() is an extension method on the string type. You needed to convert the session variable to a string to be able to use AsInt() on it:
Session["test"] = 1;
var test = Session["test"].ToString().AsInt();
hocke_2005
Member
8 Points
14 Posts
Convert a session to Int with AsInt
Feb 08, 2013 11:59 AM|LINK
I want to convert a session to int, but it didn't work for me!
I get error
Compiler Error Message: CS1928: 'object' does not contain a definition for 'AsInt' and the best extension method overload 'System.Web.WebPages.StringExtensions.AsInt(string)' has some invalid argument
What Im doing wrong? Im a newbie with C#
SalmanAnjari...
Member
72 Points
18 Posts
Re: Convert a session to Int with AsInt
Feb 08, 2013 12:03 PM|LINK
try
Session.Add("abc", 2);
int nResult;
if (Int32.TryParse(Session["abc"].ToString(), out nResult))
{
// safe to use nResult
}
else
{
// the session was not int
}
oned_gk
All-Star
35726 Points
7295 Posts
Re: Convert a session to Int with AsInt
Feb 08, 2013 12:07 PM|LINK
Suwandi - Non Graduate Programmer
aniketmalvan...
Member
153 Points
37 Posts
Re: Convert a session to Int with AsInt
Feb 08, 2013 12:11 PM|LINK
try this;
Session["test"] = 1;
var test= (Int32) Session["test"];
Aniket
oned_gk
All-Star
35726 Points
7295 Posts
Re: Convert a session to Int with AsInt
Feb 08, 2013 12:14 PM|LINK
Do you mean?
Suwandi - Non Graduate Programmer
hocke_2005
Member
8 Points
14 Posts
Re: Convert a session to Int with AsInt
Feb 08, 2013 02:11 PM|LINK
hocke_2005
Member
8 Points
14 Posts
Re: Convert a session to Int with AsInt
Feb 12, 2013 06:35 AM|LINK
The solution
var temp=Session.Add("abc", 2); if (temp == null) {temp="";} int nResult; if (Int32.TryParse(temp.ToString(), out nResult)) { // safe to use nResult } else { // the session was not int }oned_gk
All-Star
35726 Points
7295 Posts
Re: Convert a session to Int with AsInt
Feb 12, 2013 12:24 PM|LINK
For checking null session
If (Session["test"]!=null) { }Suwandi - Non Graduate Programmer
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: Convert a session to Int with AsInt
Feb 13, 2013 04:54 AM|LINK
The actual answer to your question that nobody addressed is that session variables are an object type, whereas AsInt() is an extension method on the string type. You needed to convert the session variable to a string to be able to use AsInt() on it:
test is now an integer with a value if 1.
Web Pages CMS | My Site | Twitter
hocke_2005
Member
8 Points
14 Posts
Re: Convert a session to Int with AsInt
Feb 13, 2013 09:30 AM|LINK
Your solution is better!
Thanks to all