Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql中使用AS时对DropDownList进行排序_Sql_Vb.net - Fatal编程技术网

在sql中使用AS时对DropDownList进行排序

在sql中使用AS时对DropDownList进行排序,sql,vb.net,Sql,Vb.net,我在整理下拉列表时遇到问题 我在这里尝试了使用DataView的几种不同方法,但无法使它们正常工作 这是我的代码,它创建了我希望按[姓氏]排序的下拉列表 Dim SqlQuery As String = "SELECT [First Name] + ' ' + [Last Name] + ' ' + [Email] + ' ' + [Phone] AS [FullInfo] FROM [Official]" con.Close() con.Open() Dim dt = New DataTa

我在整理下拉列表时遇到问题

我在这里尝试了使用DataView的几种不同方法,但无法使它们正常工作

这是我的代码,它创建了我希望按[姓氏]排序的下拉列表

Dim SqlQuery As String = "SELECT [First Name] + ' ' + [Last Name] + ' ' + [Email] + ' ' + [Phone] AS [FullInfo] FROM [Official]"

con.Close()
con.Open()

Dim dt = New DataTable
Using sqlconnecton = New SqlConnection(con.ConnectionString)
    Using da = New SqlDataAdapter(SqlQuery, con)
        da.Fill(dt)
    End Using
End Using

DropDownList1.DataSource = dt
DropDownList1.DataTextField = "FullInfo"
DropDownList1.DataValueField = "FullInfo"
DropDownList1.DataBind()
更新1

我不应该试图简化这个问题。我的查询是如何完成的,比我上面发布的要复杂得多。所以实际发生的是,这个查询位于For-Each循环中,每次通过循环都会找到一条记录,并将其添加到下拉列表中。由于查询是这样完成的,我不能按姓氏排序,因为一次只有一条记录被添加到下拉列表中

下面是正在发生的事情的完整代码:

 For Each row As DataRow In table.Rows 'Check each official for availibility and fill drop down list if needed



                    Dim col1 As Int32 = row.Field(Of Int32)(0)
                    Dim col1string As String = col1


                    'now get block info for each person
                    Dim SqlQuery2 As String = "Where ([Organization] = '" + Session("ID") + "' OR [Organization 2] = '" + Session("ID") + "' OR [Organization 3] = '" + Session("ID") + "') AND [ID] = '" + col1string + "'"
                    Dim NumOfBlocks As Integer = row.ItemArray.Count - 10
                    Dim strblocks As String = "Blocks"
                    Dim FirstChange As String = "No"
                    For i = 1 To NumOfBlocks
                        If i = NumOfBlocks Then 'Last one dont end with AND
                            Dim Num As String = i

                            If String.IsNullOrEmpty(row.Field(Of String)(strblocks + Num)) Then 'dont include
                            Else 'include data
                                If FirstChange = "Yes" Then 'We have an AND

                                    SqlQuery2 = SqlQuery2 & "" + strblocks + Num + " <> '" + Session("DateTxt") + "'"

                                Else 'We dont have an AND

                                    SqlQuery2 = SqlQuery2 & " AND " + strblocks + Num + " <> '" + Session("DateTxt") + "'"

                                End If

                            End If
                        Else
                            Dim Num As String = i

                            If String.IsNullOrEmpty(row.Field(Of String)(strblocks + Num)) Then 'dont include

                            Else 'include data
                                FirstChange = "Yes"
                                SqlQuery2 = SqlQuery2 & " AND " + strblocks + Num + " <> '" + Session("DateTxt") + "' And "
                            End If


                        End If

                    Next

                    'Trim String if it ends with just "and" so an error is not thrown in sql below
                    If SqlQuery2.EndsWith("And ") Then
                        SqlQuery2 = SqlQuery2.Substring(0, SqlQuery2.Length - 5)
                    Else

                    End If

                    con.Close()

                    Dim SqlQuery As String = "SELECT [First Name] + ' ' + [Last Name] + ' ' + [Email] + ' ' + [Phone] AS [FullInfo] FROM [Official]"


                    con.Close()
                    con.Open()
                    Dim dt = New DataTable
                    Using sqlconnecton = New SqlConnection(con.ConnectionString)
                        Using da = New SqlDataAdapter(SqlQuery & SqlQuery2, con)

                            da.Fill(dt)
                        End Using
                    End Using

                    DropDownList1.DataSource = dt
                    DropDownList1.DataTextField = "FullInfo"
                    DropDownList1.DataValueField = "FullInfo"
                    DropDownList1.DataBind()

                    DropDownList3.DataSource = dt
                    DropDownList3.DataTextField = "FullInfo"
                    DropDownList3.DataValueField = "FullInfo"
                    DropDownList3.DataBind()

                    DropDownList4.DataSource = dt
                    DropDownList4.DataTextField = "FullInfo"
                    DropDownList4.DataValueField = "FullInfo"
                    DropDownList4.DataBind()

                    DropDownList5.DataSource = dt
                    DropDownList5.DataTextField = "FullInfo"
                    DropDownList5.DataValueField = "FullInfo"
                    DropDownList5.DataBind()

                    DropDownList6.DataSource = dt
                    DropDownList6.DataTextField = "FullInfo"
                    DropDownList6.DataValueField = "FullInfo"
                    DropDownList6.DataBind()

                    DropDownList7.DataSource = dt
                    DropDownList7.DataTextField = "FullInfo"
                    DropDownList7.DataValueField = "FullInfo"
                    DropDownList7.DataBind()
                    con.Close()
                Next 
