Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 7-Jan-11 6:23am
v3
Comments
William Winner 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.

Set AcceptsReturn of the TextBox to true

Regards
Espen Harlinn
 
Share this answer
 
Comments
thatraja 7-Jan-11 11:46am    
Good answer
Espen Harlinn 7-Jan-11 12:20pm    
Thanks thatraja!
William Winner 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 7-Jan-11 12:38pm    
William: I answered the question as formulated at revision 1
William Winner 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.
Do as Espen suggested and then go somewhat like this:

C#
...
    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
 
Share this answer
 
v2
This[^] article might help you.
 
Share this answer
 
Comments
fjdiewornncalwe 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.
Abhinav S 7-Jan-11 12:39pm    
Thanks Marcus. My point too.
Manfred Rudolf Bihy 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 7-Jan-11 22:39pm    
Thanks Manfred.
So, definitely not familiar with VMS, but could you convert the string to bytes and then store the bytes?
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900