Solved

Formating entries from a database

Posted on 2004-09-15
22
772 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now