Home > Software engineering >  While loop iteration displays strange tabulation
While loop iteration displays strange tabulation

Time:12-12

    using (SqlDataReader myDataReader = myCommand.ExecuteReader())
            {
                //iterate loop results 
                while (myDataReader.Read())
                {
                    Console.WriteLine($"-> Make : {myDataReader["MakeId"]},\t "  
                        $"PetNAme : {myDataReader["PetName"]},\t"  
                        $" Color : {myDataReader["Color"]}.");

                }
            } 

The output Fun with Data Readers AutoLot

-> Make : 1,     PetNAme : Zippy,        Color : Black.
-> Make : 2,     PetNAme : Rusty,        Color : Rust.
-> Make : 3,     PetNAme : Mel,  Color : Black.// <- this one |||||||||||
-> Make : 4,     PetNAme : Clunker,      Color : Yellow.
-> Make : 5,     PetNAme : Bimmer,       Color : Black.
-> Make : 5,     PetNAme : Hank,         Color : Green.
-> Make : 5,     PetNAme : Pinky,        Color : Pink.
-> Make : 6,     PetNAme : Pete,         Color : Black.
-> Make : 4,     PetNAme : Brownie,      Color : Brown.

While loop displays strange tabulation with one element , any ideas why did this happen ?

enter image description here

CodePudding user response:

\t is a Tab character. One Tab character will advance the output to whatever the next tab stop is. Which is exactly what's happening on every line of output.

For example, open a word processor and type:

this is some long text<tab><return>
this is more text<tab><return>
text<tab><return>

(Using an actual Tab and Return where indicated.) Notice what the Tab character does on each of those lines.

To address this, you might add some logic to dynamically add more Tab characters depending on the string length, you might build formatted strings with spaces in between them based on the lengths of values, or you might output to something other than the console with more formatting capabilities (a GUI, a web application, etc.).

CodePudding user response:

First query for the longest pet name, use the length in a PadRight as shown below. Change the server and table name to match your database.

public static void Demo()
{
    using var cn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=ForumExample;Integrated Security=True");
    var sql = "SELECT LEN(PetName)  2 AS length FROM Pet WHERE LEN(PetName) = "   
              "(SELECT MAX(LEN(PetName)) FROM Pet)";

    using var cmd = new SqlCommand(sql, cn);

    cn.Open();

    var nameLength = Convert.ToInt32(cmd.ExecuteScalar());
    cmd.CommandText = "SELECT MakeId, PetName, Color FROM dbo.Pet;";

    SqlDataReader reader = cmd.ExecuteReader();
    
    Debug.WriteLine("         Fun with Data Readers AutoLot            ");
    while (reader.Read())
    {
        Debug.WriteLine($"-> Make : {reader.GetInt32(0),-4}PetName : {reader.GetString(1).PadRight(nameLength)}Color : {reader.GetString(2)}");
    }

}

Results

         Fun with Data Readers AutoLot            
-> Make : 1   PetName : Zippy    Color : Black
-> Make : 2   PetName : Rusty    Color : Rust
-> Make : 3   PetName : Mel      Color : Black
-> Make : 4   PetName : Clunker  Color : Yellow
-> Make : 5   PetName : Bimmer   Color : Black
-> Make : 6   PetName : Hank     Color : Green
-> Make : 7   PetName : Pinky    Color : Pink
-> Make : 8   PetName : Pete     Color : Black
-> Make : 9   PetName : Browie   Color : Brown
  • Related