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
Solved

Displaying Special Characters

Posted on 2004-09-07
15
827 Views
Last Modified: 2009-12-16
Hello,

I have some data in my Database that contain the following characters:

  - ampersand (&)
  - quotations " to represent inches

My problem is that "Oranges & Apples" will only display as "Oranges" and 15"TV will only cause an error. How can I fix this so they appear on the page they same way they exist in the DataBase?

Thanks
0
Comment
Question by:champ_010
  • 11
  • 4
15 Comments
 
LVL 15

Expert Comment

by:Thogek
ID: 11999551
That might depend on how you are getting these values from the database to your Web page's display.
0
 
LVL 1

Author Comment

by:champ_010
ID: 11999653
I am outputting them like this inside a DataList ItemTemplate:

<%#DataBinder.Eval(Container.DataItem,"subCatName")%>

I don't know how else to do it.
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11999667
I'm presuming that the DataList is being bound to some data source object, and that that data source object is being populated from the database somehow....
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:champ_010
ID: 11999839
Yes it is nested inside another DataList and get's it's source from a DataRelation Table:

<asp:DataList ID="SiteList" RepeatColumns="3" RepeatDirection="Horizontal" runat="server">
    <ItemTemplate>
     <a href="categoriesMain.aspx?catID=<%#DataBinder.Eval(Container.DataItem,"categoryID")%>"<%#DataBinder.Eval(Container.DataItem,"categoryName")%></a>

         <asp:DataList ID="SiteSubCats" datasource='<%#((DataRowView)Container.DataItem).Row.GetChildRows("CatSub")%>'  runat="server">
      <ItemTemplate>
                  <a href="products.aspx?subCatID=<%#DataBinder.Eval(Container.DataItem,"[\"subCatID\"]")%>"
      &subCatName=<%#DataBinder.Eval(Container.DataItem,"[\"subCatName\"]")%>
                 </a>
      </ItemTemplate>
           </asp:DataList>
            
</ItemTemplate>
</asp:DataList>
0
 
LVL 1

Author Comment

by:champ_010
ID: 11999858
Sorry--there's an extra " in there from bad copying and pasting--please ignore.
0
 
LVL 15

Accepted Solution

by:
Thogek earned 125 total points
ID: 12000010
If you're trying to write out values that might contain special characters such as ampersands into a link URL, then you need to URL encode those values.  Otherwise, the ampersand will signal the end of the current value to the server processing the request.  E.g.,
    products.aspx?subCatID=14&subCatName=Oranges&Apples
indicates a "subCatID" parameter with a value of "14", a "subCatName" parameter with a value of "Oranges", and an "Apples" parameter with no value -- probably not quite what you wanted.

Try replacing

    <a href="products.aspx?subCatID=<%#DataBinder.Eval(Container.DataItem,"[\"subCatID\"]")%>&subCatName=<%#DataBinder.Eval(Container.DataItem,"[\"subCatName\"]")%>

with

    <a href="products.aspx?subCatID=<%# HttpContext.Current.Server.UrlEncode(DataBinder.Eval(Container.DataItem,"[\"subCatID\"]")) %>&subCatName=<%# HttpContext.Current.Server.UrlEncode(DataBinder.Eval(Container.DataItem,"[\"subCatName\"]")) %>
0
 
LVL 1

Author Comment

by:champ_010
ID: 12000039
Hi have several places in my website that display output from the database using:

<%#DataBinder.Eval(Container.DataItem,"subCatName")%> and they do the same thing so my problem doesn't necessary pertain to the code layout above, just simply to the out put via DataBinder.Eval.

If it's in the database as:

"Oranges & Apples", it will output as "Oranges"

Also if it is in the database as: 14" TV it will dislay 14" TV but the css formatting will disappear as thought the " closed the tag before style tag kicked in.

0
 
LVL 1

Author Comment

by:champ_010
ID: 12000044
Thanks--just got your last post. Am trying it out now....
0
 
LVL 1

Author Comment

by:champ_010
ID: 12000196
Thogek,

It wasn't the url encode at all!  When I went back to look, I noticed in the status bar that "Oranges & Apples" show up fine as &category=Oranges & Apples&catID=14  etc.

By accident I noticed that there was a space after &category=Oranges  & Apples before &catID so I went to my code and changed:

<a href="products.aspx?subCatID=<%#DataBinder.Eval(Container.DataItem,"[\"subCatID\"]")%>
&subCatName=<%#DataBinder.Eval(Container.DataItem,"[\"subCatName\"]")%>

to put it all on one line--the problem was fixed!  The problem was that I split the querystring into several lines to make it easier for viewing on the code page--I guess I should keep it all on one line.

Looking at the "other" places on my website where I am outputing "Oranges & Apples" as only "Oranges"--they are not url links.  I retreive the values from a querystring so:

string SubCatName=Request.QueryString["subCatName"].ToString();

To output by: <%=SubCatName%> will only give me "Oranges".

How to fix this?
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12000257
In the case of
    string SubCatName=Request.QueryString["subCatName"].ToString();
how is the subCatName populated into the QueryString in the first place?  (I suspect this is still a case where a preceeding link should have used Server.UrlEncode, as discussed above.)
0
 
LVL 1

Author Comment

by:champ_010
ID: 12000386
You are probably right--I see % in the address bar...am changing now...
0
 
LVL 1

Author Comment

by:champ_010
ID: 12000426
I got this error:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.HttpServerUtility.UrlEncode(string)' has some invalid arguments

for this:

<a href="categoriesMain.aspx?catID=<%#HttpContext.Current.Server.UrlEncode(DataBinder.Eval(Container.DataItem,"categoryID"))%>&category=<%#HttpContext.Current.Server.UrlEncode(DataBinder.Eval(Container.DataItem,"categoryName"))%>"><%#DataBinder.Eval(Container.DataItem,"categoryName"))%></a>

0
 
LVL 1

Author Comment

by:champ_010
ID: 12000715
Is there some way to convert it on the page that receives the querystring?
0
 
LVL 1

Author Comment

by:champ_010
ID: 12001162
Can we replace %20 with ""? it's the spaces that are giving me problems--doesn't look like the & is.
0
 
LVL 1

Author Comment

by:champ_010
ID: 12001639
ANSWER:

You need to do Convert.ToString();

So:

<a href="categoriesMain.aspx?catID=<%#DataBinder.Eval(Container.DataItem,"categoryID")%>&category=<%#Server.UrlEncode(Convert.ToString(DataBinder.Eval(Container.DataItem,"categoryName")))%>">

Point awarded anyways for letting me know about Server.Encode.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net Fit 3 divs on screen side by side 3 39
ASP.NET MVC 2 40
jquery progress bar 3 55
encoding an href string 2 24
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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