【问题标题】:ASP.NET/VB Reading/Writing Cookies - Object not set to instance of an objectASP.NET/VB 读取/写入 Cookie - 对象未设置为对象的实例
【发布时间】:2015-01-12 18:29:54
【问题描述】:

所以我有这个代码:

        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

我正在尝试读取/写入 cookie,但每次运行时,我都会收到“对象未设置为对象的实例”错误。

有人知道为什么吗?


我稍微改了一下代码,还是一样的错误?根据下面的评论,我会检查该值是否为空。

            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

另一个编辑:

我在 Try 行添加了一个断点,猜猜它没有命中什么。 “未加载符号信息等”。我尝试手动加载 DLL,我已经重建了解决方案等,没有区别?

【问题讨论】:

标签: asp.net vb.net cookies


【解决方案1】:

您收到此异常是因为您尝试读取 cookie 集合中不存在的 cookie。

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

在 C# 中试试这个

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

在 VB.Net 中

If Request.Cookies.Get("curUserId") Is Nothing Then

【讨论】:

    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2013-04-19
    • 1970-01-01
    相关资源
    最近更新 更多