2015-08-08 127 views
0

您好,我正在尝试使用c#asp.net更新我的数据库时出现以下错误。ConnectionString属性尚未初始化使用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; 
      } 
     } 
    } 
} 

我收到上述错误healthCommentBL.cs文件中捕捉statement.Here我可以说,commentstring正确的getHealthCommentDetails方法在工作healthCommentDL.cs文件,但同时它不适用于此文件的第二种方法。请帮我解决这个问题错误。

+0

在你的'updateStatusDetails'方法中使用你的sql连接作为局部变量。 –

+0

@ Soner:我很困惑,你能写出你的答案吗? – satya

+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); 

有几件事情多;

相关问题