Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

Formating entries from a database

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
fallenarmies
Asked:
fallenarmies
  • 11
  • 7
  • 2
  • +2
1 Solution
 
razoCommented:
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
 
fallenarmiesAuthor Commented:
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
 
nauman_ahmedCommented:
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!

 
David H.H.LeeCommented:
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
 
fallenarmiesAuthor Commented:
And how would I decode it back to standard HTML if its from a dataset?
0
 
Justin_WCommented:
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
 
David H.H.LeeCommented:
fallenarmies , Justin_W alerad mentioned it..
Long time to see Justin_W. Good day
0
 
fallenarmiesAuthor Commented:
What does a line break look like in a text field?

and what how do I replace "<" or ">"
0
 
David H.H.LeeCommented:
fallenarmies ,
Something like this :
yourstring=txtTextBox.Text.Replace("<","<");
0
 
fallenarmiesAuthor Commented:
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
 
David H.H.LeeCommented:
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
 
fallenarmiesAuthor Commented:
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
 
David H.H.LeeCommented:
In c#, sure this format :
vbCrlf change to "\r\n"
vbTab change to "\t"
0
 
David H.H.LeeCommented:
fallenarmies , use Server.Decode() as mentioned earlier.
0
 
David H.H.LeeCommented:
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
 
fallenarmiesAuthor Commented:
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
 
David H.H.LeeCommented:
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
 
David H.H.LeeCommented:
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
 
Justin_WCommented:
x_com>>Long time to see Justin_W. Good day
Thanks x_com!  Yeah, I was swamped with work for a while.
0
 
fallenarmiesAuthor Commented:
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
 
David H.H.LeeCommented:
IT job seem restless :-)
Hope can see your awesome comments again in EE thread, Justin_W!
0
 
David H.H.LeeCommented:
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

Technology Partners: 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!

  • 11
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now