简体   繁体   English

ASP.net C# ConnectionString 属性尚未初始化

[英]ASP.net C# The ConnectionString property has not been initialized

I have a gridview that has a Edit button.我有一个带有编辑按钮的网格视图。 when user click on that button, the contents of the gridview (that shows tbl_userContents table) goes to some textboxes, then he can change the contents and click on save button.当用户单击该按钮时,gridview 的内容(显示 tbl_userContents 表)进入一些文本框,然后他可以更改内容并单击保存按钮。 when he clicks the save button, the edited contents inserted to the tbl_contents table and the record of this content will be deleted from tbl_userContents.当他单击保存按钮时,插入到 tbl_contents 表中的编辑内容以及该内容的记录将从 tbl_userContents 中删除。

the first part (inserting to the tbl_contents table) works... but the second part (deleting from tbl_userContents) doesn't work and ex.Message shows the this error: The ConnectionString property has not been initialized第一部分(插入 tbl_contents 表)有效...但第二部分(从 tbl_userContents 中删除)无效,并且 ex.Message 显示此错误:ConnectionString 属性尚未初始化

would you please help me to solve this problem?!你能帮我解决这个问题吗?!

this is my code:这是我的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.IO;

    public partial class manager_usercontents : System.Web.UI.Page
    {
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["PP"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

}
protected void gvUserContents_SelectedIndexChanged(object sender, EventArgs e)
{

}
    protected void btnEditUserContent_Click(object sender, EventArgs e)
 {
    int id = Int32.Parse(((Button)sender).CommandArgument.ToString());
    Session.Add("ucid", id);
    string sql = "select * from tbl_userContents where ucid=@id";
    try
    {
        SqlCommand cmd = new SqlCommand(sql, cn);
        cmd.Parameters.AddWithValue("@id", id);
        SqlDataReader dr;
        cn.Open();
        dr = cmd.ExecuteReader();
        dr.Read();
        txtEditUserContentTopic.Text = dr["topic"].ToString();
        hfUserContentEdit.Value = dr["contentUrl"].ToString();
        txtEditUserContentNote.Text = dr["contentNote"].ToString();
        cmd.Dispose();
    }
    finally
    {
        cn.Close();
        cn.Dispose();
    }
}
protected void btnsaveEditUserContent_Click(object sender, EventArgs e)
{        
    //first we shoud save the user content in contents table...
    string masir, strfilename, cont = string.Empty, sql;
    try
    {
        if (fuEditUserFileUpload.HasFile)
        {
            masir = HttpContext.Current.Server.MapPath("../contents");
            strfilename = fuEditUserFileUpload.FileName;
            fuEditUserFileUpload.SaveAs(masir + "\\" + strfilename);
            cont = "contents\\" + strfilename;
        }
        else cont = hfUserContentEdit.Value;


        sql = "insert into tbl_contents (topic,contentNote,contentUrl) values(@t,@contentN,@contentU)";
        SqlCommand cmd = new SqlCommand(sql, cn);
        cmd.Parameters.Add("@t", txtEditUserContentTopic.Text);
        cmd.Parameters.Add("@contentN", txtEditUserContentNote.Text);
        cmd.Parameters.Add("@contentU", cont);

        cn.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
    }
    catch (Exception ex)
    {
        lblEditUserContentError.Style.Add("color", "red");
        lblEditUserContentError.Text = "the record does not successfully inserted"   }

    cn.Close();
    cn.Dispose();
    lblEditUserContentError.Style.Add("color", "green");
    lblEditUserContentError.Text = "the record successfully inserted";
    gvUserContents.DataBind();


    //then we should delete the user content record from the tbl_userContents table
    int SessionID = Int32.Parse(Session["ucid"].ToString());
    sql = "delete from tbl_userContents where ucid=@id";
    try
    {
        SqlCommand cmd = new SqlCommand(sql, cn);
        cmd.Parameters.AddWithValue("@id", SessionID);
        cn.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
       // lblDeleteError.Style.Add("color", "red");
        //lblDeleteError.Text = "the record does not deleted successfully.";
       lblDeleteError.Text = ex.Message;

    }
    finally
    {
       // lblDeleteError.Style.Add("color", "green");
       // lblDeleteError.Text = "record deleted successfully";
        gvUserContents.DataBind();
        cn.Close();
    }
}

and this is my webConfig:这是我的 webConfig:

      <connectionStrings>
<add name="PipelineProtection" connectionString="Data Source=MAHSA-PC;Initial Catalog=PipelineProtection;Integrated Security=True" />
<add name="PP" connectionString="Data Source=MAHSA-PC;Initial Catalog=PipelineProtection;Integrated Security=True"
  providerName="System.Data.SqlClient" />

here is what's going in your program:这是您的程序中发生的事情:

Initially you defined cn as an object of SQL connection.最初您将 cn 定义为 SQL 连接的对象。 cn was initialized. cn 已初始化。 But in your button event (first function) it worked perfectly and then in your finally block because of cn.Close(), cn now has null value.但是在您的按钮事件(第一个函数)中,它运行良好,然后由于 cn.Close(),在您的 finally 块中,cn 现在具有空值。 So it is not working in other function.所以它在其他功能中不起作用。

public partial class manager_usercontents : System.Web.UI.Page
{
string connectStr=ConfigurationManager.ConnectionStrings["PP"].ConnectionString;
SqlConnection cn;

then Everytime in your click event write the following line at the start:然后每次在你的点击事件开始时写下一行:

   cn =new SQLConnection(connectStr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尚未使用c#asp.net初始化ConnectionString属性 - The ConnectionString property has not been initialized using c# asp.net 未在ASP.NET中初始化ConnectionString属性 - ConnectionString property has not been initialized IN ASP.NET C#-ConnectionString属性尚未初始化 - C# - The ConnectionString property has not been initialized c# connectionstring 属性尚未初始化 - c# connectionstring property has not been initialized ConnectionString属性还没有初始化执行第二次 asp.net core - The ConnectionString property has not been initialized executing for second time asp.net core 错误在c#中显示“ ConnectionString属性尚未初始化” - The error says “The ConnectionString property has not been initialized” in c# connectionstring 属性尚未初始化。 c#访问数据库 - the connectionstring property has not been initialized. c# Access database C#SqlConnection(IIS问题?)“ ConnectionString属性尚未初始化” - C# SqlConnection (IIS issue?) “The ConnectionString property has not been initialized” C# 数据库 ConnectionString 属性尚未初始化 - C# Database The ConnectionString property has not been initialized c# winform 应用程序中的连接字符串属性尚未初始化错误 - The connectionstring property has not been initialized error in c# winform app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM