紧急求助ExecuteNonQuery: CommandText 属性尚未初始化
我的后台程序改了一下后,新增可以正常保存,但在现有的内容编辑后,就出现了“ExecuteNonQuery: CommandText 属性尚未初始化 ”
程序如下:
if (Session["action"].ToString() == "edit")
{
try
{//修改数据库
string Edittext = new Common().GetText(txtContent.Text);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ToString());
SqlCommand comm = new SqlCommand("", conn);
//"update Article set"
// + "[News_first_title]='" + tbox_FirTitle.Text + "',"
// + "[News_second_title]='" + tbox_SecTitle.Text + "',"
// + "[News_third_title]='" + tbox_ThdTitle.Text + "',"
// + "[News_time]='" + tbox_Date.Text + "',"
// + "[News_author]='" + tbox_Author.Text + "',"
// + "[News_content]='" + Edittext + "',"
// + "[News_homepage_tag]='" + chk_HomePage.Checked + "',"
// + "[News_photo_tag]='" + chk_Flash.Checked + "',"
// + "[News_affiche_tag]='" + chk_Affiche.Checked + "',"
// + "[News_top_tag]='" + chk_Top.Checked + "',"
// + "[news_public_tag]='" + chk_Public.Checked + "',"
// + "[news_hot_tag]='" + chk_Hot.Checked + "',"
// + "[News_attachment]='" + str_Attachment + "',"
// + "[news_public_type]='" + ddlist_PublicType.SelectedItem.Text + "',"
// + "[news_affiche_time]='" + tbox_Affiche.Text + "',"
// + "[news_group_mark]='" + Session["type"].ToString()
// + "' WHERE [News_id]='" + Session["keys"] + "'"
if (strID != null)
{
for (int ni = 0; ni < strID.Length; ni++)
{
sql += "insert into [Article] ("
+ "[News_first_title],"
...
+ "VALUES ('" + tbox_FirTitle.Text + "',"
+ "'" + tbox_SecTitle.Text + "',"
...
,"
+ "'" + strID[ni] + "'"
+ ");";
}
}
sql = "delete from Article where News_id ='" + Session["keys"] + "';" + sql;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
comm.Dispose();
conn.Dispose();
}
catch (Exception f)
{
Response.Write(f.Message.ToString());
}
}
else
{
try
{//存入数据库
string Edittext = new Common().GetText(txtContent.Text);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ToString());
SqlCommand comm = new SqlCommand("",conn);
if (strID != null)
{
for (int ni = 0; ni < strID.Length; ni++)
{
sql += "insert into [Article] ("
+ "[News_first_title],"
..."
+ "VALUES ('" + tbox_FirTitle.Text + "',"
+ "'" + tbox_SecTitle.Text + "',"
...
+ "'" + tbox_Affiche.Text + "',"
+ "'" + strID[ni] + "'"
+ ");";
}
}
else
{
sql = "insert into [Article] ("
+ "[News_first_title],"
...
+ "[news_group_mark]) "
+ "VALUES ('" + tbox_FirTitle.Text + "',"
+ "'" + tbox_SecTitle.Text + "',"
...
+ "'" + Session["type"].ToString() + "'"
+ ");";
}
comm.CommandText = sql.Substring(0, sql.Length - 1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
comm.Dispose();
conn.Dispose();
}
catch (Exception f)
{ Response.Write(f.Message.ToString()); }
}
Session["typeID"] = null;
Session["typeName"] = null;
ds.Tables[0].Rows[0]["name"] = " ";
ds.WriteXml(Server.MapPath("XML_UpFile.xml"));
Server.Transfer("manager_content_list.aspx", true);
}
protected void btn_Return_Click(object sender, EventArgs e)
{
Server.Transfer("manager_content_list.aspx", true);
}
}
[解决办法]
sql = "delete from Article where News_id ='" + Session["keys"] + "';" + sql;
comm.CommandText = sql;
conn.Open();
comm.ExecuteNonQuery();
[解决办法]