2013-08-30 84 views
0

我希望我的主題標題很有意義。如何將可用記錄的數量添加到dropdownlist選項?

我們正在建立一個汽車經銷商的網絡應用程序,讓潛在的汽車買家可以確定哪種汽車適合他們的預算和需求。

因此,我們有汽車製造,汽車模型,汽車狀況,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 [email protected]" 
      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 [email protected]" 
      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 

這工作正常。

我們想追加每輛汽車製造的可用記錄數。

例如,在汽車下拉列表的製作,我們有這樣的事:

馬自達 梅塞德斯奔馳 豐田, 等

我們將讓用戶知道特定的品牌如何可以像:

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

如何爲特定品牌的汽車添加可用數量的汽車?

回答

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來使代碼更好。或者甚至更好地把最終代碼放到存儲過程中,而不是調用這個過程。

另外一個說明 - 儘量減少重複的代碼。希望能幫助到你。

+0

@ivansivak,感謝,但我收到此錯誤: '轉換爲varchar值「)」爲數據類型int.'時轉換失敗 –

1

您需要使用CONVERT()函數。

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