我有一個具有編輯按鈕的gridview。當用戶點擊該按鈕時,gridview(顯示tbl_userContents表格)的內容會轉到某些文本框,然後他可以更改內容並單擊保存按鈕。當他單擊保存按鈕時,插入到tbl_contents表中的編輯內容和此內容的記錄將從tbl_userContents中刪除。ASP.net C#ConnectionString屬性尚未初始化
第一部分(插入到tbl_contents表)的作品...... 但第二部分(從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 [email protected]";
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 [email protected]";
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();
}
}
,這是我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" />
你爲什麼不定義'btnEditUserContent_Click'方法,而不是內部的連接字符串? –
,因爲我也會在其他一些方法中使用它...所以我在全局中定義它,然後在方法中,我將打開它並關閉它... – sana