gpt4 book ai didi

ASP.NET/VB 读/写 Cookie - 对象未设置为对象的实例

转载 作者:行者123 更新时间:2023-12-02 02:08:05 28 4
gpt4 key购买 nike

所以我有这个代码:

        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,我已经重建了解决方案等,没有区别吗?

最佳答案

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

尝试一下 C#

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

在 VB.Net 中

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

关于ASP.NET/VB 读/写 Cookie - 对象未设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27899190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com