Click here to Skip to main content
11,480,502 members (55,890 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET2.0
Hi
I was wondering if anyone new how to retrieve a Carriage Return from a .Net TextBox (in a PC App.) that has Multiline set to True?
So if the TextBox text looks like this:

hello

world

I'd like to know that the C/R's are there and store them (to database) as such, so that on redisplaying they are there.

For the moment I'm not using a RichTextBox as the data I'm storing is supposed to be simple and the field in the DB that it's saving to is 100 chars long (don't ask me to make this bigger ;O) ).

Anyway, if anyone can help, that would be great!

Cheers

Jugs


---------------------------------------------------------
From OP:

Thank you all for your immediate answers! Not quite there yet.

We're using an Alpha (VMS) database, which doesn't have the same facilities as SQL, and storing various types of characters doesn't help yet either (Chr(13), '\r' etc.).

I'm thinking of storing my own special characters in the string whilst looping through the '.Lines' property of the TextBox, and then on reading back out somehow insert a line break in the text box and discard the character.

Thanks anyway

J
Posted 7-Jan-11 3:44am
Edited 7-Jan-11 7:23am
v3
Comments
William Winner at 7-Jan-11 12:31pm
   
Is the field a variable length record? I'm not at all familiar with VMS, but it looks like if you use a variable length record, you can load in data with CR-LF and it will convert them appropriately. They aren't actually stored in the db, but it stores the length of each line in bytes.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Set AcceptsReturn of the TextBox to true

Regards
Espen Harlinn
  Permalink  
Comments
Espen Harlinn at 7-Jan-11 12:38pm
   
William: I answered the question as formulated at revision 1
William Winner at 7-Jan-11 12:49pm
   
Well, at revision 1, the post still said,

So if the TextBox text looks like this:

hello

world

implying that the carriage returns are already there. That was my point.
William Winner at 7-Jan-11 12:26pm
   
Though it does look like the assumption is that the carriage returns are already in the TextBox, so your answer actually doesn't address the question...
Espen Harlinn at 7-Jan-11 12:20pm
   
Thanks thatraja!
Espen Harlinn at 7-Jan-11 13:09pm
   
Ahh! well ;->
thatraja at 7-Jan-11 11:46am
   
Good answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Do as Espen suggested and then go somewhat like this:

...
    String myTextBoxTextWithCarriageReturns = textBox1.Text;
    bool success = WriteTextBoxData(someConnectionString, myTextBoxTextWithCarriageReturns);
...
 
private bool WriteTextBoxData(String connectionString, String text)
{
    bool retVal = true;
    string queryString = "INSERT INTO TextData text VALUES(@text)";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.Add(new SqlParameter("text", SqlDbType.VarChar,100).Value = text);
        try
        {
            connection.Open();
            command.ExectueNonQuery();
        }
        catch(Exception ex)
        {
             //ToDo: Add something here: i.e. log exception
             retVal = false;
        }
        finally
        {
             connection.Close();
        }
    }
    return retVal;
}

My code assumes that the field in the DB is type varchar(100), so some adjustments might have to be made (i.e. primary key etc.). This code works even if the text has carriage returns in it as the parameter is passed with the SqlParameters class. This class does the right encoding so that valid SQL is generated. If you build the SQL statement by hand you would have to encode the CRs yourself.

Hope that helps!

Best Regards,
Manfred
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This[^] article might help you.
  Permalink  
Comments
Marcus Kramer at 7-Jan-11 10:07am
   
Not sure why you got a 2 for this, it's worth more than that to me. I don't necessarily like the implementation in the article you linked to, but it does demonstrate how to approach the topic.
Manfred R. Bihy at 7-Jan-11 12:49pm
   
Must be a "heavy" 2 too! I added my 5+ but the score is not even close to 4. Hmmmm ...
Abhinav S at 7-Jan-11 12:39pm
   
Thanks Marcus. My point too.
Abhinav S at 7-Jan-11 22:39pm
   
Thanks Manfred.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

So, definitely not familiar with VMS, but could you convert the string to bytes and then store the bytes?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 215
1 Richard MacCutchan 150
2 CHill60 90
3 CPallini 70
4 Dave Kreskowiak 70
0 Sergey Alexandrovich Kryukov 7,874
1 OriginalGriff 7,556
2 Sascha Lefèvre 3,074
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 7 Jan 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100