我想用几个dropdownlist实现对数据库的查询,程序如下:
        Dim sql, sql0 As String
        sql = "select * from kucun where huowu<>''"
        If DropDownList1.SelectedValue <> "" Then
            sql0 = " and  huowu='" & DropDownList1.SelectedValue & "'"
        End If
        If DropDownList2.SelectedValue <> "" Then
            sql0 = sql0 & "and  xinghao='" & DropDownList2.SelectedValue & "' "
        End If
        if DropDownList3.SelectedValue <> "" Then
            sql0 = sql0 & " and gongyingshang='" & DropDownList3.SelectedValue & "' "
        End If
               sql = sql & sql0
        Dim sqlcon As New SqlConnection("server=i;uid=sa;pwd=as;database=sheji")
        sqlcon.Open()
        Dim ds As New DataSet
        Dim sqld As New SqlDataAdapter
        sqld.SelectCommand = New SqlCommand(sql, sqlcon)
        sqld.Fill(ds, "sql")
        datagrid.DataSource = ds.Tables("sql")
        datagrid.DataBind()
        sqlcon.Close()
        sqlcon = Nothing
这里是用了3个dropdownlist,对dropdownlist所绑定的数据进行筛选后用button触发事件,
但是这样查询不出数据来,只用1个dropdownlist一切正常,请高手帮忙找下原因

解决方案 »

  1.   

    程序上没问题,
    sql = "select * from kucun where huowu<>''"
            If DropDownList1.SelectedValue <> "" Then
                sql0 = " and  huowu='" & DropDownList1.SelectedValue & "'"
            End If
    这里是不是有问题,
    where huowu<>''   和 and  huowu='" & DropDownList1.SelectedValue
      

  2.   

    最好是SQL语句到查询分析器里测试一下。就很清楚了
      

  3.   

    sql = "select * from kucun where huowu<>''"
            If DropDownList1.SelectedValue <> "" Then
                sql0 = " and  huowu='" & DropDownList1.SelectedValue & "'"
            End If
            If DropDownList2.SelectedValue <> "" Then
                sql0 = sql0 & " and  xinghao='" & DropDownList2.SelectedValue & "' "
            End If
            if DropDownList3.SelectedValue <> "" Then
                sql0 = sql0 & " and gongyingshang='" & DropDownList3.SelectedValue & "' "
            End If
            sql = sql & sql0你运行时把这个SQL输出,拿到查询分析器来看看了.
      

  4.   

    把输出的sql语句贴出来看下 If DropDownList2.SelectedValue <> "" Then
                sql0 = sql0 & "and  xinghao='" & DropDownList2.SelectedValue & "' "
    End If
    这里注意空格
      

  5.   

    DropDownList1.SelectedValue "泡沫      " String
    DropDownList2.SelectedValue "          " String
    sql "select * from kucun where huowu<>''" String
    sql0 Nothing String
    是这个么?
      

  6.   

    还是这个啊:
    服务器: 消息 170,级别 15,状态 1,行 1
    第 1 行: '=' 附近有语法错误。
    服务器: 消息 1038,级别 15,状态 1,行 2
    不能使用空白的对象或列名。如果必要,请使用一个空格。
    服务器: 消息 156,级别 15,状态 1,行 2
    在关键字 'Then' 附近有语法错误。
    服务器: 消息 1038,级别 15,状态 1,行 5
    不能使用空白的对象或列名。如果必要,请使用一个空格。
    服务器: 消息 156,级别 15,状态 1,行 5
    在关键字 'Then' 附近有语法错误。
    服务器: 消息 1038,级别 15,状态 1,行 8
    不能使用空白的对象或列名。如果必要,请使用一个空格。
    服务器: 消息 156,级别 15,状态 1,行 8
    在关键字 'Then' 附近有语法错误。
    服务器: 消息 1038,级别 15,状态 1,行 11
    不能使用空白的对象或列名。如果必要,请使用一个空格。
    服务器: 消息 156,级别 15,状态 1,行 11
    在关键字 'Then' 附近有语法错误。
    服务器: 消息 10
    38,级别 15,状态 1,行 14
    不能使用空白的对象或列名。如果必要,请使用一个空格。
    服务器: 消息 156,级别 15,状态 1,行 14
    在关键字 'Then' 附近有语法错误。
    服务器: 消息 170,级别 15,状态 1,行 17
    第 17 行: 'sql0' 附近有语法错误。
      

  7.   

    Dim sql, sql0 As String
    sql0=""
    sql = "select * from kucun where huowu<>''"
            If DropDownList1.SelectedValue <> "" Then
                sql0 = " and huowu='" & DropDownList1.SelectedValue & "'"
            End If
            If DropDownList2.SelectedValue <> "" Then
                sql0 = sql0 & " and xinghao='" & DropDownList2.SelectedValue & "'"
            End If
            if DropDownList3.SelectedValue <> "" Then
                sql0 = sql0 & " and gongyingshang='" & DropDownList3.SelectedValue & "'"
            End If
    sql = sql & sql0
      

  8.   

    DropDownList3.SelectedValue.Trim()
    vb不知道怎么写
      

  9.   

    程序上没问题,
    sql = "select * from kucun where huowu<>''"
            If DropDownList1.SelectedValue <> "" Then
                sql0 = " and  huowu='" & DropDownList1.SelectedValue & "'"
            End If
    这里是不是有问题,
    where huowu<>''   和 and  huowu='" & DropDownList1.SelectedValue同是huowu  就 用 or 别用 and
      

  10.   

    还是不行
    空格加了
    sql也该了
    sql = "select * from kucun where id<>'3'"
    sql0=“”也初始化了还不行
      

  11.   

    与我设计的不一样
    我本想DropDownList.SelectedValue的值为空的时候不对相应的数据筛选 
    但是现在DropDownList.SelectedValue的值为空就加上筛选条件DropDownList.SelectedValue=""
      

  12.   

    你做一个简单的SQL语句输出,判断一下SQL语句是否正确!