Solved

Formating entries from a database

Posted on 2004-09-15
22
798 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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