2

so I have this code:

        Try
        If Request.Cookies("curUsrId")("id") Is Nothing Then
            Dim cke As HttpCookie = New HttpCookie("curUsrId")
            cke("id") = CStr(myUser.Id)
            cke.Expires = Now.AddDays(35)
            Response.Cookies.Add(cke)
        Else
            If Request.Cookies("curUsrId")("id") = "2" Then
                grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowAllRecords
                chkPaging.Checked = True
            Else
                grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowPager
                chkPaging.Checked = False
            End If
        End If
    Catch ex As Exception
        lblErrorMsg.Visible = True
        txtErrorTxt.Visible = True
        txtErrorTxt.Text = ex.Message
    End Try

I'm trying to read/write to a cookie, but everytime I run this, I get a "Object not set to an instance of an object" error.

Does anyone know why?


I changed the code slighty, still the same error? Per the comment below, I do check to see if the value is nothing.

            Try
            If Request.Cookies("curUsrId").Value Is Nothing Then
                Dim cke As HttpCookie = New HttpCookie("curUsrId")
                cke.Value = CStr(myUser.Id)
                cke.Expires = Now.AddDays(35)
                Response.Cookies.Add(cke)
            Else
                If Request.Cookies("curUsrId").Value = "2" Then
                    grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowAllRecords
                    chkPaging.Checked = True
                Else
                    grdIssues.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowPager
                    chkPaging.Checked = False
                End If
            End If
        Catch ex As Exception
            lblErrorMsg.Visible = True
            txtErrorTxt.Visible = True
            txtErrorTxt.Text = ex.Message
        End Try

ANOTHER EDIT:

I added a breakpoint at the Try line, guess, what it doesn't hit. "No symbol information loaded etc". I tried loading the DLL manually, I've rebuilt the solution etc, no difference?

Billy Brown
  • 23
  • 1
  • 8
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Rahul Singh Jan 12 '15 at 09:53
  • I checked for nothing/null above, so why doesn't it perform the 'Else' part of the statement? – Billy Brown Jan 12 '15 at 10:02

1 Answers1

5

You are getting this exception because you are trying to read value of a cookie which does not exist in cookies collection.

If Request.Cookies("curUsrId").Value //Here you are trying to read value from cookie wich is not set yet

Try this for C#

if(Request.Cookies.Get("curUsrId")==null)
{
//Your code to add cookie
}

In VB.Net

If Request.Cookies.Get("curUserId") Is Nothing Then
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40