locked
line feed, carriage return from sql

    Question

  • i simply want to show a message box in c# with more than one line like this:

    MessageBox.Show("some text\nsome other text");

    i use \n to have line feed and carriage return and its works.

    now when i use this:

    using (SqlConnection connection = new SqlConnection(connectionstring))
    {
    connection.Open();
    SqlCommand command = new SqlCommand(String.Format("SELECT * FROM [Table] WHERE (Message#='{0}", 1), connection);
    SqlDataReader dr = command.ExecuteReader();
    if (dr.HasRows)
    {
    dr.Read();
    MessageBox.Show(String.Format("{0}", dr["TexteField"]));
    }
    }

    thats is not working, i mean im not able to have a line feed and carriage return,
    even if i write \n, \r\n in the field of my table.

    Someone know why or what i can do to have a message box with more than one line when i get text from an Sql table???

    tx

    Tuesday, March 24, 2009 2:18 AM

Answers

  • Hi,

    I think you first need to assign the dr["TexteField"] to a string, then insert the line feed and carriage return:
    e.g.

    string test1 = "This is a test string";  
            string test2, test3;  
     
            test2 = test1.Insert(15, "\n\r");  
            test3 = test1.ToUpper();  
     
            Console.WriteLine("test1: '{0}'", test1);  
            Console.WriteLine("test2: '{0}'", test2);  
            Console.WriteLine("test3: '{0}'", test3);  
     
            if (test1 == test3)  
                Console.WriteLine("test1 is equal to test3");  
            else  
                Console.WriteLine("test1 is not equal to test3");  
     
            test2 = test1.Replace("test", "sample");  
            Console.WriteLine("the new test2: '{0}'", test2);  
     

    Hope this helps
    Binze

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, March 30, 2009 4:00 AM
    Friday, March 27, 2009 4:40 AM

All replies

  • \n is not treated specially in SQL-Server it is simply a string so i suppose you need to enter the text multilined in your db like:

    'test'+char(13)+char(10)+'dummy'

    Tuesday, March 24, 2009 7:37 AM
  • Hi,

    I think you first need to assign the dr["TexteField"] to a string, then insert the line feed and carriage return:
    e.g.

    string test1 = "This is a test string";  
            string test2, test3;  
     
            test2 = test1.Insert(15, "\n\r");  
            test3 = test1.ToUpper();  
     
            Console.WriteLine("test1: '{0}'", test1);  
            Console.WriteLine("test2: '{0}'", test2);  
            Console.WriteLine("test3: '{0}'", test3);  
     
            if (test1 == test3)  
                Console.WriteLine("test1 is equal to test3");  
            else  
                Console.WriteLine("test1 is not equal to test3");  
     
            test2 = test1.Replace("test", "sample");  
            Console.WriteLine("the new test2: '{0}'", test2);  
     

    Hope this helps
    Binze

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Monday, March 30, 2009 4:00 AM
    Friday, March 27, 2009 4:40 AM