• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

DbDataReader drops results

I have a scenario were my data clears out after connecting to an excel spreadsheet using DbDataReader.  Below is the sample code.  So if I mouse over datareader to the results view I see records at first but then disappears if I pause and does not return any records in the loop.
string Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=excelFile;Extended Properties=Excel 8.0;";

using (OleDbConnection conn = new OleDbConnection(Conn))
        {
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [sheet$]", conn);
            conn.Open();

            using (DbDataReader datareader = cmd.ExecuteReader())
            {

            }

Open in new window

0
ws11
Asked:
ws11
  • 6
  • 2
1 Solution
 
SriVaddadiCommented:
You got it into a reader object  and you are not doing anything inside the pause. DataReader object gets closed once the using block is finished and you would not be able to see records.
0
 
guru_samiCommented:
---> then disappears if I pause and does not return any records in the loop.
Where is your loop...or code you are trying to read data from?
0
 
ws11Author Commented:
It will not loop it just goes through what the loop contains one time despite how many records I have.  I have used this before but not with an excel file.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
ws11Author Commented:
If I stop my trace at using (DbDataReader datareader = cmd.ExecuteReader())  the results view shows my column count and all my records so it has the data but if I wait go back and look again it will clear the ienumerable.
0
 
ws11Author Commented:
See images.  Image1 is first view then Image2 is after.  The ienumerable is clearing its self.  But it should not do this when it loops until the loop is finish.  At least that is what I would expect.
Image1.jpg
Image2.jpg
0
 
ws11Author Commented:
So I guess something like this would continue to loop...

foreach (DbDataRecord record in reader)
0
 
guru_samiCommented:
to loop through the records you will need to use Read method like shown here:
while(datareader.Read())
{
//get values..
}

Check this: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson04.aspx
0
 
ws11Author Commented:
Sorry I did not follow your initial post but figured it out that I needed to go row by row.  I thought that was what ienumerables enumeration was all about.  Any comments on the enumeration aspect would be greatly appreciated.
0
 
ws11Author Commented:
Yes I have that already sorry for not posting that part.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now