C# “隐藏字段”上的错误;对象引用未设置为对象的实例&引用;

C# “隐藏字段”上的错误;对象引用未设置为对象的实例&引用;,c#,asp.net,C#,Asp.net,我想向用户显示一条消息,但我在这一行遇到一个错误: HF_Reg_Id.Value = Session["Reg_id"].ToString(); 其中HF_Reg_Id是一个隐藏字段 标记: <asp:GridView ID="GV_Contact" runat="server" DataKeyNames="Contact_id" AutoGenerateColumns="False"AllowPaging="True"AllowSorting="True"> <

我想向用户显示一条消息,但我在这一行遇到一个错误:

HF_Reg_Id.Value = Session["Reg_id"].ToString();
其中HF_Reg_Id是一个隐藏字段

标记:

 <asp:GridView ID="GV_Contact" runat="server" DataKeyNames="Contact_id" AutoGenerateColumns="False"AllowPaging="True"AllowSorting="True">
    <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
    <Columns>
           <asp:BoundField DataField="Contact_id" HeaderText="ID" />
           <asp:BoundField DataField="Reg_id" HeaderText="Register ID" /> 
           <asp:BoundField DataField="txtName" HeaderText="Name" />
           <asp:BoundField DataField="Email" HeaderText="Email" />
           <asp:BoundField DataField="txtBody" HeaderText="Message" />
           <asp:BoundField DataField="MsgType" HeaderText="Message Type" /> 
    </Columns>  
</asp:GridView>

在分配会话之前,您应该检查会话是否存在,以及会话是否具有该密钥

if(Session != null && Session["Reg_id"] != null)
{
     HF_Reg_Id.Value = Session["Reg_id"].ToString();
}

欢迎来到堆栈溢出。请在标题中总结您的问题,并在正文中添加带详细信息的描述性问题。哪一行抛出了错误?这里有几个对HF_Reg_Id的引用,我假设它是您的隐藏字段。另外,如果错误与隐藏字段相关,为什么要向我们显示GridView的标记?您确定会话中包含值吗?此行抛出错误HF_Reg_Id.value=session[“Reg_Id”]。ToString();
if(Session != null && Session["Reg_id"] != null)
{
     HF_Reg_Id.Value = Session["Reg_id"].ToString();
}