1.11M Members

There is already an open DataReader associated with this Command which must be closed

 
0
 

i am using visual studio for making website using c#
an error is occuring "There is already an open DataReader associated with this Command which must be closed"
the code is

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Getregioncategory();
    }
    public void Getregioncategory()
    {
        DataTable dt = new DataTable();

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

 SqlCommand cmd = new SqlCommand("TMS_GetRegion", conn);
cmd.CommandType = CommandType.StoredProcedure;
try
{
    conn.Open();

    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.HasRows)
    {
        this.gvwregioncategory.DataSource = cmd.ExecuteReader();

        this.gvwregioncategory.DataBind();
    }
}
catch (Exception ex)
{

    string msg = ex.Message;

    Response.Write(msg);

    Response.End();

}

finally
{

    conn.Close();

    cmd.Dispose();

}

}

    }







 <form id="form1" runat="server">
    <div>

<asp:GridView ID="gvwregioncategory" runat="server" AutoGenerateColumns="False" CssClass="basix" AllowPaging="True" >
<columns>
<asp:BoundField DataField="Region" HeaderText="region" />
<asp:BoundField DataField="Regionid" HeaderText="region_id" />
</columns>
</asp:GridView> 
<asp:label ID="lblStatus" runat="server"></asp:label>

    </div>
    </form>
 
0
 

1) Please use (code) tags to enclose your code segments as it makes them much easier to read :)
2) Judging by "ConfigurationManager.ConnectionStrings" this should be in ASP.Net

Now then...

You can approach it one of two ways...

Change your code to reflect this:

if (reader.HasRows)
{
    reader.Close();

    this.gvwregioncategory.DataSource = cmd.ExecuteReader();

    this.gvwregioncategory.DataBind();
}

or to this:

if (reader.HasRows)
{

    this.gvwregioncategory.DataSource = reader;

    this.gvwregioncategory.DataBind();
}

The way you have it currently = cmd.ExecuteReader(); occurs both outside and inside your try/catch/finally segment effectively opening the reader twice causing your error.

Hope that helps :) Please remember to mark the thread solved once your issue is resolved.

 
1
 

Firstly, please use [code]

[/code] tags when posting code to preserve the formatting and make it readable.

As to your problem, i believe it stems from calling cmd.ExecuteReader() twice. You load the data into your SqlDataReader, check it has rows then try to get the dataset again. You already have the data in your DataReader so use that.
I havent checked, but im sure you can do something like:

this.gvwregioncategory.DataSource = reader;

EDIT: pipped at the post...great minds and all that :p

 
0
 

pipped at the post...great minds and all that :p

Haha no doubt :twisted: if not for minor format variations I'd swear we were reading each other's minds when we wrote those.

Question Answered as of 4 Years Ago by Lusiphur and Ryshad
 
0
 

thanks lusiphur :)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article