2011-07-26 70 views
0

我正在ASP.Net和VB.Net中開發一個網站,使用戶能夠按照升序或降序對GridView中的數據進行排序。從字符串「DESC」轉換爲「Double」類型無效

記錄來自SQL Server Express數據庫。

我去上一欄點擊標題對數據進行排序,並且我收到以下錯誤:「無效‘

’從字符串‘DESC’轉換爲鍵入」雙

下面是我使用的分類代碼:

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click 

     Dim sqlConn As New SqlConnection 
     Dim sqlCmd As New SqlClient.SqlCommand 
     Dim sqlReader As SqlDataReader 

     'If no values are supplied in the textbox, throw an error message. 
     If TextBox2.Text = "" Then 
      MsgBox("A centre code needs to be provided...") 
     End If 

     If TextBox2.Text <> "" Then 

      'Telling the system the location of the database. 
      sqlConn.ConnectionString = "server=servername;Initial Catalog=dbName;Trusted_Connection=yes" 

      'Here we are opening the connection to the database. 
      sqlConn.Open() 

      'This is to say that sqlCmd is a stored procedure. 
      sqlCmd.CommandType = System.Data.CommandType.StoredProcedure 

      'This is creating the command to execute the stored procedure based on the information given in the connection string. 
      sqlCmd = sqlConn.CreateCommand 

      'The command is triggered to execute the stored procedure which grabs all information for the specific centre. 
      sqlCmd.CommandText = "exec GetAllInformationKCSEBoxes '" & TextBox2.Text & "' " 

      'This will read the rows in the database. 
      sqlReader = sqlCmd.ExecuteReader() 

      GridView2.Columns.Clear() 'If there are rows of data that match are criteria 
      If (sqlReader.HasRows) Then 

       'The rows of data are grabbed for the specific centre from the database using the data reader. 
       GridView2.DataSource = sqlReader 
       GridView2.DataBind() 
       GridView2.Columns.Clear() 
      Else 
       MsgBox("The centre code provided does not exist...") 
      End If 

      'This is closing the connection to the database once we have finished with it. 
      sqlConn.Close() 

      'This is to clear the list of items out of the GridView box. 

     End If 

    End Sub 

    Property GridViewSortDirection() As String 
     Get 
      If IsNothing(ViewState.Item("GridViewSortDirection")) Then 
       Return "desc" 
      End If 
      Return ViewState.Item("GridViewSortDirection") 
     End Get 

     Set(ByVal Value As String) 
      ViewState.Item("GridViewSortDirection") = Value 
     End Set 

    End Property 

    Function GetSortDirection() As String 

     Dim GridViewSortDirectionNew As String 

     Select Case GridViewSortDirection 

      Case "DESC" 
       GridViewSortDirectionNew = "ASC" 

      Case "ASC" 
       GridViewSortDirectionNew = "DESC" 

      Case Else 
       GridViewSortDirectionNew = "DESC" 

     End Select 
     GridViewSortDirection = GridViewSortDirectionNew 

     Return GridViewSortDirectionNew 

    End Function 

    Protected Sub GridView_Sorting1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView2.Sorting 

     Dim myPageIndex As Integer = GridView2.PageIndex 
     Dim mySortdirection As String = GetSortDirection() 
     Dim sortExpression = e.SortExpression 
     Dim dv As New DataView() 


     If (GridViewSortDirection = SortDirection.Ascending) Then 

      GridViewSortDirection = SortDirection.Descending 
      'SortGridView(sortExpression, "DESCENDING") 
     Else 
      GridViewSortDirection = SortDirection.Ascending 

     End If 


     'dv.Table = GridView2.DataSource 

     '   dv.Sort = e.SortExpression & " " & mySortdirection 
     '   GridView2.DataSource = dv 
     ' 
     '   GridView2.DataBind() 
     ' 
     '   GridView2.PageIndex = myPageIndex 

    End Sub 

    'Protected Sub SortGridView(string sortExpression,string direction) 

    'DataTable dt = GetData().Tables[0] 

    '   DataView(GridView2 = New DataView(GridView2)) 
    '   GridView2.Sort = sortExpression + Direction 
    ' 
    '   GridView1.DataSource = GridView2 
    '   GridView1.DataBind() 
    ' 
    '  End Sub 

我不知道,因爲我沒有使用雙這個錯誤是什麼意思,我使用的字符串。

我的GridView如下:

<asp:GridView ID="GridView2" runat="server" Height="143px" AllowSorting="true" OnSorting="GridView_Sorting1" 

我怎樣才能克服這個問題?

所有幫助和建議將不勝感激。

非常感謝,

+0

嗨丹。歡迎使用堆棧溢出,並請在報告錯誤消息時清楚它所在的線路。有可能你正在試圖給一個期望double的屬性分配一個字符串,但是如果我們知道錯誤被拋出的那一行,那麼計算出哪一個更容易。 :) – Chris

回答

1

你在代碼中使用的比較GridViewSortDirection = SortDirection.Ascending它看起來像它可能是問題的原因。您正試圖將一個字符串與一個將導致轉換問題的枚舉進行比較。您可能想要更改所有這些字符串聲明以使用枚舉,以便像like一樣進行比較。

這是肯定可能是a問題在您的代碼和可能的問題。 :)

+0

你好克里斯,謝謝你的迴應,請你介意多說一點嗎?我是VB.Net的新手,所以我想學習。謝謝,丹 – Dan

+0

@丹:實質上,刺和枚舉是不同的東西,所以有時(像現在)試圖比較它們可能會導致問題。有人說,看了更遠,我不認爲這實際上是造成你的錯誤,只是可能導致你潛在的問題。你能迴應我對主要問題的其他評論,詢問有關錯誤的詳細信息(如行號等)。 – Chris

相關問題