Reputation: 75
I'm trying to pass the value of a dropdownlist to a session variable, and then cast this value into a text label for a review page. This value then needs to be passed to an sql table (not an issue). Where the issue lies is every time I attempt to call the value (or index, I've tried both) of the dropdownlist in a label I get a null exception. Here's my code for one of the dropdowns on the first page, and the attempt to bind it from the created session on the next: FirstPage.aspx
<asp:DropDownList ID="ddlInnoc" runat="server">
<asp:ListItem Value="0">No</asp:ListItem>
<asp:ListItem Value="1">Yes</asp:ListItem>
</asp:DropDownList>
FirstPage.aspx.cs
Session["Jabs"] = ddlInnoc.SelectedIndex;
SecondPage.aspx
<asp:Label ID="lblJabs" runat="server"></asp:Label>
SecondPage.aspx.cs
lblJabs.Text = Session["Jabs"].ToString();
Please somebody tell me I'm just being stupid! The exception I receive is as follows:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 11: {
Line 12: //Capture session values from previous page and send to relevant labels
Line 13: **lblGroup.Text = Session["NoInGroup"].ToString();**
Line 14: lblFirstName.Text = Session["FirstName"].ToString();
Line 15: lblMiddleName.Text = Session["MiddleName"].ToString();
Whats kinda strange is that I can successful grab the SelectedIndex of a different DropDownList on a different page. Its making me tear my hair out!
Upvotes: 1
Views: 4139
Reputation: 5665
Make sure you are doing a postback before opening the second page.
Session["Jabs"] = ddlInnoc.SelectedIndex;
should be in the selectedindexchanged method and autpostback should be set on the dropdownlist eg:
<asp:DropDownList ID="ddlInnoc" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlInnoc_SelectedIndexChanged">
<asp:ListItem Value="0">No</asp:ListItem>
<asp:ListItem Value="1">Yes</asp:ListItem>
</asp:DropDownList>
and the method looks like this:
protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
{
//if you want "0" or "1"
Session["Jabs"] = ddlInnoc.SelectedIndex;
//if you want "Yes" or "No"
//Session["Jabs"] = ddlInnoc.SelectedItem.Text;
//also if you want "0" or "1"
//Session["Jabs"] = ddlInnoc.SelectedValue;
}
In most cases you probably don't want the Selected Index, as that is just the order of the item in the dropdownlist
To make sure that the session is always filled you can set it on Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["Jabs"] = ddlInnoc.SelectedIndex;
}
}
Also: When using Session you should always be aware that Sessions can expire and so you should ALWAYS check for NULL before using eg:
SecondPage.aspx.cs
lblJabs.Text = (Session["Jabs"] == null ? "Default Value" : Session["Jabs"].ToString());
Upvotes: 6