0

Hi i am getting the following error while trying to update my database using c# asp.net.

Error:

Server Error in '/' Application.

The ConnectionString property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error: 


Line 33:                 catch (Exception e)
Line 34:                 {
Line 35:                     throw e;
Line 36:                 }
Line 37:         }

I am explaining my code below.

index.aspx.cs:

 protected void reject_Click(object sender, EventArgs e)
        {
            //LinkButton lbtn = (LinkButton)(sender);
            //lbtn.BackColor = System.Drawing.Color.Red;
            GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
            LinkButton lbtn = (LinkButton)grdrow.FindControl("accept");
            LinkButton LRejectBtn = (LinkButton)grdrow.FindControl("reject");
           // string status = grdrow.Cells[6].Text;
            int healthId = int.Parse(lbtn.CommandArgument);
            int result=0;
            if (Convert.ToString(lbtn.BackColor) == "Color [Green]")
            {
                char updatedStatus = 'R';
                result = objhealthCommentBL.updateStatusDetails(updatedStatus, healthId);
                if (result == 1)
                {
                    LRejectBtn.BackColor = System.Drawing.Color.Red;
                    lbtn.BackColor = System.Drawing.Color.WhiteSmoke;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Your status has updated successfully.')", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Your status couldnot updated')", true);
                }
            }
        }

healthCommentBL.cs:

public int updateStatusDetails(char updatedStatus, int healthId)
        {
                int result;
                try
                {
                    result = objhealthCommentDL.updateStatusDetails(updatedStatus, healthId);
                    return result;
                }
                catch (Exception e)
                {
                    throw e;
                }
        }

healthCommentDL.cs:

namespace DataAccess
{
    public class healthCommentDL
    {
        SqlConnection con = new SqlConnection(CmVar.convar);
        public DataSet getHealthCommentDetails()
        {
            try
            {
                con.Open();
                DataSet ds = new DataSet();
                string sql = "SELECT Health_Comment_ID,Health_ID,Health_Comment_Name,Health_comment_Email,Health_Comment_Message,Health_Comment_Website,Health_Comment_Status from T_Health_Comment";
                sql += " order by Health_Comment_ID ASC ";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter objadp = new SqlDataAdapter(cmd);
                objadp.Fill(ds);
                return ds;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
        public int updateStatusDetails(char updatedStatus, int healthId)
        {
            int result;
            try
            {
                con.Open();
                string query = "UPDATE T_Health_Comment SET Health_Comment_Status = @status WHERE Health_Comment_ID = @healthid";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@healthid", healthId);
                cmd.Parameters.AddWithValue("@status", updatedStatus);
                result = cmd.ExecuteNonQuery();
                con.Close();
                return result;

            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}

I am getting the above error in healthCommentBL.cs file in catch statement.Here i can say that the commentstring is properly working in the getHealthCommentDetails method in healthCommentDL.cs file but at the same time it is not working for the 2nd method of this file.Please help me to resolve this error.

3
  • Use your sql connection as a local variable in your updateStatusDetails method. Commented Aug 8, 2015 at 11:39
  • @ Soner : I am confusing .Can you write your answer ? Commented Aug 8, 2015 at 12:14
  • I just did. Take a look. Commented Aug 8, 2015 at 12:35

1 Answer 1

1

When you write your connection as;

public class healthCommentDL
{
    SqlConnection con = new SqlConnection(CmVar.convar);

It will be a field of healthCommentDL class, not a local variable. And it's properties (like ConnectionString) is not initialiazed. Instead of that, define your connections as a local variables in your methods. ADO.NET is pretty good at maintaining your connections as a local variables. Read: SQL Server Connection Pooling

public DataSet getHealthCommentDetails()
{
    SqlConnection con = new SqlConnection(CmVar.convar);

and

public int updateStatusDetails(char updatedStatus, int healthId)
{
    SqlConnection con = new SqlConnection(CmVar.convar);

A few things more;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.