开发者

Conversion from string "DESC" to type 'Double' is not valid

I am developing a site in ASP.Net and VB.Net that will enable users to sort data in a GridView in Ascending or Descending order.

The records are coming from an SQL Server Express database.

I go to click on a column heading to sort the data and I get the following error:

'Conversion from string "DESC" to type 'Double' is not valid.'

Below is the code I am using for the sorting:

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

I am not sure what this error means as I am not using a double, I am using a String.

My GridView is as follows:

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

How can I get over this problem?

All help and advice will be greatly appreciated.

Many Thanks,

Dan


You use the comparison GridViewSortDirection = SortDirection.Ascending in your code which looks like it may be the cause of the problem. You are trying to compare a string with an enum which is going to cause you conversion issues. You may want to change all those string declarations to use the enum so that you are comparing like with like.

This is certainyl likely to be a problem in your code and possibly the problem. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