user1270384
user1270384

Reputation: 721

dropdownlist selected value

This may seem very trivial, I used to know how to do this at one time but for some reason I can't seem to get my head around it this time:

I have two tables: Hotel and HotelRooms The Hotel table has HotelID and other hotel details and the HotelID is found in HotelRooms with different roomtypes, each roomtype has a description. One Hotel can have many rooms types.

I have a DropDownList that contains HotelID and RoomIDs. HotelID comes from a session variable. HotelID is the value for the DropDownList; but the list displaysRoomType. WhenRoomtypesare selected I want to display aGridView` with the hotel room details like description, price etc....

I can't do this because the value for my DropDownList is HotelID which is mapped to the Session ID. How do I get the HotelRoom details from the selected value of the DropDownList, please?

Update:

Code for Gridview:

        string intResortID = Request.QueryString("intResortID ")
        string strRoomType = DropDownList2.SelectedValue;
        string connStr = ConfigurationManager.ConnectionStrings["bdsConnectionString"].ConnectionString;
        SqlConnection Con = new SqlConnection(connStr);
        SqlDataAdapter sdr = new SqlDataAdapter("SELECT TOP (100) PERCENT tblAvail.dtm, tblResortsRooms.strRoomType, tblResortsRooms.strDescription, tblAvail.intQty, tblAvail.curPrice, tblAvail.intResortID, tblResortsRooms.intWSCode FROM tblAvailable INNER JOIN tblResortsRooms ON tblAvail.intResortID = tblResortsRooms.intResortID AND tblAvail.strRoomType = tblResortsRooms.strRoomType WHERE (tblResortsRooms.curRecRate > 0) AND (tblAvail.intResortID = @intResortID) AND (tblAvail.strRoomType = @strRoomType) AND (tblAvailable.dtm >= { fn CURDATE() }) ORDER BY tblResortsRooms.strRoomType",Con);
        SqlParameter ResID = new SqlParameter("@intResortID", intResortID);
        SqlParameter RoomType = new SqlParameter("@strRoomType", strRoomType);
        sdr.SelectCommand.Parameters.Add(ResID);
        sdr.SelectCommand.Parameters.Add(RoomType);

      <asp:DropDownList ID="DropDownList2" runat="server" 
        DataSourceID="SqlDataSource2" DataTextField="strRoomType" 
        DataValueField="intResortID" 
        onselectedindexchanged="DropDownList2_SelectedIndexChanged" 
        AutoPostBack="True">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
       SelectCommand="SELECT [intResortID], [strRoomType] FROM [tblResortsRooms] WHERE ([intResortID] = @intResortID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="intResortID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

Upvotes: 0

Views: 12448

Answers (3)

johnd
johnd

Reputation: 1

Sounds like you need a new ID (Primary Key) in your HotelRooms table which uniquely identifies each room. If you had that, you could use that as the value for the dropdown and the problem goes away.

Upvotes: 0

Trisped
Trisped

Reputation: 6005

'DropDownList1.SelectedValue' should give you the value for @intResortID.
'DropDownList2.SelectedValue' should give you the value for @strRoomType.

DropDownList2_SelectedIndexChanged should run the query by filling something (like a DataTable).
The filled DataTable should be bound to your GridView.

Try something like the following in your DropDownList2_SelectedIndexChanged method:

string intResortID = Request.QueryString("intResortID ");
string strRoomType = DropDownList2.SelectedValue;
string connStr = ConfigurationManager.ConnectionStrings["bdsConnectionString"].ConnectionString;
SqlConnection Con = new SqlConnection(connStr);
SqlDataAdapter sdr = new SqlDataAdapter("SELECT TOP (100) PERCENT tblAvail.dtm, tblResortsRooms.strRoomType, tblResortsRooms.strDescription, tblAvail.intQty, tblAvail.curPrice, tblAvail.intResortID, tblResortsRooms.intWSCode FROM tblAvailable INNER JOIN tblResortsRooms ON tblAvail.intResortID = tblResortsRooms.intResortID AND tblAvail.strRoomType = tblResortsRooms.strRoomType WHERE (tblResortsRooms.curRecRate > 0) AND (tblAvail.intResortID = @intResortID) AND (tblAvail.strRoomType = @strRoomType) AND (tblAvailable.dtm >= { fn CURDATE() }) ORDER BY tblResortsRooms.strRoomType", Con);
SqlParameter ResID = new SqlParameter("@intResortID", intResortID);
SqlParameter RoomType = new SqlParameter("@strRoomType", strRoomType);
sdr.SelectCommand.Parameters.Add(ResID);
sdr.SelectCommand.Parameters.Add(RoomType);
DataTable results = new DataTable();

sdr.Fill(results);

resultsGridView.DataSource = results;   //Assuming resultsGridView is the name of the GridView on your ASPX page.
resultsGridView.DataBind();

Upvotes: 2

angus
angus

Reputation: 690

Try SelectedItem.Text of the dropdownlist.

Upvotes: 2

Related Questions