4
using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand(sqlCommand, connection))
        {
            command.CommandType = CommandType.Text;
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                DataTable datatable = new DataTable();
                datatable.Load(reader);

                return datatable;
            }
        }
    }

Running this code returns an empty DataTable. However, looping through reader.Read() and printing to the debug console shows that the reader has data and it prints the expected data. Also, When I expand the reader object during debugging, hasRows is true and the field count is right for the number of columns returned.

There was a similar post here Trouble loading SQL Data Reader data into DataTable but the answer essentially was, just don't use it, use a SqlDataAdapter. I would prefer to use it and the DataTable has a load method that takes an IDataReader DataTable.Load(IDataReader). I just don't know why the reader is working when I print it to the debug window but not when I load it into the DataTable. Am I just overlooking something?

Community
  • 1
  • 1
asmithdev
  • 326
  • 2
  • 12
  • 1
    @SonerGönül I don't see a Fill method on the SqlDataReader https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx – asmithdev Dec 17 '15 at 15:29
  • How about using `SqlDataAdapter` and using it's `Fill` method? – Soner Gönül Dec 17 '15 at 15:32
  • @SonerGönül I can use the `SqlDataAdapter` but I am just curious why the appropriate method on the DataTable isn't working... SqlDataReader is much faster and I would prefer to use it. Here is some info on the differences http://stackoverflow.com/a/1676809/3866019 – asmithdev Dec 17 '15 at 15:35
  • 2
    I doubt there's a difference - `DataTable.Load` uses a data adapter internally anyway :) All those "benefits" of using `SqlDataReader` that are in the answer you linked only apply when you don't use the reader to fill a `DataTable` (which shouldn't be unexpected, since data adapter simply fills a data table using the data reader; if you do that manually using a data reader instead, where would you expect any benefit? You're doing the exact same thing. The cost is in the data table.). Be careful about accepting advice like this without understanding what actually happens - it's going to hurt. – Luaan Dec 17 '15 at 15:39
  • http://www.dotnetcurry.com/aspnet/143/convert-data-reader-to-data-table there are only small differences between your code and this article, but give it a try. – terbubbs Dec 17 '15 at 15:42
  • 1
    In any case, `DataTable.Load` works just fine for me. If you keep everything as is and use a data adapter to fill the table instead, does it actually work? Do you have multiple result sets? What does the command do? – Luaan Dec 17 '15 at 15:43
  • @Luaan you make a good point about the performance. I still don't like stepping away from it without knowing why it won't work. – asmithdev Dec 17 '15 at 15:52

3 Answers3

13

It turns out I was just overlooking something and this is actually not an issue at all. The original code actually works fine. The preview of the DataTable object when debugging shows {} and looked empty to me. enter image description here

Then there was a property on the object called ExtendedProperties that had Count = 0, which obviously isn't a row count but I just glossed over and got Count = 0 stuck in my head.

enter image description here

If you find yourself in the same situation, scroll down when hovering the object and expand the Rows and you should see your row count in there. enter image description here

I got duped on this one... sorry for the stupidity on my part and thanks for the help everyone.

asmithdev
  • 326
  • 2
  • 12
0

Try doing this see if it works...

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlDataAdapter dap = new SqlDataAdapter(sqlCommand,connection);
    DataTable datatable = new DataTable();
    dap.Fill(datatable);
    return datatable;
}
khaled4vokalz
  • 876
  • 9
  • 13
0

Use SqlDataReader or DataAdapter, both will work. Write the return statement after closing the connection. Try if it works.

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
akkipedia
  • 11
  • 2