Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 28th, 2006, 10:34 AM
Registered User
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default gridview - ampersand in data field problem

I need to get data from a database where a few fields contain an ampersand. The data cannot be changed because they are legal names for corporations and so must display with the ampersand. The problem occurs where I have data such as "H & H Corporation" for example. The search variable is entered and validated on the first page, then put into a string variable and passed to the gridview page accessdatabase control. When I do a test query within the control, the sql statement pulls the correct records. But, when it displays in the grid, I get extra records, for instance, any other record that starts with a single "H" such as "H Wilson Construction" or "H & S Brothers". It appears to be the ampersand in the business name that is causing the problem. Also, as you can see from the code below, I have to use " like ? + '%' to even get any record with an ampersand in it to display.

Here is my control code on the gridview page:
    <asp:AccessDataSource ID="odbPIResults" runat="server" DataFile="~/App_Data/PInew.mdb"
            SelectCommand="SELECT CorpName FROM Pi WHERE (CorpName like ? + '%') ORDER BY CorpName">
                <asp:QueryStringParameter Name="CorpName" QueryStringField="CorpName" Type="String" />

Can anyone tell me how to deal with this problem? Is there a way to intercept the string variable when it leaves the first page and before it goes into the accessdatabase control? I think that is where the problem occurs since I can run a test query within the control that works correctly?

Your help would be GREATLY appreciated!!!

Old April 28th, 2006, 02:05 PM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi there,

Your problem is rooted in the way URLs work. The ampersand character is used to separate query string variable. For example, if you look at this page, you'll see something like this:


The ampersand separates the querystring field TOPIC_ID from the FORUM_ID. In your case, you try to have something like this:

SomePage.aspx?CorpName=H & Wilsom

Basically, this gives you a querystring parameter of CorpNamewith a value of H and a querystring parameter of Wilsom without a value. So, when you execute the SELECT statement, you query for everything that starts with an H.

There are a few ways around this. Probably the easiest way is to avoid query strings. Is there a particular reason you're using a querysting? Can't you use a search field on the same page that displays the records?

If not, then you can escape the querystring using Server.UrlEncode. For example this:

Server.URLEncode("H & Wilson")

results in


When you pass this over the querystring, there's a fair chance your query will work.

Hope this helps,

Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Track 12 by Live (Track 12 from the album: Throwing Copper) What's This?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in exporting data in a gridview to MSExcel Rashmi_Dubey ASP.NET 2.0 Basics 12 July 15th, 2009 04:57 PM
problem in inserting data in gridview deb_kareng ASP.NET 2.0 Professional 0 August 22nd, 2007 02:07 AM
Replace Gridview field if null with new field Indo77 ASP.NET 2.0 Basics 1 June 18th, 2007 06:22 AM
sorting problem with a date field in gridview jwrz200t Visual Studio 2005 0 September 21st, 2006 01:19 PM
Problem importing data into a TEXT field seansheds SQL Server DTS 1 August 19th, 2004 02:52 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.