|
-
Jul 26th, 2011, 05:55 AM
#1
Thread Starter
New Member
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:
Code:
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 ProcedureName'" & 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
My GridView looks like:
Code:
< asp:GridView ID="GridView2" runat="server" Height="143px" AllowSorting="true" OnSorting="GridView_Sorting1"
How can I get past this problem?
All help and advice will be greatly appreciated.
Many thanks,
Dan
-
Jul 26th, 2011, 06:27 AM
#2
Re: Conversion from string “DESC” to type 'Double' is not valid.
Can you indicate which line of code you're getting the error on.
-
Jul 26th, 2011, 06:29 AM
#3
Thread Starter
New Member
Re: Conversion from string “DESC” to type 'Double' is not valid.
Hello,
I get the error on the following line:
Code:
If (GridViewSortDirection = SortDirection.Ascending) Then
Thanks,
Dan
-
Jul 26th, 2011, 06:39 AM
#4
Re: Conversion from string “DESC” to type 'Double' is not valid.
You are trying to compare a string against an enum, its a completely different type!
You need to either change your GridViewSortDirection property into the same type, or compare it as text instead.
Code:
Property GridViewSortDirection() As SortDirection
-
Jul 26th, 2011, 06:55 AM
#5
Thread Starter
New Member
Re: Conversion from string “DESC” to type 'Double' is not valid.
I have set it so that it is now the same type, and when I go to click on the column to sort it I now get an error pointing to:
With the error now saying:
Conversion from string "desc" to type 'Integer' is not valid.
Any help please?
Thanks,
Dan
-
Jul 26th, 2011, 08:32 AM
#6
Re: Conversion from string “DESC” to type 'Double' is not valid.
You have exactly the same problem. You have converted your string to the type, but are still testing it as a string!
Code:
Select Case GridViewSortDirection
Case SortDirection.Ascending
-
Jul 26th, 2011, 08:47 AM
#7
Thread Starter
New Member
Re: Conversion from string “DESC” to type 'Double' is not valid.
Hey,
I have now changed them so I am not testing the string anymore:
Code:
Property GridViewSortDirection() As SortDirection
Get
If IsNothing(ViewState.Item("GridViewSortDirection")) Then
Return SortDirection.Descending
End If
Return ViewState.Item("GridViewSortDirection")
End Get
Set(ByVal Value As SortDirection)
ViewState.Item("GridViewSortDirection") = Value
End Set
End Property
Function GetSortDirection() As SortDirection
Dim GridViewSortDirectionNew As SortDirection
Select Case GridViewSortDirection
Case SortDirection.Descending
GridViewSortDirectionNew = SortDirection.Ascending
Case SortDirection.Ascending
GridViewSortDirectionNew = SortDirection.Descending
Case Else
GridViewSortDirectionNew = SortDirection.Descending
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 SortDirection = GetSortDirection()
'Dim sortExpression = e.SortExpression
'Dim dv As New DataView()
If (GridViewSortDirection = SortDirection.Ascending) Then
GridViewSortDirection = SortDirection.Descending
Else
GridViewSortDirection = SortDirection.Ascending
End If
End Sub
However, when I click on to a column, nothing actually happens now.
Any reason for this?
Thanks,
Dan
-
Jul 26th, 2011, 09:05 AM
#8
Re: Conversion from string “DESC” to type 'Double' is not valid.
2 reasons.
1.
Code:
Dim mySortdirection As SortDirection = GetSortDirection() <<< You already change the direction INSIDE that sub
'Dim sortExpression = e.SortExpression
'Dim dv As New DataView()
If (GridViewSortDirection = SortDirection.Ascending) Then <<< And you are reversing it AGAIN here
GridViewSortDirection = SortDirection.Descending
Else
GridViewSortDirection = SortDirection.Ascending
End If
2. You are not actually doing anything with the property...
-
Jul 26th, 2011, 09:12 AM
#9
Thread Starter
New Member
Re: Conversion from string “DESC” to type 'Double' is not valid.
Ok then, I get that what I am doing in GridView_Sorting1 I have already done in GetSortDirection.
How do I go about getting the data to sort when I click on the column header??
Thanks for your help so far.
Dan
-
Jul 26th, 2011, 09:22 AM
#10
Re: Conversion from string “DESC” to type 'Double' is not valid.
I think I can guess that you picked up this code from here. I have never used it before.
The eventargs has a parameter you can change to set the sortorder, surely you just change that?
Code:
e.SortExpression = GetSortDirection
-
Jul 26th, 2011, 09:45 AM
#11
Thread Starter
New Member
Re: Conversion from string “DESC” to type 'Double' is not valid.
Hi Grimfort,
Can you explain a bit more please?
I am starting to understand, but need a bit more details.
Would I do:
vb Code:
If e.SortExpression = SortDirection.Ascending Then
GridViewSortDirection = SortDirection.Descending
Else
GridViewSortDirection = SortDirection.Ascending
End If
Is that along the right lines?
If it isnt, would you mind advising on the what the right lines would be?
Thanks,
Dan
-
Jul 26th, 2011, 11:11 AM
#12
Re: Conversion from string “DESC” to type 'Double' is not valid.
Look at the link I posted to here. I have not used this before. The example shows a string version in order to create a sort expression, not the type (which you changed it to after my comment, as I had no idea why you were using it!). Simple answer is to remove what you have done, and use the code from the sample.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|