Question

i have created a 3tier application.. where i want to call the update method which connect the database and update the records accordingly.

below is my database access layer.

public class DataLogic
{
    public DataLogic()
    {
    }

    public SqlConnection ConnectDatabase
    {
        get { return new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString); }
    }

    public int UpdateArticle(BusinessLogic b, int ArticleId)
    {
        int updateExecuted = -1;
        StringBuilder formParamString = new StringBuilder();
        formParamString.Append("IsArticlePaging=" + b.IsPagingEnable + " ");
        string updateString = "update crossarticle_article set " + formParamString.ToString() + "where id = " + ArticleId + "";
        try
        {
            using (SqlCommand comUpdateArticle = new SqlCommand(updateString, ConnectDatabase))
            {
                ConnectDatabase.Open();
                updateExecuted = comUpdateArticle.ExecuteNonQuery();
            }
        }
        catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); }
        finally { ConnectDatabase.Close(); }
        return updateExecuted;
    }
}

below is my business logic layer

public class BusinessLogic
{
    DataLogic dLogic = new DataLogic();
    public BusinessLogic()
    {

    }
    private bool _IsPagingEnable;

    public bool IsPagingEnable
    {
        get { return _IsPagingEnable; }
        set { _IsPagingEnable = value; }
    }
    private int _articleID;
    public int ArticleID
    {
        get { return _articleID; }
        set { _articleID = value; }
    }

    public int UpdateExtraFieldArticle()
    {
        return dLogic.UpdateArticle(this, ArticleID);
    }
}

now when i create the BusinessLogic object and call the update method, it calls the DataLogic's update method as expected, but before updating the database it throws error saying, ExecuteNonQuery requires open and available connection. but i have already opened the connection.

Please any one help me regarding the sqlconnection.

Was it helpful?

Solution

The problem is you are opening two separate connections. Try the following:

using (sqlConnection connection = ConnectDatabase)
{
    using (SqlCommand comUpdateArticle = new SqlCommand(updateString, connection))
    {
        connection.Open();
        updateExecuted = comUpdateArticle.ExecuteNonQuery();
    }
}

OTHER TIPS

Your ConnectDatabase always returns a new Connection:

public SqlConnection ConnectDatabase
{
 get { return new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString); }
}

You should use a local variable and initialize it once from a factory-method:

public SqlConnection CreateConnection
{
    return new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
}

For example:

using (SqlConnection con = CreateConnection())
{
   using (SqlCommand comUpdateArticle = new SqlCommand(updateString, con))
   {
      con.Open();
      updateExecuted = comUpdateArticle.ExecuteNonQuery();
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top