我的数据库中有两个表,Building
和Rooms
。我正在使用两个下拉列表。目的是获得第一个下拉列表ddlBuilding
,用于选择建筑物名称。选择名称后,我想查询数据库,只查找与该建筑物对应的房间号,并在房间下拉列表ddlRoomNumber
中返回。
例如:
A楼有1号房,2号房和3号房 B楼有4号,5号和6号房间。
如果用户从ddlBuilding
中选择了建筑物A,则只能在ddlRoomNumber
我已经设置了一个查询来执行此操作但我收到一个错误,说dropdownlist无法转换为字符串。有问题的代码是:
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
导致错误的是行where build.Building_name = ddlBuilding
。关于如何将字符串拉出下拉列表并使用它来验证我的查询的任何想法。
在一些回复和研究之后,我已将代码更改为以下内容。它仍然无法正常工作。 ddlRoomNumber
未绑定到我的查询。
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
这是asp页面。有没有办法输入linq数据源来指向我的vb查询。
<asp:DropDownList ID="ddlRoomNumber" runat="server" AutoPostBack="True">
</asp:dropdownlist>
答案 0 :(得分:1)
您可以使用ddlBuilding.SelectedItem.Text
代替ddlBuilding