Friday, February 24, 2012

How to store a content into database

Hi everyone,

I'm just using TEXTBOX control to save data into my DB. My question is, if I have multiple lines of sentences, it does not store as what it looks like. For example;

Hello,
Mike

It stores into the database as "Hello, Mike" and if I read from the database, it does not have seperate lines and shows it like this;


Hello, Mike

How can I store the contents into database and make the contents as original?
(Sorry for the poor English, but I hope you understand it.)

One way of doing this will be to replace all new lines with <BR> before saving it to database.

do something like that:

TextBox1.Text.Replace(vbCrLf,

"<br>")|||

SQL Server dose accept carriage returns, but note it stores carriage returns as 2 characters:1st for line feed (ascii code 10)and 2nd for carriage return (ascii code 13). How did you push the lines in the textbox into database? If you use a single string which contains multiple lines, then you can get the lines back from database. For example:

using (SqlConnection conn = new SqlConnection(@."Data Source=.\iori2000;Integrated Security=SSPI;Database=tempdb"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("CustOrderHist", conn);
string s1 = textBox1.Text;

cmd.CommandText = "INSERT INTO testString SELECT @.v";
cmd.Parameters.Add("@.v", SqlDbType.VarChar).Value = s1;

cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT s FROM testString";
SqlDataReader sda = cmd.ExecuteReader();
sda.Read();
s1 = sda.GetString(0);
textBox1.Text = "Retrieved:\n" + s1;
}

No comments:

Post a Comment