1

I am not even sure how SessionParameters are meant to be used as opposed to a normal Parameters. What I am trying to do is Update or Insert records into my database based on a value from a Session variable "StaffID", which I know exists (based on debugging the PageLoad event). As it turns out my "StaffID" session is actually NULL (found after exhaustive debugging) and I get exceptions about trying to update a table with null values.

My SqlDataSource is thus ...

 <asp:SqlDataSource ID="SqlDataProgmDetails" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
                    UpdateCommand="UPDATE tblStudentProgramReg SET StaffModified = @StaffModified, DateModified = GETDATE() WHERE (ProgramEnrolmentID = @ProgramEnrolmentID)">
                    <UpdateParameters>
                        <asp:Parameter Name="EnrolmentDate" Type="DateTime" />
                        <asp:Parameter Name="StudentProgramNotes" Type="String" />
                        <asp:Parameter Name="TerminationDate" Type="DateTime" />
                        <asp:Parameter Name="StudProgEnrolStatusID" />
                        <asp:Parameter Name="QualificationCode" />
                        <asp:ControlParameter ControlID="gvPrograms" Name="ProgramEnrolmentID" Type="Int32" PropertyName="SelectedValue" />
                        <asp:SessionParameter Name="StaffModified" SessionField="StaffID" />
                    </UpdateParameters>
rs>
                </asp:SqlDataSource>

When I UPDATE my DetailsView I get the dreaded "String or binary data would be truncated. The statement has been terminated." error.

I know this is happening because the "StaffID" session variable that I am sure existed at one point is NULL in the UPDATE phase of the above SqlDataSource.

The only way around it I found was to force load the value in CodeBehind ...

protected void SqlDataProgmDetails_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    if (Session["StaffID"] != null)
    {
        e.Command.Parameters[5].Value = Session["StaffID"].ToString().Trim();
    }
}

protected void SqlDataProgmDetails_Inserting1(object sender, SqlDataSourceCommandEventArgs e)
{
    if (Session["StaffID"] != null)
    {
        e.Command.Parameters[8].Value = Session["StaffID"].ToString().Trim();
    }
}

But why should I have to do this?

I would have thought the StaffID should have loaded automatically as per the SessionParameter request in my ASP page.

Like I said, I am not sure how SessionParameters (or Session variables work) in general, so if someone could please explain why I have to use codebehind to get this to work, that would be appreciated?

Thank you

1 Answer 1

1

The Session object is in fact an IDictionary<string, object>, which is persisted for you. It is persisted per-session. When a new session begins, the client receives a cookie from the server with a unique session identifier.

On each subsequent request the client sends the cookie to the server and in the global.asax the Session dictionary is repopulated. So let's say that in a code-behind you do Session[UserId] when the user authenticates, on each subsequent request you could do var UserId = Session[UserId] and as long as the session is "alive", you will receive the value you stored.

Restoring values from session as you have done is very logical. Since HTTP is state-less, Session is one of the mechanisms for simulating statefulness.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Elad. So what you're saying is that restoring my session variable as I did above in code-behind is actually the only way to go, hence it's logical and acceptable?
Yes, this usage is a big part of the WebForms story. This is exactly what session was designed for.
Thanks Elad. Your answer is it then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.