Solved

Formating entries from a database

Posted on 2004-09-15
22
786 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
  • 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert html to string 6 34
Help with C#, MVC, razor. 6 34
Reading the web config for a running service on Windows 10 16 41
RLDC Reporting in Visual studio 11 16
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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