Search code examples
c#asp.netsqldatasource

Calculate a value from drop down list that uses sqldatasource as data source


So basically i want to do calculation based on selected index in Dropdown list that uses sqldatasource as it sources and having trouble to view it on another page.I'm using session in the process.

Here's the coding for the Dropdown list's that is using sqldatasource :-

<asp:DropDownList ID="DdlMRoom" runat="server" DataSourceID="SqlDataSource2" DataTextField="Room" DataValueField="Price" Height="16px" Width="84px" Visible="False">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price] FROM [Meneur Hotel]"></asp:SqlDataSource>
<asp:DropDownList ID="DdlGRoom" runat="server" DataSourceID="SqlDataSource3" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [Gardenia Hotel]"></asp:SqlDataSource>
<asp:DropDownList ID="DdlARoom" runat="server" DataSourceID="SqlDataSource4" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [Al-Rashid Hotel]"></asp:SqlDataSource>
<asp:DropDownList ID="DdlPRoom" runat="server" DataSourceID="SqlDataSource5" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [Petra Sella Hotel]"></asp:SqlDataSource>
<asp:DropDownList ID="DdlTORoom" runat="server" DataSourceID="SqlDataSource6" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [The Olive Branch Hotel]"></asp:SqlDataSource>

Here's one of example of my calculation : -

int calculation()
{   
    int price=0;

    if(DdlMRoom.SelectedIndex==1)
    {
        int price;
        price = Convert.ToInt16(DdlMRoom.SelectedValue) * Convert.ToInt16(TxtPax.Text);
    }

    return price;

}

protected void Button1_Click(object sender, EventArgs e)
{     
    calculation().ToString();
    Session["Price"] = price;

    Response.Redirect("View.aspx);
}

Here's the coding on View.aspx & .cs : -

<asp:Label ID="lblPrice" runat="server"></asp:Label>

code-behind

protected void Page_Load(object sender, EventArgs e)
{
    lblPrice.Text = Session["Price"].ToString();  
}

i hope someone can answer this, i'm doing this for assignment and is due tomorrow. Will be terrific if any of you guys help me!!


Solution

  • this should work

    int calculation()
    {   
        int price=0;
    
        if(DdlMRoom.SelectedIndex==1)
        {
            price = Convert.ToInt16(DdlMRoom.SelectedValue) * Convert.ToInt16(TxtPax.Text);
        }
    
        return price;
    
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {     
        string price = calculation().ToString();
        Session["Price"] = price;
    
        Response.Redirect("View.aspx");
    }