-3

Here i use my webmethod.But when i going to connect it shows this error.But my code is good i think.

ExecuteReader requires an open and available Connection. The connection's current state is connecting.

MyCode

public static List<CommonPages> GetCommonPagesDescription(int Type)
{
    List<CommonPages> CommonPageDescription = new List<CommonPages>();
    try
    {
        SqlCommand comGetAllFiles = new SqlCommand("GetCommonPageDescriptions", conDB);
        comGetAllFiles.CommandType = CommandType.StoredProcedure;
        if (conDB.State == ConnectionState.Closed)
            conDB.Open(); // <-- Debugger Skip this & goto next line
        comGetAllFiles.Parameters.Add("@Type", SqlDbType.Int);
        comGetAllFiles.Parameters["@Type"].Value = Type;

        SqlDataReader rdr = comGetAllFiles.ExecuteReader();//<-- error generating here
        DataTable dt = new DataTable();
        dt.Load(rdr);
        foreach (DataRow r in dt.Rows)
        {
            CommonPageDescription.Add(new CommonPages
            {
                Id = (int)r["Id"],
                Description = r["Description"].ToString(),
                Type = (int)r["Type"],
                UpdatedDate = (DateTime)r["UpdatedDate"],
                UpdatedBy = (Guid)r["UpdatedBy"]

            });
        }

    }
    catch (Exception ee)
    {
    }
    finally
    {
        conDB.Close();
    }
    return CommonPageDescription;
}

conDB Initialized here

 static SqlConnection conDB = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
TechGuy
  • 4,298
  • 15
  • 56
  • 87

2 Answers2

1

conDB must be a shared connection? Probably not a good idea, leverage connection pooling.

To fix it, consider opening and closing a connection per request. Don't care if the voodoo sayers that this inefficient, in truth you want to open/close connections as little as possible but sometimes you need to go to the DB for a single reason. There are ways you could share a connection better, using a context pattern for example. But to solve your immediate problem structure your DB calls this way.

try
{
  using( System.Data.Common.DbConnection conn = CreateConnection() )
  {
     //create your command...
     //create your reader/or execute your command...
  }
}
T McKeown
  • 12,971
  • 1
  • 25
  • 32
0

Instead of opening the connection when it is closed, try to open the connection when it is not opened.

public static List<CommonPages> GetCommonPagesDescription(int Type)
{
    List<CommonPages> CommonPageDescription = new List<CommonPages>();
    try
    {
        SqlCommand comGetAllFiles = new SqlCommand("GetCommonPageDescriptions", conDB);
        comGetAllFiles.CommandType = CommandType.StoredProcedure;
        if (conDB.State != ConnectionState.Open)
            conDB.Open(); // <-- Debugger Skip this & goto next line
        comGetAllFiles.Parameters.Add("@Type", SqlDbType.Int);
        comGetAllFiles.Parameters["@Type"].Value = Type;

        SqlDataReader rdr = comGetAllFiles.ExecuteReader();//<-- error generating here
        DataTable dt = new DataTable();
        dt.Load(rdr);
        foreach (DataRow r in dt.Rows)
        {
            CommonPageDescription.Add(new CommonPages
            {
                Id = (int)r["Id"],
                Description = r["Description"].ToString(),
                Type = (int)r["Type"],
                UpdatedDate = (DateTime)r["UpdatedDate"],
                UpdatedBy = (Guid)r["UpdatedBy"]

            });
        }

    }
    catch (Exception ee)
    {
    }
    finally
    {
        conDB.Close();
    }
    return CommonPageDescription;
}
Sailesh Babu Doppalapudi
  • 1,534
  • 1
  • 10
  • 22