ASP.net C# ConnectionString 属性 尚未初始化
ASP.net C# The ConnectionString property has not been initialized
我有一个带有“编辑”按钮的网格视图。当用户单击该按钮时,gridview 的内容(显示 tbl_userContents table)会转到一些文本框,然后他可以更改内容并单击保存按钮。当他点击保存按钮时,编辑的内容被插入到tbl_contents table 并且该内容的记录将从tbl_userContents.
中删除
第一部分(插入 tbl_contents table)有效...
但第二部分(从 tbl_userContents 中删除)不起作用并且 ex.Message 显示此错误:ConnectionString 属性 尚未初始化
你能帮我解决这个问题吗?!
这是我的代码:
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();
}
}
这是我的网络配置:
<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" />
你的程序是这样的:
最初您将 cn 定义为 SQL 连接的对象。 cn 已初始化。但是在你的按钮事件(第一个函数)中它工作得很好,然后在你的 finally 块中因为 cn.Close(),cn 现在有空值。所以它不适用于其他功能。
public partial class manager_usercontents : System.Web.UI.Page
{
string connectStr=ConfigurationManager.ConnectionStrings["PP"].ConnectionString;
SqlConnection cn;
然后每次在您的点击事件中,在开头写下以下行:
cn =new SQLConnection(connectStr);
我有一个带有“编辑”按钮的网格视图。当用户单击该按钮时,gridview 的内容(显示 tbl_userContents table)会转到一些文本框,然后他可以更改内容并单击保存按钮。当他点击保存按钮时,编辑的内容被插入到tbl_contents table 并且该内容的记录将从tbl_userContents.
中删除第一部分(插入 tbl_contents table)有效... 但第二部分(从 tbl_userContents 中删除)不起作用并且 ex.Message 显示此错误:ConnectionString 属性 尚未初始化
你能帮我解决这个问题吗?!
这是我的代码:
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();
}
}
这是我的网络配置:
<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" />
你的程序是这样的:
最初您将 cn 定义为 SQL 连接的对象。 cn 已初始化。但是在你的按钮事件(第一个函数)中它工作得很好,然后在你的 finally 块中因为 cn.Close(),cn 现在有空值。所以它不适用于其他功能。
public partial class manager_usercontents : System.Web.UI.Page
{
string connectStr=ConfigurationManager.ConnectionStrings["PP"].ConnectionString;
SqlConnection cn;
然后每次在您的点击事件中,在开头写下以下行:
cn =new SQLConnection(connectStr);