2015-04-07 42 views
1

我有一個網頁,不會讓我對下拉列表進行排序。我有一個SQLDataSource連接配置成調出tblCodesWorkNotRec表中的行說明。當我進入Order By並按說明進行排序,然後測試查詢時,出現錯誤 -排序網頁上的下拉列表時出錯

無法比較或排序文本,ntext和圖像數據類型,除非使用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> 
+1

該錯誤是一個sql server錯誤消息,而不是aspx或vb消息。你可以調用一個有錯誤的視圖嗎?您是否正在生成SQL?如果是這樣,展示它。如果不顯示生成錯誤的代碼和該代碼中變量的定義。 – paqogomez

+0

我在我的SQL數據庫中沒有任何視圖。 tblCodesWorkRec中的字段描述是一個文本字段。我把代碼放在我的帖子中。該dropdownlist(DropDownList2)接近兩者的底部。謝謝你的幫助!我重新編寫了整個表單,試圖找出我得到這個錯誤的地方,因爲調試沒有幫助。如果您需要更多信息,請告訴我。 – user3033348

回答

0

我相信你的錯誤是在你的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件事。我說的是名字,姓氏,僱員號碼。

  3. 不要把事務處理器的商業邏輯放在。注意你的Page_Load方法。這是一個事件處理程序。你放在這裏的任何代碼都不能從其他地方調用,所以你最終會一遍又一遍地複製代碼。只需將呼叫移至自己的方法,然後從Page_Load中調用該方法即可。 (GoF pattern - Facade

  4. 將你的用戶界面元素放在他們自己的div標籤中,並將它們與CSS對齊。擺脫(或者至少認真思考)<br/>&nbsp;標記,並使用類似CSS中的邊距來移動元素。同時將所有內聯樣式標籤移至CSS。