我正在尝试将数据从3个不同的下拉列表绑定到网格视图。
用户可以从1个DDL或2个DDL或3个DDL中选择1个值,然后将生成一个SQL查询以从数据库检索数据并将其绑定到我的GV。
我所需要的就是一个完美的SQL查询或一个可以接受从D1,D2,D3中选择的值的函数,并考虑到用户可以从D1,D2,D3或DDL的倍数中进行选择
<form id="form1" runat="server">
<div style="height: 265px">
welcome to this page
<br />
here u r allowed to search for a car with available attributes
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MODELCODE" ShowHeaderWhenEmpty="True">
<Columns>
<asp:BoundField DataField="MODELCODE" HeaderText="MODELCODE" InsertVisible="False" ReadOnly="True" SortExpression="MODELCODE" />
<asp:BoundField DataField="Manufacturercountry" HeaderText="Manufacturercountry" SortExpression="Manufacturercountry" />
<asp:BoundField DataField="modelname" HeaderText="modelname" SortExpression="modelname" />
<asp:BoundField DataField="modelcolor" HeaderText="modelcolor" SortExpression="modelcolor" />
<asp:BoundField DataField="dailyhirringrate" HeaderText="dailyhirringrate" SortExpression="dailyhirringrate" />
<asp:BoundField DataField="sellingprice" HeaderText="sellingprice" SortExpression="sellingprice" />
<asp:BoundField DataField="numberofcars" HeaderText="numberofcars" SortExpression="numberofcars" />
<asp:BoundField DataField="currentmielage" HeaderText="currentmielage" SortExpression="currentmielage" />
<asp:BoundField DataField="enginesize" HeaderText="enginesize" SortExpression="enginesize" />
<asp:BoundField DataField="liecensenumber" HeaderText="liecensenumber" SortExpression="liecensenumber" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource9" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT * FROM [CARMODEL] WHERE (([Manufacturercountry] = @Manufacturercountry) AND ([modelname] = @modelname))">
<SelectParameters>
<asp:ControlParameter ControlID="country" DefaultValue="""" Name="Manufacturercountry" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownList2" DefaultValue="""" Name="modelname" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="buy a car" />
<asp:Button ID="Button2" runat="server" Text="rent a car" />
<br />
<br />
</div>
<br />
country
<asp:DropDownList ID="country" runat="server" DataSourceID="SqlDataSource1" DataTextField="Manufacturercountry" DataValueField="Manufacturercountry" AppendDataBoundItems ="true">
<asp:listitem selected="True" text="" value=""></asp:listitem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT DISTINCT [Manufacturercountry] FROM [CARMODEL]"></asp:SqlDataSource>
modelname :
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="modelname" DataValueField="modelname">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT DISTINCT [modelname] FROM [CARMODEL]"></asp:SqlDataSource>
color
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3" DataTextField="modelcolor" DataValueField="modelcolor" style="margin-left: 12px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT [modelcolor] FROM [CARMODEL]"></asp:SqlDataSource>
daily H.rate :
<asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource4" DataTextField="dailyhirringrate" DataValueField="dailyhirringrate">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT [dailyhirringrate] FROM [CARMODEL]"></asp:SqlDataSource>
<br />
<br />
<br />
selling price
<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="SqlDataSource5" DataTextField="sellingprice" DataValueField="sellingprice">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT [sellingprice] FROM [CARMODEL]"></asp:SqlDataSource>
E.size
<asp:DropDownList ID="DropDownList7" runat="server" DataSourceID="SqlDataSource6" DataTextField="enginesize" DataValueField="enginesize">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT [enginesize] FROM [CARMODEL]"></asp:SqlDataSource>
mileage:
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SqlDataSource7" DataTextField="currentmielage" DataValueField="currentmielage">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT [currentmielage] FROM [CARMODEL]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList8" runat="server" DataSourceID="SqlDataSource8" DataTextField="liecensenumber" DataValueField="liecensenumber">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource8" runat="server" ConnectionString="<%$ ConnectionStrings:MALAK1ConnectionString %>" SelectCommand="SELECT [liecensenumber] FROM [CARMODEL]"></asp:SqlDataSource>
<br />
<br />
<asp:Button ID="Button3" runat="server" Text="check for cars" />
<br />
<asp:Button ID="Button4" runat="server" Text="Button" />
<asp:Button ID="Button5" runat="server" style="margin-left: 365px" Text="Button5" />
<asp:Button ID="Button6" runat="server" style="margin-left: 134px" Text="Button" Width="109px" />
</form>
Protected Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
If country.SelectedValue = "" Then
conn = obj.Connection
Dim cmd As New SqlCommand("select * from CARMODEL where modelname = @mdn and modelcolor = @mdc and dailyhirringrate = @dhr and sellingprice = @spr and liecensenumber = @ln", conn)
cmd.Parameters.AddWithValue("@mdn", DropDownList2.SelectedValue)
cmd.Parameters.AddWithValue("@mdc", DropDownList3.SelectedValue)
cmd.Parameters.AddWithValue("@dhr", DropDownList4.SelectedValue)
cmd.Parameters.AddWithValue("@spr", DropDownList5.SelectedValue)
cmd.Parameters.AddWithValue("@ln", DropDownList8.SelectedValue)
Dim dst As New DataSet()
Dim dt As New DataTable("frmcntry")
cmd.ExecuteNonQuery()
cmd.CommandType = CommandType.Text
Dim dadapt As New SqlDataAdapter(cmd)
dadapt.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
答案 0 :(得分:0)
键将是与LINQ TO SQL结合在一起的实体数据模型
如果使用EDM将程序设置到数据库中,那将变得非常容易。
示例:
using container as new yourEDMContainer
Dim yourquery as IQueriable(Of yourdatatableelementclass) = container.yourtableSet
If DDL1wasselected Then
yourquery = yourquery.where(Function(v) v.thevalue1tocompare = DDL1value)
End If
If DDL2wasselected Then
yourquery = yourquery.where(Function(v) v.thevalue2tocompare = DDL2value)
End If
If DDL3wasselected Then
yourquery = yourquery.where(Function(v) v.thevalue3tocompare = DDL3value)
End If
Dim your results = yourquery.ToList() 'only at this point the query is resolved and a call to the datbase will be made.
end using