0

I want to store multiple pieces of data from two pages in session state and display them on a third page in separate labels. I can retrieve data from textboxes from the first page but my program then stops when I try to retrieve selected value from drop down list(connected to database) on second page from session state.

This is code behind from first page. It works ok and I can display these elements on third page labels.

public partial class booking : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnBookingContinue_Click(object sender, EventArgs e)
    {
        Session["firstName"] = tbxFirstName.Text;
        Session["lastName"] = tbxLastName.Text;
        Session["country"] = tbxCountry.Text;
        Session["phone"] = tbxPhone.Text;
        Session["email"] = tbxEmail.Text;

        Server.Transfer("~/booking2.aspx");
    }
}

This is from the second page showing the elements I have in place and dataconnection.

<ul class="bookingForm">
                <li>
                    <h2>Booking Form:</h2>
                </li>
                <li>
                    <asp:Label ID="lblChoosePackage" runat="server" Text="Choose Package:" CssClass="bookinglabel"></asp:Label>
                    <asp:DropDownList ID="ddlChoosepackage" runat="server" DataSourceID="ChoosePackageList" DataTextField="Title" DataValueField="Title" CssClass="bookingdropdown" OnSelectedIndexChanged="ddlChoosepackage_SelectedIndexChanged"></asp:DropDownList>
                    <asp:SqlDataSource ID="ChoosePackageList" runat="server" ConnectionString="<%$ ConnectionStrings:FlorianopoliSurfConnection %>" SelectCommand="SELECT [Title] FROM [Package] ORDER BY [PackageID]"></asp:SqlDataSource>
                </li>
                <li>
                    <asp:Label ID="lblArrivalDate" runat="server" Text="Arrival Date:" CssClass="bookinglabel"></asp:Label>
                    <asp:TextBox ID="tbxArrivaldate" runat="server" CssClass="bookingtextbox"></asp:TextBox>
                </li>
                <li>
                    <asp:Label ID="lblPeople" runat="server" Text="Number Of People:" CssClass="bookinglabel"></asp:Label>
                    <asp:TextBox ID="tbxPeople" runat="server" CssClass="bookingtextbox"></asp:TextBox>
                </li>
                <li>
                    <asp:Label ID="lblDeparture" runat="server" Text="Departure Location:" CssClass="bookinglabel"></asp:Label>
                    <asp:DropDownList ID="ddlDeparture" runat="server" CssClass="bookingdropdown">
                        <asp:ListItem Selected="True">Dublin</asp:ListItem>
                        <asp:ListItem>Cork</asp:ListItem>
                        <asp:ListItem>Shannon</asp:ListItem>
                        <asp:ListItem>Belfast</asp:ListItem>
                    </asp:DropDownList>
                </li>
                <li>
                    <asp:Label ID="lblCreditCard" runat="server" Text="Credit Card:" CssClass="bookinglabel"></asp:Label>
                    <asp:TextBox ID="tbxCreditCard" runat="server" CssClass="bookingtextbox"></asp:TextBox>
                </li>
                <li>
                    <asp:Button ID="btnContinueToSummary" runat="server" Text="Continue" PostBackUrl="~/confirmBooking.aspx" OnClick="btnContinueToSummary_Click" />
                </li>
            </ul>

This is the code behind from second page.

public partial class booking2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlChoosepackage.SelectedIndex = 0;
        }
    }

    protected void ddlChoosepackage_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlChoosepackage.SelectedIndex != 0)
        {
            Session["choosePackage"] = ddlChoosepackage.SelectedItem.Value;
        }

    }

    protected void btnContinueToSummary_Click(object sender, EventArgs e)
    {
        //Session["choosePackage"] = ddlChoosepackage.SelectedItem;
        Session["arrivalDate"] = tbxArrivaldate.Text;
        Session["numOfPeople"] = tbxPeople.Text;
        Session["departure"] = ddlDeparture.SelectedItem;
        Session["creditCard"] = tbxCreditCard.Text;

        Response.Redirect("~/confirmBooking.aspx");
    }
}

This is code behind from third page. The line I get caught on is:

string package = (string)Session["choosePackage"];

I get the error:

Object reference not set to an instance of an object.

So I seem to be getting a null value from the drop down list or from the session variable of the drop down list.

There is a rookie mistake here somewhere no doubt.

public partial class confirmBooking : System.Web.UI.Page
{
    public void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["CurrencySelection"] != null)
        {
            HttpCookie cookie = Request.Cookies["CurrencySelection"];
            ddlChooseCurrency.SelectedItem.Text = Server.HtmlEncode(cookie.Value);
        }

        string firstName = (string)Session["firstName"];
        string lastName = (string)Session["lastName"];
        string country = (string)Session["country"];
        string phoneNumber = (string)Session["phone"];
        string email = (string)Session["email"];

        string package = (string)Session["choosePackage"];
        string arrivalDate = (string)Session["arrivalDate"];
        string numOfPeople = (string)Session["numOfPeople"];
        string departure = (string)Session["departure"];
        string creditCard = (string)Session["creditCard"];

        lblSummaryNameDisplay.Text = string.Format("{0} {1}", firstName, lastName);
        lblSummaryCountryDisplay.Text = string.Format("{0}", country);
        lblSummaryPhoneDisplay.Text = string.Format("{0}", phoneNumber);
        lblSummaryEmailDisplay.Text = string.Format("{0}", email);

        lblSummaryPackageDisplay.Text = package.ToString();
        lblSummaryArrivalDateDisplay.Text = arrivalDate.ToString();
        lblSummaryPeopleDisplay.Text = numOfPeople.ToString();
        lblSummaryDepartureDisplay.Text = departure.ToString();
        lblSummaryCardDisplay.Text = creditCard.ToString();

    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

1

I believe the problem is that the SelectedIndexChanged event doesn't fire when you set SelectedIndex on Page_Load. I had a similar issue before, try adjusting your Page_Load event...

        if (!IsPostBack)
        {
            ddlChoosepackage.DataBind();
            ddlChoosepackage.SelectedIndex = 0;
        }
TestWell
  • 734
  • 10
  • 19
  • Thank you for responding. I have tried different variations of SelectedItem.Text and SelectedValue but nothing seems to work yet. – user3723134 May 22 '15 at 16:03
  • Thank you TestWell I have implemented your suggestion but after putting in the breakpoint it shows that my ddlChoosepackage.SelectedIndex is -1 even after the if statement. – user3723134 May 22 '15 at 16:06
  • I believe the SelectedIndex cannot be anything other than -1 until the page is physically drawn. Would `Session["choosePackage"] = "";` in your load event help anything? The user would have to actually click a selection, however. Where and how are you populating `ddlChoosepackage`? – TestWell May 22 '15 at 16:31
  • Hi, with the above code I get an error of: Index was out of range. Must be non-negative and less than the size of the collection. My selectedIndex stays at -1 and therefore seems to not select anything from the drop down. – user3723134 May 22 '15 at 16:40
  • Thank you, I didn't see your last message prior to replying. I am populating the drop down from database using the SqlDataSource in the second code snippet above (the html of second page). That was another thing I thought it could be.. should I be (re)populating the drop down on page load of page two even thought it's connected to database? – user3723134 May 22 '15 at 16:47
  • Try putting `ddlChoosepackage.DataBind();` above `ddlChoosepackage.SelectedIndex = 0;` in the `Page_Load` event. – TestWell May 22 '15 at 16:58
  • Thank you very much. That solved the ddlChoosepackage issue. I am now getting the same error: 'Object reference not set to an instance of an object' for the arrivalDate. I need to look into it a little bit more. – user3723134 May 22 '15 at 17:14