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.

Community
  • 1
  • 1
Frederik
  • 632
  • 4
  • 16
  • 34

1 Answers1

1

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);
    }
il_guru
  • 8,383
  • 2
  • 42
  • 51
  • Thanks for your response. However, I can't really seem to get it working with the rest of my code. I just get a blank output. I got 3 columns of data to be shown, and n number of rows. Do you have a clue how to solve? – Frederik Oct 24 '12 at 17:01
  • Aight. Fixed the "no-output" part. However, I am getting an `System.NullReferenceException` - added more details to the question. – Frederik Oct 24 '12 at 19:02
  • how have you declared the arrValues? If you declared it like in my code [5,5] you could get it because the code does not fill the last two values for every line – il_guru Oct 25 '12 at 06:37
  • Oh lawd. Now it shows data. However, the data does not match a row, look at my update for how it actually prints it. – Frederik Oct 25 '12 at 07:02
  • Fixed the code... there was too much reading. This should fix the read data output – il_guru Oct 25 '12 at 07:43
  • THANKS! That fixed all the small issues I had – Frederik Oct 25 '12 at 08:04
  • You're wellcome. I was just writing a complete answer before you accept. here it is the final code – il_guru Oct 25 '12 at 08:17