Home > DeveloperSection > Forums > Data Table loads only last record of mysqlDataReader
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    September-01-2014 12:09 AM

 C#
Ratings:


 1 Reply(s)
 936  View(s)
Rate this:

In my page mysqlDataReader returns 4 records.Whenever I load this mysqlDataReader to DataTable using 

DataTable dt = new DataTable();

dt.Clear();

dt.Load(dr);

DataTable shows only last record.

my Code is:

DataTable dt = new DataTable();

for (int i = 0; i < gvDemoBatches.Rows.Count; i++)

{

     CheckBox cb = (CheckBox)gvDemoBatches.Rows[i].FindControl("checkSelect");

     if (cb.Checked == true)

     {

         Panel2.Visible = true;

         objBEBatch.BatchID = Convert.ToInt32(((Label)gvDemoBatches.Rows[i].Cells[0].FindControl("lblBatchId")).Text);

          MySqlDataReader dr = objBLAddNewDemo.GetParticularDemoData(objBEBatch);

         dt.Clear();

         dt.Load(dr);

      }

 }

"GetParticularDemoData" stored Procedure Returns 7 rows.but datatable shows only last record.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-01-2014 12:49 AM

Hi Manoj, 

your are adding rows to datatable inside loop. So the last loop record will be saved in the datatable

for (int i = 0; i < gvDemoBatches.Rows.Count; i++)

{

will execute multiple times and inside this you have

 MySqlDataReader dr = objBLAddNewDemo.GetParticularDemoData(objBEBatch);

 dt.Clear();

 dt.Load(dr);

So the data table will contain the data only for the last row of the gridview where cb.Checked == true)


Don't want to miss updates? Please click the below button!

Follow MindStick