[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

cookies asp null

I am having a problem with my asp cookies
I am trying an if statement :
if request.Cookies("itemAmount")("112") >= 1 then

I am getting this error
Type mismatch: '[string: ""]'

I tried clearing the cookie out with this:
Response.Cookies("itemAmount")(j) = ""

but it's not helping.

Any idea what is wrong?
0
rivkamak
Asked:
rivkamak
  • 7
  • 7
1 Solution
 
pateljituCommented:
Could you please try casting the cookie value, also confirm value returned form cookie:

1. if Cint(request.Cookies("itemAmount")("112")) >= 1 then
2. Do response.write for request.Cookies("itemAmount")("112") to verify the value returned.
0
 
rivkamakAuthor Commented:
1. when I tried cint, i got this error: Type mismatch: 'Cint'
2. nothing is returned. but it didn't throw an error.
0
 
pateljituCommented:
Looks like the value for the cookie is not assigned and that is resulting into error.

Try this sample:
if Not isNull(request.Cookies("itemAmount")("112")) and request.Cookies("itemAmount")("112") <> "" then 

   if request.Cookies("itemAmount")("112") >= 1 then 
      'Execute your code here
   end if
end if

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rivkamakAuthor Commented:
that's not throwing an error anymore, but later on the page I have this.
For Each key In Request.Cookies("itemID")
i = CStr(key)
IF request.cookies("itemAmount")(i) <> "" THEN
IF request.cookies("itemAmount")(i) > 0 THEN
END IF
costItem=(request.cookies("itemCost")(i)* request.Cookies("itemAmount")(i))
totalCost = totalCost + costItem
END IF 

NEXT
the request.cookie again is throwing
Type mismatch: '[string: ""]'. Data is 112

Open in new window

0
 
pateljituCommented:
I would guess the line number it errors is i = Cstr(key)

Try this please:
For Each key In Request.Cookies("itemID")
IF request.cookies("itemAmount")(i) <> "" THEN
i = CStr(key)
IF request.cookies("itemAmount")(i) > 0 THEN
    costItem=(request.cookies("itemCost")(i)* request.Cookies("itemAmount")(i))
END IF
totalCost = totalCost + costItem
END IF 

NEXT

Open in new window

0
 
rivkamakAuthor Commented:
I should keep it whatever format it is in?
0
 
rivkamakAuthor Commented:
I'm still getting a mismatch error even after I took out the cstr
error on line 5 of the code above.
0
 
pateljituCommented:
Ok, there is another cookie array that is in use i.e. ("itemcost"), so in your If statement you would need to verify is that is no NULL too.

For Each key In Request.Cookies("itemID")
IF (request.cookies("itemAmount")(i) <> "" and request.cookies("itemCost")(i) <> "") THEN
i = CStr(key)
IF (request.cookies("itemAmount")(i) > 0 ) and request.cookies("itemCost")(i) > 0 THEN
    costItem=(request.cookies("itemCost")(i)* request.Cookies("itemAmount")(i))
END IF
totalCost = totalCost + costItem
END IF 

NEXT

Open in new window

0
 
rivkamakAuthor Commented:
Now the errors all cleared, but it's not tabulating the numbers properly.
on one hand I need to make sure it's not null before checking if it's bigger than 1. But now nothing is happening.
if Not isNull(request.Cookies("itemAmount")(productID)) and request.Cookies("itemAmount")(productID) <> "" then 
response.write("first if")
	if (request.Cookies("itemAmount")(productID)) >= 1 then 
	response.write("if more than 1")
	Response.Cookies("itemAmount")(productID) = request.Cookies("itemAmount")(productID) + quantity
	else
	response.write("if none in there")
	Response.Cookies("itemAmount")(productID) = quantity
	end if 
end if 

Open in new window


I am getting no responses returned.
0
 
pateljituCommented:
Which would indicate that the cookies has no values assigned yet, could you run this code and see if all cookies has proper values assigned to it, if no values are assigned in that case please resolve that issue.

<!DOCTYPE html>
<html>
<body>

<%
dim x,y
for each x in Request.Cookies
  response.write("<p>")
  if Request.Cookies(x).HasKeys then
    for each y in Request.Cookies(x)
      response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
      response.write("<br />")
    next
  else
    Response.Write(x & "=" & Request.Cookies(x) & "<br />")
  end if
  response.write "</p>"
next
%>

</body>
</html>

Open in new window


Reference from:
http://www.w3schools.com/asp/asp_cookies.asp
0
 
rivkamakAuthor Commented:
How can I check if it's bigger than 1 without throwing off because it's really null?
0
 
pateljituCommented:
Well in case cookie is NULL you can never have a true condition for >= 1.

1. Is the value for cookie suppose to be NULL
2. If not, than the cookie is not getting values assigned properly. In which case you will need to fix the place where cookies values are getting assigned.
3. Provide with all code sample i.e. where cookie is getting assigned, where you need to use it and whats the expected value for the cookie.
0
 
rivkamakAuthor Commented:
so can I check , if cookie has been set already then check for >1 and do A
otherwise, if not set yet, do B

the code looks like this.
<%
sub updateCartCookies(itemID, quantity) 
Set itemInfo_cmd = Server.CreateObject ("ADODB.Command")
itemInfo_cmd.ActiveConnection = myConn
itemInfo_cmd.CommandText = "SELECT * FROM items WHERE id = ?" 
itemInfo_cmd.Prepared = true
itemInfo_cmd.Parameters.Append itemInfo_cmd.CreateParameter("param1", 5, 1, -1, itemID) ' adDouble

Set itemInfo = itemInfo_cmd.Execute
itemInfo_numRows = 0
productID = TRIM(itemID )
productImage = (itemInfo.Fields.Item("image").Value)
productName = (itemInfo.Fields.Item("title").Value)
productCost = (itemInfo.Fields.Item("cost").Value)


'when switch start here

Response.Cookies("itemID")(productID) = 1
Response.Cookies("itemID").Expires = Date() + 10
Response.Cookies("itemName")(productID) = productName 
Response.Cookies("itemName").Expires = Date() + 10
Response.Cookies("itemCost")(productID) = productCost 
Response.Cookies("itemCost").Expires = Date() + 10
Response.Cookies("itemImage")(productID) = productImage
Response.Cookies("itemImage").Expires = Date() + 10

'response.write("before if")
'if Not isNull(request.Cookies("itemAmount")(productID)) and request.Cookies("itemAmount")(productID) <> "" then 
'response.write("first if")
	if (request.Cookies("itemAmount")(productID)) >= 1 then 

'	response.write("if more than 1")
	Response.Cookies("itemAmount")(productID) = request.Cookies("itemAmount")(productID) + quantity
	else
'	response.write("if none in there")
	Response.Cookies("itemAmount")(productID) = quantity
	end if 
'end if 
Response.Cookies("itemAmount").Expires = Date() + 10

End Sub 'updateCartCookies

Open in new window

0
 
pateljituCommented:
Based your submitted code:

Line 23-24, you created cookie ("itemCost"):
Response.Cookies("itemCost")(productID) = productCost
Response.Cookies("itemCost").Expires = Date() + 10

Now just above line 31 paste this code:
response.write(Request.Cookies("itemCost")(productID) &" PRODUCT COST <br />")

Line 31:
You are using cookie request.Cookies("itemAmount")(productID) which was not assigned a value, maybe instead you would use cookie request.Cookies("itemCost")(productID)

Line 34:
Have changed the logic for calculating cost from + quantity to * quantity (calculating product cost times the number of quantity)

So your code would look something as below:

<%
sub updateCartCookies(itemID, quantity) 
Set itemInfo_cmd = Server.CreateObject ("ADODB.Command")
itemInfo_cmd.ActiveConnection = Conn
itemInfo_cmd.CommandText = "SELECT * FROM partners WHERE parid = ?" 
itemInfo_cmd.Prepared = true
itemInfo_cmd.Parameters.Append itemInfo_cmd.CreateParameter("param1", 5, 1, -1, itemID) ' adDouble

Set itemInfo = itemInfo_cmd.Execute
itemInfo_numRows = 0
productID = TRIM(itemID )
productImage = (itemInfo.Fields.Item("firstname").Value)
productName = (itemInfo.Fields.Item("lastName").Value)
productCost = (itemInfo.Fields.Item("datacredit").Value)


'when switch start here

Response.Cookies("itemID")(productID) = 1
Response.Cookies("itemID").Expires = Date() + 10
Response.Cookies("itemName")(productID) = productName 
Response.Cookies("itemName").Expires = Date() + 10
Response.Cookies("itemCost")(productID) = productCost 
Response.Cookies("itemCost").Expires = Date() + 10
Response.Cookies("itemImage")(productID) = productImage
Response.Cookies("itemImage").Expires = Date() + 10

response.write(Request.Cookies("itemCost")(productID) &" COST<br />")
'if Not isNull(request.Cookies("itemAmount")(productID)) and request.Cookies("itemAmount")(productID) <> "" then 
'response.write("first if")
	if (request.Cookies("itemCost")(productID)) >= 1 then 

'	response.write("if more than 1")
'Response.Cookies("itemCost")(productID) = request.Cookies("itemCost")(productID) + quantity
Response.Cookies("itemCost")(productID) = request.Cookies("itemCost")(productID) * quantity
              response.write(Request.Cookies("itemCost")(productID) &" COST PArt 2 <br />")
	else
'	response.write("if none in there")
	Response.Cookies("itemCost")(productID) = quantity
	end if 
'end if 
Response.Cookies("itemCost").Expires = Date() + 10

End Sub 'updateCartCookies
%>

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now