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>

Recommended Answers

All 4 Replies

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.

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

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.

thanks lusiphur :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.