We help IT Professionals succeed at work.

how to save and display enter key in text string

metropia
metropia asked
on
Hello Experts,

I have an ASP.NET page written in C# that contains a multi-line text-box in which a user can enter their comments.

I am assuming that the user while entering the comments will hit the enter key to start a writing a new paragraph, or a new line.

I would like to know, how can that event be saved when inserting the contents of the textbox into the SQL database table, as well as how to display the lines formatted as the user entered them.

I hope this question is clear, if not, please let me know.

In advance, thank you much.
Comment
Watch Question

Author

Commented:
I am attaching the code of how I save the text string, the field is:

COMMENTS

Thanks.
SqlCommand cmnd = new SqlCommand("INSERT INTO RG_AR_COLLECTIONS_LOG " +
                                             "(CUSTOMER_ID, " +
                                             "  ENTITY_ID, " +
                                             "  INVOICE_ID, " +
                                             "  INVOICE_DATE, " +
                                             "  NET_DUE_DATE, " +
                                             "  COMMENTS, " +
                                             "  LAST_CONTACT_DATE) " +
                                             "VALUES (@CUSTOMER_ID,@ENTITY_ID,@INVOICE_ID,@INVOICE_DATE,@NET_DUE_DATE,@COMMENTS,@LAST_CONTACT_DATE)" +
                                             " SELECT CAST(scope_identity() AS int)", con3);

            cmnd.Parameters.AddWithValue("CUSTOMER_ID", txtCustID.Text);
            cmnd.Parameters.AddWithValue("ENTITY_ID", txtEntityID.Text);
            cmnd.Parameters.AddWithValue("INVOICE_ID", txtInvoiceID.Text);
            cmnd.Parameters.AddWithValue("INVOICE_DATE", dtInvDate);
            cmnd.Parameters.AddWithValue("NET_DUE_DATE", dtNetDueDate);
            cmnd.Parameters.AddWithValue("COMMENTS", strComments);
            cmnd.Parameters.AddWithValue("LAST_CONTACT_DATE", paramCommentDate.Value);
            cmnd.Connection = con3;
            newID = (Int32)cmnd.ExecuteScalar();

Open in new window

replace all new line characters in your string with CHAR(13) when inserting.

INSERT INTO MyTable (MyStringColumn) VALUES('My name is'+CHAR(13)+'Viral Patel')

Open in new window


The above statement will insert the following:

My name is
Viral Patel

Author

Commented:
what about this solution?

strComments = txtComments.Text.Replace(Environment.NewLine, "<br />");
Top Expert 2011
Commented:
Yes, I think your question is clear.

If you use textarea control, it automatically wraps to a new line.

Besides, when a user hits the enter key, a new line is created.

<asp:TextBox runat="server" TextMode="MultiLine"/>

Open in new window


When you display the record, using similar control but with data bound, you get the same data in the format it is entered.

If this is not happening for you, then check if you are escaping the output and the way you are doing so.
you will need to replace when inserting i.e. in your sql statement.



INSERT INTO MyTable (MyStringColumn) VALUES(REPLACE('this is my \r\n string',CHAR(13))


Author

Commented:
this is my control code:

                <asp:TextBox
                    ID="txtComments"
                    runat="server"
                    Height="110px"
                    TextMode="MultiLine"
                    Width="410px"
                    BorderColor="#CCCCFF"
                    BorderStyle="Solid"
                    BorderWidth="1px"
                    Font-Names="Verdana"
                    Font-Size="Small"
                    CssClass="style5">
                </asp:TextBox>



If I enter:

this is a test.
this is my test.
this is another silly test.

then save it.

it displays like:

this is a test.<br />this is my test.<br />this is another silly test.
sorry, correction:

INSERT INTO MyTable (MyStringColumn) VALUES(REPLACE('this is my \r\n string','\r\n',CHAR(13))

Author

Commented:
how would I implement your solution using my current code?

cmnd.Parameters.AddWithValue("COMMENTS", strComments);
nishant joshiTechnology Development Consultant
Commented:
your value will be stored with /r and \n formate without any issue..

but the later use of this elements are important so can you able to give info about it...

where you want to use that comment to show a later on pure html or asp.net label control???

Regards,
nishant

Author

Commented:
I want to display it in another multi-line asp text-box control.