如何将可用记录的数量附加到下拉列表选项?

时间:2013-08-30 13:59:59

标签: asp.net sql-server vb.net

我希望我的主题标题有意义。

我们正在建立一个汽车经销商网络应用程序,允许潜在的购车者确定哪种类型的汽车符合他们的预算和需求。

因此,我们有汽车制造,车型,车况,milage,价格范围等下拉列表。

下面的代码显示了汽车品牌的下拉列表。

当用户选择他/她喜欢的汽车时,与该品牌相关联的模型会自动加载到第二个下拉列表中。

    <form id="form1" runat="server">
    <div>
        <span style ="font-family:Arial">Select Make : </span> 
        <asp:DropDownList ID="ddlMake" runat="server" AutoPostBack = "true" OnSelectedIndexChanged="ddlMake_SelectedIndexChanged">
        <asp:ListItem Text = "------" Value = ""></asp:ListItem>
        </asp:DropDownList>
        <br /><br />
        <span style ="font-family:Arial">Select Model : </span> 
        <asp:DropDownList ID="ddlModel" runat="server" Enabled = "false" AutoPostBack = "true" OnSelectedIndexChanged="ddlModel_SelectedIndexChanged">
        <asp:ListItem Text = "------" Value = ""></asp:ListItem>
        </asp:DropDownList>
        <br /><br />
        <asp:button id="btnSave" runat="server" Text="Submit New Request"></asp:button><br /><br />
        <asp:Label ID="lblResults" runat="server" Text="" Font-Names = "Arial"></asp:Label>        
    </div>
    </form>



     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            If Not IsPostBack Then
                ddlMake.AppendDataBoundItems = True
                Dim strConnString As [String] = ConfigurationManager _
                 .ConnectionStrings("amsconstr").ConnectionString
                Dim strQuery As [String] = "select vehicleId, Make from MakeModel"
                Dim con As New SqlConnection(strConnString)
                Dim cmd As New SqlCommand()
                cmd.CommandType = CommandType.Text
                cmd.CommandText = strQuery
                cmd.Connection = con
                Try
                    con.Open()
                    ddlMake.DataSource = cmd.ExecuteReader()
                    ddlMake.DataTextField = "Make"
                    ddlMake.DataValueField = "vehicleId"
                    ddlMake.DataBind()
                Catch ex As Exception
                    Throw ex
                Finally
                    con.Close()
                    con.Dispose()
                End Try
            End If
        End Sub
   Protected Sub ddlMake_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            ddlModel.Items.Clear()
            ddlModel.Items.Add(New ListItem("--Select Model--", ""))

            ddlModel.AppendDataBoundItems = True
            Dim strConnString As [String] = ConfigurationManager _
                       .ConnectionStrings("amsconstr").ConnectionString
            Dim strQuery As [String] = "select vehicleId Employee from MakeModel " _
                                        & "where vehicleId=@vehicleId"
            Dim con As New SqlConnection(strConnString)
            Dim cmd As New SqlCommand()
            cmd.Parameters.AddWithValue("@vehicleId", _
                                 ddlMake.SelectedItem.Value)
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            cmd.Connection = con
            Try
                con.Open()
                ddlModel.DataSource = cmd.ExecuteReader()
                ddlModel.DataTextField = "Model"
                ddlModel.DataValueField = "VehicleId"
                ddlModel.DataBind()
                If ddlModel.Items.Count > 1 Then
                    ddlModel.Enabled = True
                Else
                    ddlModel.Enabled = False
                End If
            Catch ex As Exception
                Throw ex
            Finally
                con.Close()
                con.Dispose()
            End Try
        End Sub
  Protected Sub ddlModel_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

            Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("conString").ConnectionString
            Dim strQuery As [String] = "select VehicleId, Model from MakeModel where VehicleId=@vehicleId"
            Dim con As New SqlConnection(strConnString)
            Dim cmd As New SqlCommand()
            cmd.Parameters.AddWithValue("@vehicleId", ddlModel.SelectedItem.Value)
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            cmd.Connection = con
            Try
                con.Open()
            Catch ex As Exception
                Throw ex
            Finally

                con.Close()
                con.Dispose()
            End Try
    End Sub

这很好用。

我们想追加每辆车的可用记录数量。

例如,在Make of a car下拉列表中,我们有类似的内容:

马自达 梅赛德斯奔驰 丰田, 等

我们希望用户知道特定品牌如何可用:

Mazda (10)
Mercedez Benz (2)
Toyota (7)

如何为该车的特定品牌添加可用的汽车数量?

2 个答案:

答案 0 :(得分:1)

您需要使用CONVERT()函数。

select VehicleId, (Model + '(' + CONVERT(NVARCHAR, (select count(*) from MakeModel B where a.ID=b.ID)) + ')') as Make from MakeModel A

答案 1 :(得分:0)

更改您的SQL语句,以便它返回您需要的所有内容。而不只是:

select VehicleId, Model from MakeModel.....

使用类似的东西:

select VehicleId, (Model + '(' + (select count(*) from MakeModel B where a.ID=b.ID) + ')') as Make from MakeModel A...

..这不好,但会奏效。使用SQL Server CTE来使代码更好。或者甚至更好地将最终代码放到存储过程中并调用此过程。

另外一个注意事项 - 尝试减少重复代码。希望它有所帮助。