---
title: "Invalid attempt to call Read when reader is closed.  ExecuteReader to get multiple table results"  
description: "Invalid attempt to call Read when reader is closed.  ExecuteReader to get multiple table results"  
author: "Anonymous User"  
published: 2013-09-24  
updated: 2013-09-24  
canonical: https://www.mindstick.com/forum/1507/invalid-attempt-to-call-read-when-reader-is-closed-executereader-to-get-multiple-table-results  
category: "c#"  
tags: ["c#", "mindstick", "Invalid attempt", "ExecuteReader", "get multiple table results", "reader is closed. ExecuteReader"]  
reading_time: 2 minutes  

---

# Invalid attempt to call Read when reader is closed.  ExecuteReader to get multiple table results

I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to return a List(Of [DataTable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net)) using a SqlDataReader instead of [filling](https://yourviews.mindstick.com/audio/1303/the-power-of-silence-what-happens-when-you-stop-filling-every-moment) a dataset. I'm getting the "Invalid attempt to call Read when reader is closed" [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) after it retrieves 4 [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development) correctly. Here is the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) I'm using

[Private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) Function ExecuteDS(ByVal SPName As [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp), ByVal ParamList As List(Of System.Data.SqlClient.SqlParameter)) As List(Of System.Data.DataTable)

Dim ds As New List(Of System.Data.DataTable)

Dim dbConStr As [Database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax)

dbConStr = New Database()

Using con As New System.Data.SqlClient.SqlConnection(dbConStr.ReturnString)

Dim cmd As New System.Data.SqlClient.SqlCommand(CStr(SPName), con)

cmd.CommandType = System.Data.CommandType.StoredProcedure

cmd.CommandTimeout = 30

For Each p As System.Data.SqlClient.SqlParameter In ParamList

cmd.Parameters.Add(p)

Next

con.Open()

Using dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader

If dr.HasRows Then

Do While dr.Read()

Dim dt As New System.Data.DataTable

dt.Load(dr)

ds.Add(dt)

Loop

End If

End Using

con.Close()

End Using

Return ds

End Function

The error occurs at dr.Read(). It should return 4 tables but after it gets the 4th, it still goes and tries to do the dr.Read() and throws the error instead of just exiting the loop properly. Any [insight](https://www.mindstick.com/articles/157223/5-inbound-project-management-insights-every-client-needs-to-know), I would appreciate it, thank you.

## Replies

### Reply by Pravesh Singh

Hi Ashish,

You need to not test for HasRows if there is no NextResut

## In C#

That last While will cause it to leave immediately.

```
using (IDataReader reader = ...) {   do   {    if
(reader.HasRows())    {       while
(reader.Read())        {            ....       }    }  } while
(reader.NextResult())}
```


---

Original Source: https://www.mindstick.com/forum/1507/invalid-attempt-to-call-read-when-reader-is-closed-executereader-to-get-multiple-table-results

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
