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