SqlDataReader Error: Invalid attempt to call MetaData when reader is closed in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

Invalid attempt to call MetaData when reader is closed.

Error occured in line no 30

namespace Chipsmart
{
    public partial class WebForm19 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);       
        SqlCommand cmd11 = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            checkDate();
            if (!this.IsPostBack)
            {               
                con.Open();
                SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT AddRaw_199.Id,AddRaw_199.Name,AddRaw_199.Contact,AddRaw_199.District,AddRaw_199.Address,AddRaw_199.State,AddRaw_199.Status,AddRaw_199.Email,AddRaw_199.PoQuantity,Company_Data199.PQuantity,Total=AddRaw_199.PoQuantity*Company_Data199.PQuantity FROM AddRaw_199 INNER JOIN Company_Data199 ON AddRaw_199.Id = Company_Data199.Id where AddRaw_199.Status = 0 and AddRaw_199.Company_Email = Company_Data199.Email and PoQuantity != 0 ", con);
                SqlDataReader dr = cmd1.ExecuteReader();
                gvData.DataSource = dr;
                gvData.CssClass = "table table-bordered table-highlight";
                gvData.DataBind();
                con.Close();
                try
                {
                    con.Open();
                    cmd11.Connection = con;
                    cmd11.CommandType = CommandType.Text;
                    cmd11.CommandText = "select Name,Address,District,State,Contact from Company_Data199 where Email='" + Session["Com"].ToString() + "'";
                    SqlDataReader dr1 = cmd11.ExecuteReader();                   
                    if (dr1.HasRows)
                    {
                        if (dr1.Read())
                        {                         
                            n1.Text = dr["Name"].ToString();
                            sta.Text = dr["State"].ToString();
                            dis.Text = dr["District"].ToString();
                            add.Text = dr["Address"].ToString();
                            cont.Text = dr["Contact"].ToString();                         
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }
                finally
                {
                    con.Close();
                }
            }
            try
            {
                con.Open();
                cmd11.Connection = con;
                cmd11.CommandType = CommandType.Text;
                cmd11.CommandText = "select Name,Address,District,State,Contact from Company_Data199 where Email='" + Session["Com"].ToString() + "'";
                SqlDataReader dr = cmd11.ExecuteReader();
                if (dr.HasRows)
                {
                    if (dr.Read())
                    {
                        n1.Text = dr["Name"].ToString();
                        sta.Text = dr["State"].ToString();
                        dis.Text = dr["District"].ToString();
                        add.Text = dr["Address"].ToString();
                        cont.Text = dr["Contact"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
    }
}

 

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi VinitaAkha,

You have closed dr connection and you are tring to read data from dr while you have read data in dr1 so you need to get data using dr1.

Refer below code.

Code

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString);
SqlCommand cmd11 = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
    checkDate();
    if (!this.IsPostBack)
    {
        con.Open();
        SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT AddRaw_199.Id,AddRaw_199.Name,AddRaw_199.Contact,AddRaw_199.District,AddRaw_199.Address,AddRaw_199.State,AddRaw_199.Status,AddRaw_199.Email,AddRaw_199.PoQuantity,Company_Data199.PQuantity,Total=AddRaw_199.PoQuantity*Company_Data199.PQuantity FROM AddRaw_199 INNER JOIN Company_Data199 ON AddRaw_199.Id = Company_Data199.Id where AddRaw_199.Status = 0 and AddRaw_199.Company_Email = Company_Data199.Email and PoQuantity != 0 ", con);
        SqlDataReader dr = cmd1.ExecuteReader();
        gvData.DataSource = dr;
        gvData.DataBind();
        con.Close();
        try
        {
            con.Open();
            cmd11.Connection = con;
            cmd11.CommandType = CommandType.Text;
            cmd11.CommandText = "select Name,Address,District,State,Contact from Company_Data199 where Email='" + Session["Com"].ToString() + "'";
            SqlDataReader dr1 = cmd11.ExecuteReader();
            if (dr1.HasRows)
            {
                if (dr1.Read())
                {
                    n1.Text = dr1["Name"].ToString();
                    sta.Text = dr1["State"].ToString();
                    dis.Text = dr1["District"].ToString();
                    add.Text = dr1["Address"].ToString();
                    cont.Text = dr1["Contact"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        finally
        {
            con.Close();
        }
    }
    try
    {
        con.Open();
        cmd11.Connection = con;
        cmd11.CommandType = CommandType.Text;
        cmd11.CommandText = "select Name,Address,District,State,Contact from Company_Data199 where Email='" + Session["Com"].ToString() + "'";
        SqlDataReader dr = cmd11.ExecuteReader();
        if (dr.HasRows)
        {
            if (dr.Read())
            {
                n1.Text = dr["Name"].ToString();
                sta.Text = dr["State"].ToString();
                dis.Text = dr["District"].ToString();
                add.Text = dr["Address"].ToString();
                cont.Text = dr["Contact"].ToString();
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        con.Close();
    }
}