【问题标题】:Error when sorting a dropdownlist on webpage对网页上的下拉列表进行排序时出错
【发布时间】:2015-06-12 12:51:30
【问题描述】:

我的网页不允许我对下拉列表进行排序。我有一个 SQLDataSource 连接配置为在表tblCodesWorkNotRec 中显示行描述。当我转到 Order By 并选择按描述排序,然后测试查询时,我收到错误 -

text、ntext 和 image 数据类型不能进行比较或排序,除非使用 IS NULL 或 LIKE 运算符。

为什么?

如果需要看代码,什么代码——aspx 还是vb?

这是vb代码:

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Mvc

Partial Class TimeOffAddNoRequest
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Once data is edited on this webpage, do not go back to first loaded data (data from gridview)
        If Page.IsPostBack = True Then
            Return
        End If

        Dim windowsLoginName As System.String = HttpContext.Current.User.Identity.Name   'System.Security.Principal.WindowsIdentity.GetCurrent().Name

        Dim split As String() = Nothing
        Dim vname As String

        Dim sqlConnection As New SqlConnection("Data Source=janetdev;Initial Catalog=TimeSQL;Persist Security Info=True;User ID=sa;Password=password")
        Dim cmd, cmd1, cmd2 As New SqlCommand
        Dim returnValue, returnValue1, returnValue2 As Object
        Dim dt As Date
        dt = Today
        MsgBox(Today)

        'Get network login name (name only)
        split = windowsLoginName.Split("\".ToCharArray)
        vname = split(1)

        'Get firstname from tblEmployees that matches login name
        cmd.CommandText = "SELECT FirstName FROM tblEmployees where login = '" & vname & "'"
        cmd.CommandType = CommandType.Text
        cmd.Connection = sqlConnection

        'Get lastname from tblEmployees that matches login name
        cmd1.CommandText = "SELECT LastName FROM tblEmployees where login = '" & vname & "'"
        cmd1.CommandType = CommandType.Text
        cmd1.Connection = sqlConnection

        'Get employeeid from tblEmployees that matches login name 
        cmd2.CommandText = "SELECT EmployeeID FROM tblEmployees where login = '" & vname & "'"
        cmd2.CommandType = CommandType.Text
        cmd2.Connection = sqlConnection

        sqlConnection.Open()

        'firstname
        returnValue = cmd.ExecuteScalar()

        'lastname
        returnValue1 = cmd1.ExecuteScalar()

        'employeeid
        returnValue2 = cmd2.ExecuteScalar()

        sqlConnection.Close()

        'display firstname and lastname on screen
        TextBox3.Text = returnValue & " " & returnValue1
        TextBox4.Text = returnValue2

        'display today's date
        DateRequested.Text = dt

        'Get value of other fields 

        If Not [String].IsNullOrEmpty(Request.QueryString("BeginDateOff").ToString()) Then
            'Retrieving the BeginDateOff Value
            BeginDate.Text = Request.QueryString("BeginDateOff").ToString()
        End If

        If Not [String].IsNullOrEmpty(Request.QueryString("EndDateOff").ToString()) Then
            'Retrieving the EndDateOff Value
            EndDate.Text = Request.QueryString("EndDateOff").ToString()
        End If

        If Not [String].IsNullOrEmpty(Request.QueryString("BeginTimeOff").ToString()) Then
            'Retrieving the BeginTimeOff Value
            BeginTimeDD.SelectedValue = Request.QueryString("BeginTimeOff").ToString
        End If

        If Not [String].IsNullOrEmpty(Request.QueryString("EndTimeOff").ToString()) Then
            'Retrieving the EndTimeOff Value
            EndTimeDD.Text = Request.QueryString("EndTimeOff").ToString()
        End If

        'Retrieving the All Day Value
        AllDay.Checked = Boolean.Parse(Request.QueryString("AllDay_YesNo"))
        'AllDay.Checked = Request.QueryString("AllDay_YesNo")

        'Retrieving the WorkHoursNotRecordID
        wkid.Text = Request.QueryString("WorkHoursNotRecordID").ToString()

        If Request.QueryString("description").ToString = "Unknown" Then
            Label2.Text = "You must change the reason from Unknown."
            DropDownList2.Focus()
            Exit Sub
        Else
            'Retrieving the Description Value
            DropDownList2.Text = Request.QueryString("Description").ToString()
        End If

        BeginDate.Focus()

    End Sub

这里是aspx代码:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="TimeOffAddNoRequest.aspx.vb" Inherits="timework.TimeOffAddNoRequest" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ToolkitScriptManager ID="toolkitScriptManager" runat="server" />
    <asp:Label ID="Label2" runat="server" 
         style="color: #990033; font-weight: 700"></asp:Label>
    <asp:Label ID="Label1" runat="server" 
         style="color: #000099; font-weight: 700"></asp:Label>
    <br />
    <label>Employee Name:</label><asp:TextBox ID="TextBox3" runat="server" BorderStyle="None" 
         style="margin-left: 7px; font-size: medium; font-weight: 700;" 
         Width="150px" Height="22px" AutoPostBack="True"></asp:TextBox>
    <asp:TextBox ID="TextBox4" runat="server" Width="129px" 
         AutoPostBack="True" Visible="False"></asp:TextBox>
    <br />
    <hr align="left" style="width: 791px; margin-left: 0px" />
    <label>Date Requested:<asp:TextBox ID="DateRequested" runat="server" style="margin-left: 9px; font-size: medium; font-weight: 500;" 
         Width="150px" Height="22px" AutoPostBack="True" BorderStyle="None"></asp:TextBox>
    </label>
    <br />
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <label>Begin Date Off:</label>
    <asp:TextBox ID="BeginDate" runat="server" style="margin-left: 10px; font-size: medium; font-weight: 500;" 
         Width="150px" Height="22px" AutoPostBack="True" BorderStyle="Solid" 
            BorderWidth="1px"></asp:TextBox>
     <label>&nbsp; End Date Off:</label>
     <asp:TextBox ID="EndDate" runat="server" style="margin-left: 15px; font-size: medium; font-weight: 500;" 
         Width="150px" Height="22px" AutoPostBack="True" BorderStyle="Solid" 
            BorderWidth="1px"></asp:TextBox>
    <br />
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <label> All Day?</label>&nbsp;
    <asp:CheckBox ID="AllDay" runat="server" AutoPostBack="True" />
    <label>&nbsp;&nbsp; Begin Time Off:</label>&nbsp;<label>&nbsp;
    <asp:DropDownList ID="BeginTimeDD" runat="server" 
        DataSourceID="SqlDataSource1" DataTextField="Time" DataValueField="Time" 
        Height="26px" Width="100px">
    </asp:DropDownList>
        &nbsp; End Time Off:</label>&nbsp;<asp:DropDownList ID="EndTimeDD" 
            runat="server" DataSourceID="SqlDataSource1" DataTextField="Time" 
            DataValueField="Time" Height="26px" Width="100px">
    </asp:DropDownList>
    <br />
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TimeSQLConnectionString2 %>" 
        SelectCommand="SELECT [Time] FROM [tblCodesTime] ORDER BY [Time]">
    </asp:SqlDataSource>
       <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Reason:</label>  
       <asp:DropDownList 
        ID="DropDownList2" runat="server" AutoPostBack="True" 
        DataSourceID="SqlDataSource2" DataTextField="Description" 
        DataValueField="Description" Height="26px" Width="149px">
    </asp:DropDownList>
    <br />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TimeSQLConnectionString1 %>" 

        SelectCommand="SELECT [Description] FROM [tblCodesWorkNotRec] ORDER BY [Description]">
    </asp:SqlDataSource>
    <asp:TextBox ID="wkid" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button2" runat="server" Text="Save" />
    <br />
    <br />
 </asp:Content>

【问题讨论】:

  • 该错误是 sql server 错误消息,而不是 aspx 或 vb 消息。你能调用一个有错误的视图吗?你在生成 SQL 吗?如果是这样,显示它,。如果不显示生成错误的代码以及该代码中变量的定义。
  • 我的 SQL 数据库中没有任何视图。 tblCodesWorkRec 中的字段描述是一个文本字段。我把代码放在我的帖子里。该下拉列表(DropDownList2)位于两者的底部附近。谢谢你的帮助!我重新编写了整个表格,试图找出我在哪里得到这个错误,因为调试没有帮助。如果您需要更多信息,请告诉我。

标签: asp.net vb.net sorting drop-down-menu


【解决方案1】:

我相信你的错误是在你的这一行的 aspx 中

SELECT [Description] FROM [tblCodesWorkNotRec] ORDER BY [Description]

order by description 将导致您出现该错误。您可以通过转换为 varchar 来解决此问题:

SELECT [Description] 
FROM [tblCodesWorkNotRec] 
ORDER BY Convert(varchar(max), [Description])

我不确定您为什么要按描述字段进行订购。也许最好只是取消order by。该错误也可能发生在order by [time] 行上。但如果这是一个DateTime 字段,那很好。这个字段可能应该重命名为不是关键字的东西,比如CodeTime

关于您的代码的其他一些注释,重构这是一个好主意。

  1. 作为建议,我会创建一个与您的数据库对话的单个类。不要将 sql 连接放在你的代码后面,在你的 aspx 中要少得多。 (如果你要这样做,选择一个位置,而不是两个位置)这个类将包含你所有的 sql 并只返回你想要的值。

  2. 整合你的 sql。没有理由对同一个表进行 3 次调用以从每次调用中获取一个值。打一个电话就可以得到 3 样东西。我说的是名字、姓氏、员工ID 电话。

  3. 不要将业务逻辑放在事件处理程序中。注意您的 Page_Load 方法。这是一个事件处理程序。您放在这里的任何代码都不能从其他地方调用,因此您最终会一遍又一遍地复制代码。只需将调用移至它们自己的方法,然后从 Page_Load 调用该方法。 (GoF pattern - Facade)

  4. 将你的 UI 元素放在它们自己的 div 标签中,并将它们与 CSS 对齐。 摆脱(或至少认真考虑)&lt;br/&gt;&amp;nbsp; 标记并使用CSS中的margin之类的东西可以移动你的元素。还将所有内联样式标签移动到 css。

【讨论】:

    猜你喜欢
    • 2015-11-20
    • 1970-01-01
    • 2023-03-21
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2023-03-03
    相关资源
    最近更新 更多