?
Solved

error when changing sort key in gridview to a non-numeric value

Posted on 2009-07-06
9
Medium Priority
?
398 Views
Last Modified: 2012-05-07
VB.NET on VS2008 with MS SQL2005 backend

I have a gridview which is devoid of any columns as I am using a single template field for each row.  The user can select a sort key from a dropdown list (outside of the gridview) and once selected the gridview will re-sort itself based on the selection. (see code - using a control parameter of the dropdownlist)

This works well for numeric values but as soon as I try to sort using a nchar(50) datatable field I receive an error as folows

"Error Converting Data Type Varchar To Bigint."

So my problem is when I try to sort on the Domain field of the example supplied in code. Sorting on the other 2 fields works perfectly.
<asp:SqlDataSource ID="SqlDataSource_ContactHyperlink" runat="server" ConnectionString="<%$ ConnectionStrings:conn_db1 %>"
                                            ProviderName="System.Data.SqlClient" SelectCommand="SELECT IndexID, OrderID, 
                                            hyperlink_url, DomainName, hyperlink_memo, origin_UserName, origin_UserID, origin_Visibility, 
                                            origin_dateTime, starRating FROM tbl_bookmarks
                                            WHERE origin_UserID = @curUserID
                                            ORDER BY CASE @SortBy
                                            WHEN 'IndexID' THEN IndexID 
                                            WHEN 'starRating' THEN starRating
                                            WHEN 'domain' THEN DomainName
                                            END">
                                            <SelectParameters>
                                                <asp:SessionParameter Name="curUserID" SessionField="UserGUID" Type="String" />
                                                <asp:ControlParameter ControlID="DropDownList_sortOrder" Name="SortBy" PropertyName="SelectedValue"
                                                    Type="String" />
                                            </SelectParameters>
                                        </asp:SqlDataSource>

Open in new window

0
Comment
Question by:JOHNFROG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24784195
Hope this helps:

Since your DomainName was character, it creates the problem for other WHEN Clauses in your CASE statement.
SELECT IndexID, OrderID, 
hyperlink_url, DomainName, hyperlink_memo, origin_UserName, origin_UserID, origin_Visibility, 
origin_dateTime, starRating FROM tbl_bookmarks
WHERE origin_UserID = @curUserID
ORDER BY CASE @SortBy
WHEN 'IndexID' THEN cast(IndexID AS varchar(1000))
WHEN 'starRating' THEN cast(starRating AS varchar(1000))
WHEN 'domain' THEN DomainName
END

Open in new window

0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24784199
Or check whether you pass varchar values instead of bigint for @curUserID
0
 

Author Comment

by:JOHNFROG
ID: 24784341
That confuses me as what you are doing is converting all the other fields to character based fields and leaving the problematic one alone. Will give it a try and see what happens though. The curUserID is a GUID so it definately isnt bigint either.

Remember this works for all sorts except the domainname field which is nchar(50)

Have been playing with using RTrim on the DomainName field but cant get the syntax right. Is this perhaps the solution or am i barking up the wrong tree??
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24784398
Let me explain clearly.

In a CASE statement, the THEN Clause that is your output should be in same datatype for all WHEN clauses.

You have IndexID and starRating as integers/ bigint and DomainName as Varchar.
Obviously to resolve out this issue, you have to convert it into a common datatype either Varchar / bigint.
Conversion from bigint to varchar is possible whereas varchar to bigint is not possible.

And hence I converted all fields to varchar which should work out fine.

Hope this clarifies.
0
 

Author Comment

by:JOHNFROG
ID: 24784436
Thanks I didnt know that. Got my stripes on MS Access so that will probably explain it.

While we are on the subject can you tell me if it creates extra overhead if I was to create multiple SqlDataSources on my page and simply toggled between them upon changing of the dropdown list?
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 2000 total points
ID: 24784497
>> can you tell me if it creates extra overhead if I was to create multiple SqlDataSources on my page and simply toggled between them upon changing of the dropdown list?

As far as you manage the connections properly by opening and closing as required, there is no overhead on it.
But if you have multiple things opened and not properly closed then it might bring the system down by not allowing to open new connections and connection resource related issues.

Hope that query too worked out.
0
 

Author Comment

by:JOHNFROG
ID: 24784505
works brilliantly - next time I will be less critical of answers that seem a bit strange.

Will be posting another shortly on the same block of code re getting the ascending descending to work. Currently am toggling between 2 identical SqlDataSources to achieve asc and desc outcomes.

0
 

Author Closing Comment

by:JOHNFROG
ID: 31600110
flawless !
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24784577
Thanks..
And glad to help you out now and in future too..
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question