簡體   English   中英

從字符串“ DESC”到類型“ Double”的轉換無效

[英]Conversion from string “DESC” to type 'Double' is not valid

我正在ASP.Net和VB.Net中開發一個站點,該站點使用戶可以按Grid升序或降序對數據進行排序。

記錄來自SQL Server Express數據庫。

我單擊列標題對數據進行排序,然后收到以下錯誤:

'從字符串“ DESC”到類型“ Double”的轉換無效。

以下是我用於排序的代碼:

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

我不確定這個錯誤是什么意思,因為我沒有使用double,而是使用String。

我的GridView如下:

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

我該如何解決這個問題?

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

非常感謝,

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

這是certainyl可能是在你的代碼中的問題和可能的問題。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM