DATA TABLE LOADS ONLY LAST RECORD OF MYSQLDATAREADER

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
C#
 1235  View(s)
Ratings:
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.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Data Table loads only last record of mysqlDataReader

    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)

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!