【发布时间】:2018-10-08 21:14:24
【问题描述】:
我正在尝试将数据绑定到来自 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
【问题讨论】:
-
“我所需要的只是一个完美的 sqlquery 或一个函数”。我们不是来为您编写代码的。如果您不知道如何编写查询,那么您还没有做足够的研究。如果您确实有一个想法,然后做您认为合适的事情,如果它不起作用,则需要发布该代码并解释它的实际行为与您的预期有何不同。
标签: sql-server vb.net