【问题标题】:Is it possible to add a third parameter to an autocomplete textbox?是否可以将第三个参数添加到自动完成文本框?
【发布时间】:2013-07-07 02:34:26
【问题描述】:

我正在使用下面的代码使用姓氏和选定状态填充 ajax 自动完成文本框。因此,如果用户键入“Jones”,它将填充姓氏类似于“Jones”的所有行。是否可以添加另一个参数以便用户可以键入“Jones, John”?所以它也会查找 ", John" ?谢谢!

Public Function GetCompletionList(prefixText As String, count As Integer, ByVal contextKey As String) As String()
    Try
        Dim Con As SqlConnection
        Dim cmd As SqlCommand
        Con = New SqlConnection
        Dim test As String
        test = contextKey
        Con.ConnectionString = ""
        Con.Open()

        cmd = New SqlCommand
        cmd.Connection = Con
        'cmd.CommandText = "SELECT NPI, [Entity Type Code], [Provider Last Name (Legal Name)], [Provider First Name], [Provider Business Mailing Address City Name] FROM NPIData WHERE [Provider Last Name (Legal Name)] LIKE @Provider + '%' AND [Provider Business Mailing Address City Name] = @State"
        cmd.CommandText = "SELECT NPI, [Entity Type Code], [Provider Last Name (Legal Name)], [Provider First Name],[Provider First Line Business Mailing Address], [Provider Business Mailing Address City Name], [Provider Business Mailing Address State Name], [Provider Business Mailing Address Postal Code] FROM NPIData WHERE     ([Provider Business Mailing Address State Name] = @State) AND ([Provider Last Name (Legal Name)] LIKE N'%' + @Provider + N'%') ORDER BY [Provider First Name]"
        cmd.Parameters.AddWithValue("@Provider", prefixText)
        cmd.Parameters.AddWithValue("@State", contextKey)
        Dim customers As List(Of String) = New List(Of String)
        Dim reader As SqlDataReader = cmd.ExecuteReader()


        While reader.Read
            customers.Add(reader("Provider Last Name (Legal Name)").ToString + ", " + reader("Provider First Name").ToString + "   " + reader("Provider First Line Business Mailing Address").ToString + "  " + reader("Provider Business Mailing Address City Name").ToString + ", " + reader("Provider Business Mailing Address State Name").ToString + "  " + reader("Provider Business Mailing Address Postal Code").ToString + "  " + reader("NPI").ToString)

        End While


        Con.Close()

        Return customers.ToArray
    Catch ex As Exception

    End Try

End Function

【问题讨论】:

  • 只需用逗号分割输入并相应地调整您的查询。无需传递第三个参数。
  • 你想AND这两个值吗?换句话说,如果用户输入“Jones, John”,那么您期望它应该返回 Last Name = "Jones" 的所有行和 First Name = "John" 的所有行?
  • 好吧,我同意@Zia,但你必须在你的 UI 中明确表示逗号分隔的值适用于不同的列,如果这实际上是你想要的。它使用户界面复杂化。
  • 是的,我希望列表也按名字过滤,所以当他们输入“Jones, J”时,它将开始按所有以 J 开头的名字过滤
  • 我会这样做吗? AND ([Provider Last Name (Legal Name)] AND [Provider First Name] LIKE N'%' + @Provider + N'%') ORDER BY [Provider First Name]" 如何过滤掉","

标签: asp.net vb.net asp.net-ajax


【解决方案1】:

在您的GetCompletionList 函数中,您需要拆分传入的prefixText 字符串,如下所示:

' Split prefixText string by commas
Dim words As String() = prefixText.Split(New Char() {","c})

现在您可以通过引用字符串片段来调整您的SQL,如下所示:

words(0) = Last Name
words(1) = First Name

【讨论】:

  • 我将我的 SQL 语句更改为以下内容,但出现错误 An expression of non-boolean type specified in an context where a condition is expected, near ','.
  • cmd.CommandText = "SELECT NPI, [Entity Type Code], [Provider Last Name (Legal Name)], [Provider First Name],[Provider First Line Business Mailing Address], [Provider Business邮寄地址城市名称]、[提供者企业邮寄地址州名]、[提供者企业邮寄地址邮政编码] FROM NPIData WHERE ([提供者企业邮寄地址州名] = @State) AND ('" & words(0) & " ','" & words(1) & "'LIKE N'%' + @Provider + N'%') ORDER BY [Provider First Name]"
  • 您在使用words(0)words(1) 时的语法错误。我建议将 SQL 拆分为两个查询,并在尝试将它们组合成一个查询之前让结果正常工作。
  • 我还建议将其移至新问题,因为它现在是 SQL 问题而不是代码问题。如果对您有帮助,请随时接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多