Learn how to a build a cloud-first strategyRegister Now

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

newline in varchar

Hello,
I am trying to put in a nvarchar field in my database the content from a textbox.
I can't figure out how to insert the newline.
My idea was to put something like "@#%x" in the text when i want a new line and then:

string s,ss;
s = TxtNews.Text.ToString();
ss = s.Split(""@#%x");
s= ss(0) + Environment.NewLine + ss(1);

But i have more then 1 newline, and not the same number. I don't know how to count how many times "@#%x" because string method Contains returns a bool, not an number. Can someone show me another way to do it?
0
radupopescu777
Asked:
radupopescu777
  • 2
  • 2
  • 2
  • +2
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
ss = s.Split(""@#%x");
number_of_xxx = s.Length - 1
0
 
dhananjayadCommented:
Just allow the user to enter a new line into the textarea by pressing the enter key. When you read the content of the textarea
txtNews.Text.Split("\n");
0
 
radupopescu777Author Commented:
String Var1,Var2,Var3;
                Var1 = TxtDetalii.Text.ToString();
                Var2 = Var1.Split("#@#");


When i do this is says:
Error      1      The best overloaded method match for 'string.Split(params char[])' has some invalid arguments. Do you know why?
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
sunithnairCommented:
Why dont you just replace the text with the new line like this
string s;
s = Replace(TxtNews.Text.ToString(), "@#%x", Environment.NewLine);

Open in new window

0
 
sunithnairCommented:
Sorry like this
string s;
s = TxtNews.Text.Replace("@#%x", Environment.NewLine);

Open in new window

0
 
dhananjayadCommented:
Parameters for Split are a character array. So what you need to do is, use a special character like '~' for the new line and then:

txtNews.Text.Split('~')
0
 
radupopescu777Author Commented:
If i use the replace function with Environment.NewLine it just inserts another /n in the text.

And if i use:
 string Var1;              
 Var1 = Var1.Split('~');
It gives me: Error      1      Cannot implicitly convert type 'string[]' to 'string.

And if i try:
 String Var1;
 String[] Var2;
 Var2 = Var1.Split('~');
It gives me: Error      1      Use of unassigned local variable 'Var1'

Help..?



0
 
BlomholmCommented:
I assume you are going to display the text from the database somewhere on your site and you want there to be paragraphs. Not just one big chunk of text ?
Could do it like this:

Literal lit = new Literal();
lit.Text = YourStringFromDb.Replace("\n", "<br />");

Open in new window

0
 
BlomholmCommented:
If your TextArea is set to TextMode="Multiline"
then it will containt '\n' characters where the user hit enter.
So if you insert the content of : yourTextArea.Text into the db, you'll get the newlines characters inserted in db as ' \n ':
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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