1

I have applied breakpoint and when I click on query "ResultView" its showing data but when I click 2nd time then data is empty and its showing Enumeration yielded no results. It is strange, is there any cache issue or something else? Why it is showing empty when I click after 1 time, its just show data 1st time,

       var connectionString = String.Format(@"
        Provider=Microsoft.ACE.OLEDB.12.0;
        Data Source={0};
        Extended Properties=""Excel 12.0 Xml;HDR=YES""
    ", filePath);
           
            //Creating and opening a data connection to the Excel sheet 
            using (var conn = new OleDbConnection(connectionString))
            {
                conn.Open();

                var cmd = conn.CreateCommand();
                cmd.CommandText = "select * from [الحيازات$]";

                using (var rdr = cmd.ExecuteReader())
                {
                    //LINQ query - when executed will create anonymous objects for each row
                    var query =
                             (from DbDataRecord row in rdr
                              select row).Select(x =>
                              {
                                  //dynamic item = new ExpandoObject();
                                  Dictionary<string, string> item = new Dictionary<string, string>();
                                  for (int i = 0; i < x.FieldCount; i++)
                                  {
                                      string data = "" + rdr.GetName(i) + ":{'id':'" + rdr.GetName(i) + "','code':'" + rdr.GetName(i) + "','title':'" + rdr.GetName(i) + "','type':'text','response";
                                     
                                      item.Add(data, x[i] + "}");
                                  }
                                  return item;
                              });

                    //Generates JSON from the LINQ query

                    json = JsonConvert.SerializeObject(query, Formatting.Indented).Replace("'", "\"");// break point here
Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31

2 Answers2

0

See similar question. Inspecting with the debugger calls Read(), which will try to go to the next row. Does your table only have 1 row?

The first time you view the data (when you can actually see it, not the second time when you get the Exception), are there multiple rows shown? If not, then you have an issue with your query as it is only returning one row.

If there are multiple rows, there may be an issue with using LINQ on a DataReader object. Full transparency, I haven't used LINQ to loop through a SqlDataReader before. This is how I would do it:

using (var rdr = cmd.ExectuteReader())
{
    while(rdr.Read())
    {
         //do something with the first column
         Console.WriteLine(rdr[0]);
    }
}

If you want to use LINQ, you could always create a datatable to read from.

using (var rdr = cmd.ExectuteReader())
{
    DataTable datatable = new DataTable();
    datatable.Load(rdr)
}

...
var results = (from row in datatable...
bcook
  • 13
  • 4
  • NO, its reading data from excel file and excel file contains 760 rows, – Abdul Hameed Feb 03 '21 at 16:06
  • Edited my answer. – bcook Feb 03 '21 at 16:43
  • Hi thanks for answer, but my question is why its showing data first time in debug and then again click its showing empty no data also cursor show busy then data empty, i think data is big so thats why?? – Abdul Hameed Feb 03 '21 at 17:05
  • It wouldn't show you that message if the data was large, and in addition, 760 rows isn't really large for a data reader. Ty the rdr.Read() method I suggested. – bcook Feb 03 '21 at 17:22
0

The first time it works displaying a subset of your data. At the index of last successful row + 1 there might be a breaking record.

Then the enumerator will stop working.

enter image description here

In my specific case, calling the ToList() worked fine. (CsvHelper, CS0726) https://github.com/JoshClose/CsvHelper/issues/1434

IvanF.
  • 513
  • 10
  • 18