Link to home
Create AccountLog in
Avatar of smithmrk
smithmrkFlag for United States of America

asked on

ASP.net Cookies (VS2005)

OK, this seems so simple...but I'm not having much luck with this!
Here is what I'm trying to do...I have a RadioButtonList that displays a list of reports.
These reports all have different types of Paramaters that need to be passed in to the SQL Statement to produce the correct report.

As the user selects a report I want to update the Cookie with ALL the paramater information so that when the click the Run Report Button I can grab those parameters from the cookie and produce the report.

Here is the code for the RadioButtonList:
        Dim MyReportDataView As DataView
        MyReportDataView = CType(SQL_WebReports.Select(DataSourceSelectArguments.Empty), DataView)

        'Get the Report ID and the Parameters for that Selected Report:
        Dim myCookie As HttpCookie = New HttpCookie("ReportParameters")
        For Each MyRow As DataRowView In MyReportDataView
            myCookie.Values("Report_StoredProcedure") = Trim(MyRow("StoredProcedure"))
            myCookie.Values("Report_FileName") = Trim(MyRow("ReportName"))
            myCookie.Values("Report_Parameters") = Trim(MyRow("Parameters"))
            myCookie.Values("Report_Location") = "Reports\" & Trim(MyRow("ReportName"))
            myCookie.Expires = DateTime.Now.AddDays(1)
            Response.Cookies.Add(myCookie)
        Next

Here is the Code for the Run Report Button:
        If Not Request.Cookies("ReportParameters") Is Nothing = True Then
            Dim sTest As String = Request.Cookies("ReportParameters")("Report_FileName")
        End If

When I look at my sTest String I get NOTHING???
Why?

Thanks,
Mark
Avatar of guru_sami
guru_sami
Flag of United States of America image

Any reason to use cookies to store all values? I see you storing storedproc name in the cookie which might be a security risk.

To troubleshoot your cookie issue:
1: Set a break on this line:  Response.Cookies.Add(myCookie) and see if proper values are set for the cookie.

2: You can use browser tools like IE's web developer tool to check if the cookie it self is set.

3: Since you are setting cookie in the loop, it could be a problem as you are using the same cookie name over and over. So only the last item in the loop will be saved in the cookie and rest of the others will be lost.
Avatar of smithmrk

ASKER

Thanks guru_sami!
First I agree with your security risk...but this is my first time experimenting with cookies and so this isn't really going to be deployed I just to a section of a web site I was working on to play around with.

I wanted to show you something I just tested and see the attached PDF screen shot and see if you can find out what is going on because nothing stands out to me!

I added a sTest String variable at the END of my loop to see the cookie right after it has been created and it still shows nothing???

Thanks,
Mark
Cookies.pdf
That myCookie you added in the foreach won't show up until the next request.
So what you are seeing is correct.
OK, but isn't the Next Request in my screen shot the sTest Varaible?

Why is it nothing?

Mark
How about trying it with a simple test and very simple cookie:

Dim testCookie As HttpCookie = New HttpCookie("testcookie","Myvalue")
Response.Cookies.Add(testCookie)

Then read it in your button click.

Response.Write(Request.Cookies("testcookie"))
Yes that works fine!
Why won't it work with a Collection of Values?

Thanks,
Mark
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
OK, I think I just found my problem!!!!
Some of my Stored Procedures have a ";1" for example Report;1

I think the Semi-Colon is throwing off the Cookie!

Because it seems to work fine when I select the reports without the semi-colon.

Mark
Thank You for helping me debug my problem!
It also appears that some of my StoredProcedure Names were also too long because once I removed the semi-colons I got everything I was looking for except one report which turned out to be too long because once I shortened the StoreProcedure name it worked.

Thanks again...I had been pulling my hair out for the past two days thinking it had to do with my code when really it had to do with my values.

Mark