Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

am unable to read a cookie value from the cookies. i have set cookie value from javascript in scripting side and try to read cookie value from coding part ie. default.aspx.vb. i have shown my javascript page below:

C#
function setcookie()
  {
  var today=new Date();
  var expire=new Date();
  var ndays;
  if(ndays==null || ndays==0)ndays=1;
  expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = "hostname"+"="+VIH_HostName+ ";expires="+expire.toGMTString();
 alert(VIH_HostName)
}


this function called from onload in body tag.

here is my vb code to read cookie value

VB
If Not Request.Cookies("hostname") Is Nothing Then
                   count = Request.Cookies("hostname").Value
                   Response.Write("cookie: " & count)
               End If



if i read the cookie i get only null string.. anyone help me to resolve this problem..

regards
Sasikumar
Posted
Updated 11-Nov-10 9:02am
Comments
raju melveetilpurayil 11-Nov-10 15:02pm    
[Edit] Edited for more readability [/Edit]

Your javascript has errors: nDays instead of ndays and variable VIH_HostName is not defined.
I changed the js code.
Try this:
XML
<script type="text/javascript">
        function setcookie() {
            var today = new Date();
            var expire = new Date();
            var ndays;
            if (ndays == null || ndays == 0) ndays = 1;
            expire.setTime(today.getTime() + 3600000 * 24 * ndays);
            document.cookie = "hostname=VIH_HostName;expires=" + expire.toGMTString();
        }</script>


and get the cookie from server side like this

Response.Write(Request.Cookies["hostname"].Value);


It works for me
 
Share this answer
 
there may be an issue with the name if you're setting a cookie using javascript and reading it with VB.net.

you could loop through the cookies collection of the request object and step through it using the debugger - see if you recognise your cookie at all.
 
Share this answer
 
Comments
ramuksasi 11-Nov-10 9:19am    
thanks jim...
if i use the cookies collection the debugger not deduct the loop, its automatically skip that line..

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