高手帮忙看下下面的这段代码,为什么删除新闻分类时候,提示删除成功可实际上没有删除呢!
Category category = new Category();
protected void gvCategoryList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.gvCategoryList.EditIndex = -1;
SetBind();
}
protected void gvCategoryList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if((e.Row .RowState ==DataControlRowState .Normal ||e.Row .RowState ==DataControlRowState .Alternate)&&e.Row .RowType ==DataControlRowType.DataRow)
{
LinkButton lb = e.Row .Cells [0].Controls[2] as LinkButton ;
lb.Attributes.Add("onclick","return confirm('确定删除该新闻吗?')");
}
}
protected void gvCategoryList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if(this.gvCategoryList.EditIndex ==-1)
{
try
{
category.CategoryId = Convert.ToInt32(this .gvCategoryList.DataKeys [e.RowIndex ].Value);
category.Delete();//调用下面类中的方法删除:这句好像就不执行直接跳到下一句了弹出“删除成功!”,不过我本人也不是太确定,望大家好好看看帮帮我,这个问题好长时间了。
WebHelper.AlertAndRefresh("删除成功");
}
catch (Exception ex)
{
WebHelper.Alert(ex.Message .ToString ());//不用看这
}
}
}
下面这个是个类
public class Category
{
private int m_CategoryId; //分类Id
private string m_CategoryName; //分类名称 public int CategoryId
{
get
{
return m_CategoryId;
}
set
{
m_CategoryId = value;
}
}
public string CategoryName
{
get
{
return m_CategoryName;
}
set
{
m_CategoryName = value;
}
}
public void Delete()
{
Database db = DatabaseFactory.CreateDatabase(); string strSql2 = "DELETE * FROM News_News WHERE CategoryId='" + this.m_CategoryId + "'";
string strSql = "delete * from News_Category where CategoryId='" + this.m_CategoryId + "'"; DbCommand cmd2 = db.GetSqlStringCommand(strSql2);
DbCommand cmd = db.GetSqlStringCommand(strSql);
using(DbConnection conn = db.CreateConnection())
{
conn.Open();
DbTransaction trans = conn.BeginTransaction();
try
{
db.ExecuteNonQuery(cmd2);
db.ExecuteNonQuery(cmd);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
this.ErrMsg = ex.Message.ToString();
} finally
{
cmd.Dispose();
cmd2.Dispose ();
conn.Close();
conn.Dispose();
}
}
}
}
Category category = new Category();
protected void gvCategoryList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.gvCategoryList.EditIndex = -1;
SetBind();
}
protected void gvCategoryList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if((e.Row .RowState ==DataControlRowState .Normal ||e.Row .RowState ==DataControlRowState .Alternate)&&e.Row .RowType ==DataControlRowType.DataRow)
{
LinkButton lb = e.Row .Cells [0].Controls[2] as LinkButton ;
lb.Attributes.Add("onclick","return confirm('确定删除该新闻吗?')");
}
}
protected void gvCategoryList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if(this.gvCategoryList.EditIndex ==-1)
{
try
{
category.CategoryId = Convert.ToInt32(this .gvCategoryList.DataKeys [e.RowIndex ].Value);
category.Delete();//调用下面类中的方法删除:这句好像就不执行直接跳到下一句了弹出“删除成功!”,不过我本人也不是太确定,望大家好好看看帮帮我,这个问题好长时间了。
WebHelper.AlertAndRefresh("删除成功");
}
catch (Exception ex)
{
WebHelper.Alert(ex.Message .ToString ());//不用看这
}
}
}
下面这个是个类
public class Category
{
private int m_CategoryId; //分类Id
private string m_CategoryName; //分类名称 public int CategoryId
{
get
{
return m_CategoryId;
}
set
{
m_CategoryId = value;
}
}
public string CategoryName
{
get
{
return m_CategoryName;
}
set
{
m_CategoryName = value;
}
}
public void Delete()
{
Database db = DatabaseFactory.CreateDatabase(); string strSql2 = "DELETE * FROM News_News WHERE CategoryId='" + this.m_CategoryId + "'";
string strSql = "delete * from News_Category where CategoryId='" + this.m_CategoryId + "'"; DbCommand cmd2 = db.GetSqlStringCommand(strSql2);
DbCommand cmd = db.GetSqlStringCommand(strSql);
using(DbConnection conn = db.CreateConnection())
{
conn.Open();
DbTransaction trans = conn.BeginTransaction();
try
{
db.ExecuteNonQuery(cmd2);
db.ExecuteNonQuery(cmd);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
this.ErrMsg = ex.Message.ToString();
} finally
{
cmd.Dispose();
cmd2.Dispose ();
conn.Close();
conn.Dispose();
}
}
}
}
string strSql2 = "DELETE * FROM News_News WHERE CategoryId='" + this.m_CategoryId + "'";
CategoryId是int型不用''--看看是不是这里
string strSql2 = "DELETE * FROM News_News WHERE CategoryId=" + this.m_CategoryId ;
string strSql = "delete * from News_Category where CategoryId="+ this.m_CategoryId;
可好像还是不行啊!