Friday, March 9, 2012

Row brakes when displaying text from database

I have a sql database where i have descriptions of products stored. I insert text using a simple sql insert and that seems to work fine. But when i try to get these texts and display them in a gridview with an object data source thet brake rows are not displayed. The text is just display with now row brakes at all.

How can i fix this so that i get it displayed in rows? Because its almost unreadable like ths.

Make the colum that doent get show text break like this

<asp:BoundFieldDataField="A"HtmlEncode="false"/>

|||

I added theHtmlEncode="false"but nothing changed. Maybe Im doing something wrong when Im adding the text to my database. Im using a html TextArea that iv set as runat="server" to get my text. Maybe the code doesnt recognise that there are rowbrakes in the TextArea?

<textarearunat="server"id="TextAreaUpsinfo"cols="40"rows="3"></textarea>

//some more html code//this is inside codebehindstring myConnectionString = @."Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\alho\Documents\Intrapoint\Web\App_Data\Intrapoint.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; SqlConnection myConnection =new SqlConnection(myConnectionString);string myInsertQuery ="INSERT INTO ups (partID, description, listprice, picture, info) Values(@.partID, @.description, @.listprice, @.picture, @.info)"; SqlCommand myCommand =new SqlCommand(myInsertQuery); myCommand.Parameters.AddWithValue("partID", TextBoxUpspartID.Text); myCommand.Parameters.AddWithValue("description", TextBoxUpsdescription.Text); myCommand.Parameters.AddWithValue("listprice", TextBoxUpslistprice.Text); myCommand.Parameters.AddWithValue("picture", TextBoxUpspicture.Text); myCommand.Parameters.AddWithValue("info", TextAreaUpsinfo.InnerText); ButtonSubmitUps.Enabled =false; myCommand.Connection = myConnection; myConnection.Open(); myCommand.ExecuteNonQuery(); myCommand.Connection.Close();
|||

flummer:

Maybe Im doing something wrong when Im adding the text to my database

flummer:

Maybe the code doesnt recognise that there are rowbrakes in the TextArea?

Exactly the same is happening. Basically the row break is a combination of two invisible characters ( "\r\n" carriage return and newline ). Now, at the time of adding the value you should replace these values with Environment.NewLine ( if you are using C# ) or VBNewLine ( if you are using VB ). Change the info parameter command line as follows.

flummer:

myCommand.Parameters.AddWithValue("info", TextAreaUpsinfo.InnerText);

myCommand.Parameters.AddWithValue("info", TextAreaUpsinfo.InnerText.Replace("\r\n",Environment.NewLine));

Hope this will help.

|||

I replaced my AddWithValue with the one you wrote but Im afraid I still get the text unformated when displayed in my datagrid. I think the problem is that i need to do something similar that you wrote, when the data is displayed in the gridview?

|||

I don't think you need to do the reverse replacement while displaying the record back. I think a multi line text control should handle this itself. Anyways, if the data is not getting displayed properly then you can try the reverse replacement at the time of displaying the data back.

No comments:

Post a Comment