将每一行作为表中的DataRow。Rows'检查每个官员的可用性,如果需要,填写下拉列表
Dim col1 As Int32=行字段(属于Int32)(0)
Dim col1字符串作为字符串=col1
'现在获取每个人的区块信息
Dim SqlQuery2为String=“其中([Organization]='”+会话(“ID”)+”)或[Organization 2]='“+会话(“ID”)+””或[Organization 3]='“+会话(“ID”)+”)和[ID]='“+col1string+””
Dim NumOfBlocks As Integer=row.ItemArray.Count-10
将strblock变为String=“Blocks”
Dim FirstChange As String=“否”
对于i=1到numf块
如果i=numoblocks,那么最后一个不会以AND结尾
Dim Num作为字符串=i
如果String.IsNullOrEmpty(行.字段(字符串的)(strblocks+Num))则“不包括”
“其他”包括数据
如果FirstChange=“Yes”,则“我们有一个
SqlQuery2=SqlQuery2&“+strblocks+Num+”“+Session(“DateTxt”)+””
否则我们就没有一个
SqlQuery2=SqlQuery2&“和“+strblocks+Num+””“+Session(“DateTxt”)+””
如果结束
如果结束
其他的
Dim Num作为字符串=i
如果String.IsNullOrEmpty(行.字段(字符串的)(strblocks+Num))则“不包括”
“其他”包括数据
FirstChange=“是”
SqlQuery2=SqlQuery2&“和“+strblocks+Num+””“+Session(“DateTxt”)+”和”
如果结束
如果结束
下一个
'修剪字符串,如果它仅以“and”结尾,则不会在下面的sql中引发错误
如果SqlQuery2.EndsWith(“And”),则
SqlQuery2=SqlQuery2.Substring(0,SqlQuery2.Length-5)
其他的
如果结束
con.Close()
Dim SqlQuery As String=“从[Official]中选择[First Name]+'+'+[Last Name]+'++'+[Email]+'+'+[Phone]作为[FullInfo]。”
con.Close()
con.Open()
Dim dt=新数据表
使用sqlconnecton=newsqlconnection(con.ConnectionString)
使用da=newsqldataadapter(SqlQuery&SqlQuery2,con)
da.填充(dt)
终端使用
终端使用
DropDownList1.DataSource=dt
DropDownList1.DataTextField=“FullInfo”
DropDownList1.DataValueField=“FullInfo”
DropDownList1.DataBind()
DropDownList3.DataSource=dt
DropDownList3.DataTextField=“FullInfo”
DropDownList3.DataValueField=“FullInfo”
DropDownList3.DataBind()
DropDownList4.DataSource=dt
DropDownList4.DataTextField=“FullInfo”
DropDownList4.DataValueField=“FullInfo”
DropDownList4.DataBind()
DropDownList5.DataSource=dt
DropDownList5.DataTextField=“FullInfo”
DropDownList5.DataValueField=“FullInfo”
DropDownList5.DataBind()
DropDownList6.DataSource=dt
DropDownList6.DataTextField=“FullInfo”
DropDownList6.DataValueField=“FullInfo”
DropDownList6.DataBind()
DropDownList7.DataSource=dt
DropDownList7.DataTextField=“FullInfo”
DropDownList7.DataValueField=“FullInfo”
DropDownList7.DataBind()
con.Close()
下一个

我可以通过创建一个数组列表并对其排序来解决我的问题。这是在:

此示例代码如下所示:

Private Sub SortDropDown(ByVal ddl As DropDownList)


    '---Get listItems from dropDownList
    Dim ddlList As New ArrayList
    For Each li As ListItem In ddl.Items
        ddlList.Add(li)
    Next

    '---Sort arraylist
    ddlList.Sort(New ListItemComparer)


    '---Copy sorted list back into the dropDownList
    ddl.Items.Clear()
    For Each li As ListItem In ddlList
        ddl.Items.Add(li)
    Next

End Sub
这是:

Public Class ListItemComparer : Implements IComparer


Public Function Compare(ByVal x As Object, _
      ByVal y As Object) As Integer _
      Implements IComparer.Compare
    Dim a As ListItem = x
    Dim b As ListItem = y
    Dim c As New CaseInsensitiveComparer
    Return c.Compare(a.Text, b.Text)
End Function

End Class

为什么不在SQL查询中直接排序?我更新了我的帖子,解释了为什么我不能在SQL查询中排序。我过度简化了我的原始问题。这适用于我过度简化的原始问题。我不应该为我的问题简化我的代码,而是在OP中进行了更新。从审阅队列:我可以请求您在源代码周围添加一些上下文吗。只有代码的答案很难理解。如果你能在你的文章中添加更多的信息,这将有助于询问者和未来的读者。
Public Class ListItemComparer : Implements IComparer


Public Function Compare(ByVal x As Object, _
      ByVal y As Object) As Integer _
      Implements IComparer.Compare
    Dim a As ListItem = x
    Dim b As ListItem = y
    Dim c As New CaseInsensitiveComparer
    Return c.Compare(a.Text, b.Text)
End Function

End Class