Gavin
Gavin

Reputation: 437

Convert dropdownlist value to string

I have two tables in my db, Building and Rooms. I am using two dropdownlists. The aim is to have the first dropdownlist, ddlBuilding, for selecting a building name. When the name is selected I want to query the database to find only room numbers that correspond to that building and return them in the room dropdownlist, ddlRoomNumber.
For example:

Building A has rooms 1, 2 and 3.
Building B has rooms 4, 5 and 6.

If the user selects building A from ddlBuilding they should only be presented with rooms 1, 2 and 3 in ddlRoomNumber

I have set up a query to do this but I am getting an error to say that dropdownlist cannot be converted to string. The code in question is:

  Protected Sub txtRoomNumber_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRoomNumber.TextChanged

    Dim db As New OrionDataClassesDataContext

    Dim roomNumber = (From build In db.Buildings Join rm In db.Rooms On build.Building_code Equals rm.Building_code
                      Where build.Building_name = ddlBuilding
                      Select rm.Room_Number).distint

    ddlRoomNumber.DataSource = roomNumber
    ddlRoomNumber.DataBind()

End Sub

It is the line where build.Building_name = ddlBuilding that is causing the error. Any thoughts on how I can pull the string out of the dropdownlist and use it to validate my query.

I have changed the code to the follow after some responses and research. It is still not working. ddlRoomNumber is not binding to my query.

Protected Sub ddlRoomNumber_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRoomNumber.SelectedIndexChanged

    Dim db As New OrionDataClassesDataContext


    ddlRoomNumber.DataSource = (From build In db.Buildings Join rm In db.Rooms On build.Building_code Equals rm.Building_code
                                Where (build.Building_name = ddlBuilding.SelectedItem.Text)
                                Select rm.Room_Number, rm.Room_ID)

    ddlRoomNumber.DataTextField = "Room_Number"
    ddlRoomNumber.DataValueField = "Room_ID"
    ddlRoomNumber.DataBind()
End Sub

This is the asp page. Is there any way to type in the linq datasource to point it to my vb query.

<asp:DropDownList ID="ddlRoomNumber" runat="server" AutoPostBack="True">
                           </asp:dropdownlist>

Upvotes: 0

Views: 2766

Answers (1)

Ece Ko&#231;
Ece Ko&#231;

Reputation: 11

you can use ddlBuilding.SelectedItem.Text instead of ddlBuilding

Upvotes: 1

Related Questions