首页上一页 1 下一页尾页 1 条记录 1/1页
删除失败! 看看怎么圆回事
发表在ASP.NET图书答疑
2012-04-08
是否精华
是
否
版块置顶:
是
否
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class admin_bgguangao_guanggao : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断当前页面是否是首次加载
if (!this.IsPostBack)
{
//如果是首次加载数据绑定
this.BindGrid();
}
}
private void BindGrid()
{
//创建使用Windows登陆的 SqlConnection对象
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
SqlCommand com = con.CreateCommand();
com.CommandText = "SELECT * FROM showinfomage";
//创建数据适配器对象
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.SelectCommand = com;
//填充DataSet
DataSet ds = new DataSet();
adpt.Fill(ds);
//数据绑定
this.GridView1.DataSource = ds;
//this.GridView1.DataKeyNames = new string[] {" ID"};
this.GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sql = "DELETE FROM [showinfomage] WHERE [ID] =' "+ GridView1.DataKeys[e.RowIndex].ToString() +"'";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
SqlCommand com = new SqlCommand( sql, conn);
int ad=0;
try
{
conn.Open();
ad= com.ExecuteNonQuery();
}
catch (SqlException ex)
{
//return -1;
}
finally
{
conn.Close();
}
//bool rows = sql;
//bool il= GridView1.DataKeys [e.RowIndex ].ToString ();
if (ad==1)
{
Response.Write("<script lanuage=javescript>alert('删除成功!')</script>");
}
else
{
Response.Write("<script lanuage=javescript>alert('删除失败!')</script>");
}
GridView1.DataBind();
}
//}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Response.Redirect("~/admin/bgguangao/ggupdate.aspx");
}
}
删除失败!!GridView1.DataKeyNames已经设置为ID,数据库也设置了主键
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class admin_bgguangao_guanggao : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断当前页面是否是首次加载
if (!this.IsPostBack)
{
//如果是首次加载数据绑定
this.BindGrid();
}
}
private void BindGrid()
{
//创建使用Windows登陆的 SqlConnection对象
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
SqlCommand com = con.CreateCommand();
com.CommandText = "SELECT * FROM showinfomage";
//创建数据适配器对象
SqlDataAdapter adpt = new SqlDataAdapter();
adpt.SelectCommand = com;
//填充DataSet
DataSet ds = new DataSet();
adpt.Fill(ds);
//数据绑定
this.GridView1.DataSource = ds;
//this.GridView1.DataKeyNames = new string[] {" ID"};
this.GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sql = "DELETE FROM [showinfomage] WHERE [ID] =' "+ GridView1.DataKeys[e.RowIndex].ToString() +"'";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
SqlCommand com = new SqlCommand( sql, conn);
int ad=0;
try
{
conn.Open();
ad= com.ExecuteNonQuery();
}
catch (SqlException ex)
{
//return -1;
}
finally
{
conn.Close();
}
//bool rows = sql;
//bool il= GridView1.DataKeys [e.RowIndex ].ToString ();
if (ad==1)
{
Response.Write("<script lanuage=javescript>alert('删除成功!')</script>");
}
else
{
Response.Write("<script lanuage=javescript>alert('删除失败!')</script>");
}
GridView1.DataBind();
}
//}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Response.Redirect("~/admin/bgguangao/ggupdate.aspx");
}
}
删除失败!!GridView1.DataKeyNames已经设置为ID,数据库也设置了主键