-3

In my program, I have this error come out in if (Session["EmployeeID"].ToString() != null). Error message is below :

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Web.SessionState.HttpSessionState.this[string].get returned null. System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Web.SessionState.HttpSessionState.this[string].get returned null.

I have tried add-in the sessionState under the Webconfig also did not solve the issue.

                <sessionState timeout="40" />

Below here is my Code:

Login.aspx.cs

if (EmployeeID == txtEmployeeID.Value) {
    Session["EmployeeID"] = EmployeeID;
    Session["UserName"] = User;
    Session["Email"] = Email;
    Session["UserID"] = UserID;
    Response.Redirect("Home.aspx");
} else {
    lblErrorMessage.Visible = true;
}

Home.aspx.cs

public partial class Home : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        ***if (Session["EmployeeID"].ToString() !=null)*** {
            lblName.Text=Session["Name"].ToString();
        }

        if ( !Page.IsPostBack) {}
    }
}  

Web.config

<configuration>
    <connectionStrings>
        <add
            name="test.dbo"
            connectionString="Data Source=test;Initial Catalog=Card;User 
         ID=test;Password=test"
            providerName="System.Data.SqlClient"
        />
    </connectionStrings>

    <system.web>
        <sessionState timeout="40" />
        <compilation debug="true" targetFramework="4.7.2" />
        <httpRuntime targetFramework="4.7.2" />
        <pages>
            <namespaces>
                <add namespace="System.Web.Optimization" />
            </namespaces>
            <controls>
                <add
                    assembly="Microsoft.AspNet.Web.Optimization.WebForms"
                    namespace="Microsoft.AspNet.Web.Optimization.WebForms"
                    tagPrefix="webopt"
                />
            </controls>
        </pages>
    </system.web>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity
                    name="Antlr3.Runtime"
                    publicKeyToken="eb42632606e9261f"
                />
                <bindingRedirect
                    oldVersion="0.0.0.0-3.5.0.2"
                    newVersion="3.5.0.2"
                />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity
                    name="Newtonsoft.Json"
                    publicKeyToken="30ad4fe6b2a6aeed"
                />
                <bindingRedirect
                    oldVersion="0.0.0.0-12.0.0.0"
                    newVersion="12.0.0.0"
                />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity
                    name="WebGrease"
                    publicKeyToken="31bf3856ad364e35"
                />
                <bindingRedirect
                    oldVersion="0.0.0.0-1.6.5135.21930"
                    newVersion="1.6.5135.21930"
                />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <system.codedom>
        <compilers>
            <compiler
                language="c#;cs;csharp"
                extension=".cs"
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, 
        Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, 
        PublicKeyToken=31bf3856ad364e35"
                warningLevel="4"
                compilerOptions="/langversion:default /nowarn:1659;1699;1701"
            />
            <compiler
                language="vb;vbs;visualbasic;vbscript"
                extension=".vb"
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, 
          Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, 
           PublicKeyToken=31bf3856ad364e35"
                warningLevel="4"
                compilerOptions='/langversion:default /nowarn:41008 
               /define:_MYTYPE=\"Web\" /optionInfer+'
            />
        </compilers>
    </system.codedom>
</configuration>

Kindly advise.

Tmz
  • 1

1 Answers1

1

You can not call .ToString() for null objects. So when Session["EmployeeID"] returns null application throw an error.

Remove .ToString() from if condition in your code.

Pablo
  • 561
  • 4
  • 16