Solved

Formating entries from a database

Posted on 2004-09-15
22
801 Views
Last Modified: 2008-01-09
Hi all

I have a Textfield that allows the user to enter in paragraphs of information to a database, this information is then read back out of the database using a Repeater. I was wondering is it possible to format the data so it is exaxtly as the user entered it in. I was told HTML Encode would do the job


but how would I actually encode the data

This is how I currently doing it:

string str = Server.HtmlEncode( Comments.Text.Replace( "'", "''" ));

and inserting into the database using this:

cmd.CommandText = String.Format("Insert INTO forum (userId,forumTitle,countryId,cityId,dateEntry,forumDate,forumEntry,private) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", UserName.Text,forumTitle.Text,country.SelectedValue,city.SelectedValue,System.DateTime.Now.ToLongDateString(),Date.Text,str,MakeMePrivate);

So that will enter in the data, Im simply inserting str, which I encoded above. Is this correct?

Next up Is getting this infor back out of the database?

Do I need to use HTML Decode? and how on earth do I decode the data, if Im putting it straigt into a dataset?

Cheers for the help..

0
Comment
Question by:fallenarmies
[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
  • 11
  • 7
  • 2
  • +2
22 Comments
 
LVL 8

Expert Comment

by:razo
ID: 12064326
what u would want to do is to use a richtetbox that would transform the data into html
u save the html in ur databse
then u use a literal to view data
try using www.freetextbox.com
if u need specific example dont hesitate to ask
0
 

Author Comment

by:fallenarmies
ID: 12064834
And is there any way to do it without freetext box, because I dont need anything that complex just something that will read paragraphs and spaces correctly. No richtext formating is required.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12064924
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12065130
fallenarmies ,
You need to turn off validateRequest property before you try to use Server.HtmlEncode inside your web site otherwise you'll get trouble on it.
This link give you more definations about it :
http://asp.net/faq/RequestValidation.aspx.
Use Server.HtmlEncode before store your data into your db, then use Server.HtmlDecode to revert html into your textarea.
0
 

Author Comment

by:fallenarmies
ID: 12065290
And how would I decode it back to standard HTML if its from a dataset?
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 12065707
You also may need to replace line breaks with "<br>" and spaces with "&nbsp;".  Otherwise, the user's whitespace may be partially or completely ignored when redisplayed.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12065771
fallenarmies , Justin_W alerad mentioned it..
Long time to see Justin_W. Good day
0
 

Author Comment

by:fallenarmies
ID: 12065779
What does a line break look like in a text field?

and what how do I replace "<" or ">"
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12065825
fallenarmies ,
Something like this :
yourstring=txtTextBox.Text.Replace("<","<");
0
 

Author Comment

by:fallenarmies
ID: 12065980
I just did this

str = Server.HtmlEncode( Comments.Text.Replace( " ", "&nbsp;" ));
str = Server.HtmlEncode( Comments.Text.Replace( "<", "<" ));

So I got them working, but ummm how do I detect if the user has entered in a TAB or Return?

also how would I decode the results from a dataset?
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 12066103
fallenarmies ,
Try something like this :
str = Server.HtmlEncode( Comments.Text.Replace( " ", "&nbsp;" ));
str = str.Replace( "<", "<" );
str = str.Replace(vbCrlf,"<br>");
str = str.Replace(vbTab,"&nbsp;&nbsp;&nbsp;&nbsp;");
0
 

Author Comment

by:fallenarmies
ID: 12066150
Ok I have the carriage return thing, and it seems to work fine, now I guess all I need to do is decode the thing so it displays the HTML Formatting. Any ideas on how to do this from a dataset?
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12066152
In c#, sure this format :
vbCrlf change to "\r\n"
vbTab change to "\t"
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12066163
fallenarmies , use Server.Decode() as mentioned earlier.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12066187
fallenarmies ,
I got to convert my head from vb.net to c#. Sorry if i got make any mistake there.
here is the conversion : http://www.netcoole.com/VB6TOCS/vbhtml/vbconsts.htm
0
 

Author Comment

by:fallenarmies
ID: 12066292
I have the server.decode part. But I take it directly from the database put it into a dataset and use a Repeater to display the data, so how do I use Server.Decode with that

is it

<% Server.Decode(#DataBinder.Eval(Container.DataItem, "Comments")) %>
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12066335
fallenarmies ,
You can pull your value inside a returned function.
eg:
<%#ValidData(DataBinder.Eval(Container.DataItem, "Comments")) %>

public function ValidData(string strItem)
{
  return Server.Decode(strItem);
}
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12066338
fallenarmies ,
You can pull your value inside a returned function.
eg:
<%#ValidData(DataBinder.Eval(Container.DataItem, "Comments")) %>

public string ValidData(string strItem)
{
  return Server.Decode(strItem);
}
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 12066395
x_com>>Long time to see Justin_W. Good day
Thanks x_com!  Yeah, I was swamped with work for a while.
0
 

Author Comment

by:fallenarmies
ID: 12066429
Cool now finally one minor problem

When I view the entry, I have a small summary of each entry. But I have the summary limited to 255 characters, the problem is that When I have a all the <BR> in it does read them.

Another problem is that when they dont enter a return or space it picks it up as one big string is it possible to limit the number of characters before it goes to the next line?
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12066470
IT job seem restless :-)
Hope can see your awesome comments again in EE thread, Justin_W!
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 12066536
fallenarmies,
>>When I view the entry, I have a small summary of each entry. But I have the summary limited to 255 characters, the problem is that When I have a all the <BR> in it does read them.
-You mean your summary data type=255 characters(maximum)?The rest of the text is concat?

>>Another problem is that when they dont enter a return or space it picks it up as one big string is it possible to limit the number of characters before it goes to the next line?
-you can use javascript to prevent user to enter text before user press enter after certain limit of characters. You can get a good answer at Javascript area about this request.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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