SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to keep line breaks while saving to SQL database and retrieving it to show inside a label

I have a textarea where I enter some text in multiple lines by pressing enter:

<asp:TextBox ID="tbMessage" runat="server" ClientIDMode="Static" 
     TextMode="MultiLine" Columns="30" Rows="5"></asp:TextBox>

I save the text from code-behind (MessageText column is of type varchar):

using (SqlCommand qSave = new SqlCommand())
{
    qSave.Connection = oCon;
    qSave.CommandType = CommandType.Text;
    qSave.CommandText = @"INSERT INTO [Db1].[dbo].[Table1] (MessageText) VALUES (@MessageText)";
    qSave.Parameters.AddWithValue("@MessageText", tbMessage.Text);

    try
    {
        oCon.Open();
        qSave.ExecuteNonQuery();
    }
    catch (SqlException ce)
    {
    }
    finally
    {
        oCon.Close();
    }
}

I retrieve the column and show it inside a label from code-behind:

public void MyFunction()
{
    strSql = @"SELECT * FROM [Db1].[dbo].[Table1]";

    using (SqlConnection conn = new SqlConnection(gloString))
    {
        try
        {
            // create data adapter
            SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
            // this will query your database and return the result to your datatable

            myDataSet = new DataSet();
            da.Fill(myDataSet);

            string specific = "";
            string generic = "";
            string strTemp = "";

            foreach (DataRow r in myDataSet.Tables[0].Rows)
            {
                if (r["MessageText"].ToString().Length <= 65)
                {
                    strTemp = r["MessageText"].ToString();
                }
                else
                {
                    strTemp = TruncateLongString(r["MessageText"].ToString(), 65) + "...";
                }

                specific += "<span class='hoverText tooltip' title='" + this.Server.HtmlEncode(r["MessageText"].ToString().Replace("\r\n", "<br />")) + "'>" + this.Server.HtmlEncode(strTemp) + "</span><span class='dvMsgInitHidden'>" + this.Server.HtmlEncode(r["MessageText"].ToString().Replace("\r\n", "<br />")) + "</span><br /><br />";
            }

            lblMessage.Text = specific;

            upMessage.Update();
        }
        catch (Exception ce)
        {
        }
    }
}

public string TruncateLongString(string str, int maxLength)
{
    return str.Substring(0, maxLength);
}

As you can see from the following screenshot the title shows correctly but the message is all jumbled into one line:

enter image description here

Please help me resolve it.

Upvotes: 4

Views: 4901

Answers (3)

SearchForKnowledge
SearchForKnowledge

Reputation: 3751

The following CSS takes care of it:

white-space: pre-wrap;

Upvotes: 12

BrettJ
BrettJ

Reputation: 980

We've made a helper method that simply replaces \r\n, \n, and \r with the HTML break tag
for output. We store the text in the database with the raw \r \n characters, and during edit, TextArea honors the line break naturally. Its only when showing on the "details page" in a span, that the conversion to a HTML break tag should be done.

Upvotes: 1

Oscar
Oscar

Reputation: 13960

If you need to store formating in ddbb, you can use rtf or html tags for this. Rtf could be more suited for desktop apps, in your case, you'll need to store html tags among the text (p, br, etc)

Upvotes: 0

Related Questions