---
title: "Data Table loads only last record of mysqlDataReader"  
description: "Data Table loads only last record of mysqlDataReader"  
author: "Manoj Bhatt"  
published: 2014-09-01  
updated: 2014-09-01  
canonical: https://www.mindstick.com/forum/2218/data-table-loads-only-last-record-of-mysqldatareader  
category: "c#"  
tags: ["c#", ".net", "Data table", "mysqlDataReader", "Data Table loads", "Data Table loads only last record of mysqlDataReader"]  
reading_time: 1 minute  

---

# Data Table loads only last record of mysqlDataReader

In my [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) mysqlDataReader returns 4 records.Whenever I load this mysqlDataReader to [DataTable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) using

DataTable dt = new DataTable();

dt.Clear();

dt.Load(dr);

DataTable [shows](https://yourviews.mindstick.com/view/81380/new-york-violent-shooting-shows-people-are-not-safe-in-night-life) only last record.

my [Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) is:

DataTable dt = new DataTable();

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

{

[CheckBox](https://www.mindstick.com/articles/291/how-should-use-checkbox-control-in-vb-dot-net) cb = (CheckBox)gvDemoBatches.Rows[i].FindControl("checkSelect");

if (cb.Checked == [true](https://yourviews.mindstick.com/view/81326/boycott-chinese-products-dream-will-come-true))

{

Panel2.Visible = true;

objBEBatch.BatchID = Convert.ToInt32((([Label](https://www.mindstick.com/articles/12835/print-label-tracking-how-do-these-features-make-auspost-woocommerce-plugin-better))gvDemoBatches.Rows[i].Cells[0].FindControl("lblBatchId")).Text);

MySqlDataReader dr = objBLAddNewDemo.GetParticularDemoData(objBEBatch);

dt.Clear();

dt.Load(dr);

}

}

"GetParticularDemoData" [stored Procedure](https://www.mindstick.com/articles/803/using-stored-procedure-in-asp-dot-net) Returns 7 rows.but datatable shows only last record.

## Replies

### Reply by Sumit Kesarwani

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)


---

Original Source: https://www.mindstick.com/forum/2218/data-table-loads-only-last-record-of-mysqldatareader

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
