ConnectionString属性尚未使用c#asp.net初始化

时间:2015-08-08 11:36:34

标签: c# asp.net

您好我在尝试使用c#asp.net更新数据库时收到以下错误。

错误:

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:         }

我正在解释下面的代码。

  

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;
            }
        }
    }
}

我在catch语句的healthCommentBL.cs文件中收到了上述错误。我可以说,commentstring在getHealthCommentDetails文件中的healthCommentDL.cs方法中正常工作但同时它不适用于此文件的第二种方法。请帮我解决此错误。

1 个答案:

答案 0 :(得分:1)

当您将连接写为;

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

它将是healthCommentDL类的字段,而不是局部变量。它的属性(如ConnectionString)未初始化。而不是那样,将您的连接定义为方法中的局部变量。 ADO.NET非常擅长将您的连接维护为局部变量。阅读:SQL Server Connection Pooling

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

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

还有一些事情;