Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:32:49+00:00 2026-06-13T10:32:49+00:00

I want to output to my console a nicely formatted table with data from

  • 0

I want to output to my console a nicely formatted table with data from SqlDataReader.

I found this answer here on SO with a nice class to do all the work, however, I need some help to implement the SqlDataReader part.

My code for printing out the table looks like this:

        SqlDataReader data = getCommentsFromDB();
        int value = 997;
        string[,] arrValues = new string[5, 5];
        for (int i = 0; i < arrValues.GetLength(0); i++)
        {
            for (int j = 0; j < arrValues.GetLength(1); j++)
            {
                value++;
                arrValues[i, j] = value.ToString();
            }
        }
        ArrayPrinter.PrintToConsole(arrValues);
        Console.ReadLine();

getCommentsFromDB looks like this:

        SqlConnection conn = dal.connectDatabase();
        conn.Open();
        cmd = new SqlCommand(@"SELECT * FROM GuestBook", conn);
        rdr = cmd.ExecuteReader();
        return rdr;

If you need anything else, please tell.

UPDATE

I got it a bit further. However, now I am getting this nasty error:

Error: {0}System.NullReferenceException: Object reference not set to an instance of an object.
   at GuestBook.ArrayPrinter.GetMaxCellWidth(String[,] arrValues) in \GuestBook\GuestBook\ArrayPrinter.cs:line 39
   at GuestBook.ArrayPrinter.GetDataInTableFormat(String[,] arrValues) in \GuestBook\GuestBook\ArrayPrinter.cs:line 60
   at GuestBook.ArrayPrinter.PrintToConsole(String[,] arrValues) in \GuestBook\GuestBook\ArrayPrinter.cs:line 117
   at GuestBook.StudentManager.showAllComments() in \GuestBook\GuestBook\StudentManager.cs:line 49
   at GuestBook.ConsoleGUI.start(String[] args) in \GuestBook\GuestBook\ConsoleGUI.cs:line 28
   at GuestBook.Program.Main(String[] args) in \GuestBook\GuestBook\Program.cs:line 20

With my experience, I would say something is wrong with the class I am using. Maybe it needs an update?

I am running in VS2012 and C#.NET 4.0

Update 2

My data prints out like this:

-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|       X         |                 |                 |                 |
|                 |        X        |                 |                 |
|                 |                 |       X         |                 |
|                 |                 |                 |        X        |
-------------------------------------------------------------------------

and not in a single row.

My code so far:

    public void showAllComments()
    {
        SqlDataReader reader = getCommentsFromDB();

        string[,] arrValues = new string[5, 3];

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (!reader.Read()) break; //no more rows
                {
                    arrValues[i, j] = reader[j].ToString();
                }
            }
        }
        ArrayPrinter.PrintToConsole(arrValues);
    }

Also, I would like it to expand vertically to contain all the data in the database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:32:50+00:00Added an answer on June 13, 2026 at 10:32 am

    To let the array printer class works with you SqlDataReader you have to move the data contained in the DataReader into an array of string.

    You can access the SqlDataReader using a while loop using as a condition the Read() function which move the Reader to the next row; values can be accessed using [] operator with the column position or field name as index.

    while (reader.Read())
    {
        string myVal = reader["COLUMN_NAME"].ToString();
    }
    

    Supposing you wanna read the first 5 columns in the first five rows in you reader you could do something like this using a for loop(obviously you will probably need to make the code more flexible)

    EDIT: modified code

    SqlDataReader reader = command.ExecuteReader();
    
    string[,] arrValues = new string[5, 5];
    
    for (int i = 0; i < 5; i++)
    {
        if (!reader.Read()) break; //no more rows 
        for (int j = 0; j < 5; j++)
        {
            arrValues[i,j] = reader[j].ToString();
        }
    }
    

    EDIT 2:

    This should fix the random number for row problem. Not an optimal solution but should works.
    Warning i just wrote it in a text editor, could not test it with a compiler

    public void showAllComments()
        {
            SqlDataReader reader = getCommentsFromDB();
            List<List<string>> myData; //create list of list
            int fieldN = reader.FieladCount; //i assume every row in the reader has the same number of field of the first row
            //Cannot get number of rows in a DataReader so i fill a list
            while (reader.Read())
            {
                //create list for the row
                List<string> myRow = new List<string>();
                myData.Add(myRow);//add the row to the list of rows
                for (int i =0; i < fieldN; i++)
                {
                    myRow.Add(reader[i].ToString();//fill the row with field data
                }
            }
    
            string[,] arrValues = new string[myData.Count, fieldN]; //create the array for the print class
    
            //go through the list and convert to an array
            //this could probably be improved 
            for (int i = 0; i < myData.Count; i++)
            {
                List<string> myRow = myData[i];//get the list for the row
                for (int j = 0; j < nField; j++)
                {
                    arrValues[i, j] = myRow[j]; //read the field
                }
            }
            ArrayPrinter.PrintToConsole(arrValues);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want the output displayed in webconsole using console.log in a table format is
i want to launch ffmpeg from my app and retrive all console output that
This is a hw assignment. The answer/output I want is correct. I just don't
I want to output some characters in C# console application and then rewrite them,
I want to build a console-like output using JTextPane. Therefore I am using a
I want the output as shown in below image This is a Sikh holy
I want to get the console output along with the regular test results in
I have this shape and I want to output it to ConoleApplication Windows. I
I found lots of samples how to redirect console output into a file. However
Using java.util.logging.Logger to output some log to the console just like this: public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.