Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting cookie values 1

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
I'm creating a cookie by this method :

HttpCookie objCookie = new HttpCookie(strCookieName);
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
objCookie.Values.Add(strCookieName, strCookieValue);

But when I read the cookie, I can't convert the value (strCookieValue) to an integer. I get the error 'Input string was not in a correct format'.


Can anybody help me out with this?


Thanks,

lfc77
 
you must be putting something in there that is not an number...what kind of values are you storing there?

dlc
 
The value is an integer that I converted to a string so that it met the correct signature :

objCookie.Values.Add(string, string);
 
public string GetCookie(string strCookieName)
{
string strCookieValue = "";

try
{
strCookieValue = Request.Cookies[strCookieName].Value;
}
catch
{
strCookieValue = "no cookie";
}

if (Request.Cookies[strCookieName] != null)
{
return strCookieValue;
}
else
{
return "no cookie";
}
}

private void Page_Load(object sender, System.EventArgs e)
{
string strCookieName = "CallUK";

string strCookieValue = GetCookie(strCookieName);

if (strCookieValue == "no cookie")
{
Response.Redirect("default_new_user.aspx");
}
else
{
int intCookieValue = Convert.ToInt32(strCookieValue);

(here is where it crashes)


 
In your GetCookie function
try
{
strCookieValue = Request.Cookies[strCookieName].Value;
}

strCookieValue is going to have the name value pair, cookieName=cookieValue.
give this a try

try
{
HttpCookie cookie = Request.Cookies[strCookieName];
strCookieValue = cookie.Values[strCookieName];
}

strCookieValue should now just have the value

hth,
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top