删除代码:
// 处理删除事件
public void DataGrid_Delete(object sender, DataGridCommandEventArgs e)
{
// 构造SQL语句
String strSQL="delete from 供求信息 where id=@id"; //连接字符串
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection  conn = new SqlConnection(con); // 创建Command对象cm
SqlCommand cm=new SqlCommand(strSQL,conn); // 给参数赋值
cm.Parameters.Add(new SqlParameter("@id",SqlDbType.BigInt));
cm.Parameters["@id"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex]; // 打开连接
cm.Connection.Open(); try 
{
cm.ExecuteNonQuery();
Label1.Text="删除成功";
}
catch (SqlException) 
{
Label1.Text="删除失败";
} // 关闭连接
cm.Connection.Close();
// 更新DataGrid
BindGrid();
}问题:
我点击一次删除,怎么会同时删除掉上面的一条记录啊,比如我本是删除ID为9的记录,但它把ID为8的记录也删除掉了。是代码的错误吗?
望高手能够赐教!!!!!!!!!!

解决方案 »

  1.   

    可能是IsPostBack 之类的问题 
    看你上面函数调用的时机
      

  2.   

    全部代码,希望有人帮忙,一个下午,都没有看出来是什么问题,其实是在删除一条记录的时候,前后的两条记录也被删除了,为什么asp.net这么难控制的啊!using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace cibs.system
    {
    /// <summary>
    /// gqxxList 的摘要说明。
    /// </summary>
    public class gqxxList : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!Page.IsPostBack)
    {
    BindGrid();
    }
    }
    // 处理删除事件
    public void DataGrid_Delete(object sender, DataGridCommandEventArgs e)
    {
    // 构造SQL语句
    String strSQL="delete from 供求信息 where id=@id"; //连接字符串
    string con=ConfigurationSettings.AppSettings["dsn"];
    SqlConnection  conn = new SqlConnection(con); // 创建Command对象cm
    SqlCommand cm=new SqlCommand(strSQL,conn); // 给参数赋值
    cm.Parameters.Add(new SqlParameter("@id",SqlDbType.BigInt));
    cm.Parameters["@id"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex]; // 打开连接
    cm.Connection.Open(); try 
    {
    cm.ExecuteNonQuery();
    Label1.Text="删除成功";
    }
    catch (SqlException) 
    {
    Label1.Text="删除失败";
    } // 关闭连接
    cm.Connection.Close();
    // 更新DataGrid
    BindGrid();
    }
    //处理分页事件
    public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
    } //绑定DataGrid
    public void BindGrid()
    {
    string con=ConfigurationSettings.AppSettings["dsn"];
    SqlConnection  conn = new SqlConnection(con);//连接字符串
    conn.Open();
            
    SqlDataAdapter myCommand = new SqlDataAdapter();  //创建SqlDataAdapter 类
          myCommand.SelectCommand=new SqlCommand("admin_selectGqxx",conn);
    myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
    //SqlParameter classname=myCommand.SelectCommand.Parameters.Add("@id",SqlDbType.Char,200);
    //classname.Value =Request["classid"].ToString().Trim();

    DataSet ds=new DataSet();  //建立并填充数据集
    myCommand.Fill(ds,"供求信息");

    DataGrid1.DataSource=ds;
    DataGrid1.DataBind(); conn.Close();
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid_Delete);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  3.   

    你在private void Page_Load(object sender, System.EventArgs e)
    public void DataGrid_Delete(object sender, DataGridCommandEventArgs e)
    设俩个断点,然后看一下怎么会调用DataGrid_Delete3次的。
    行不行你自己试试,帮你顶一下!
      

  4.   

    我做了个类似实例(与你的代码相同),没问题~~!你可根据以下几点去测试一下:
    1、把DataGrid_Delete(object sender, DataGridCommandEventArgs e)
       里面相关的数据库操作先注析掉,打一个简单的Response.Write....,
       看看有什么逻辑上的问题!
    2、检查你的存储过程!
    3、重(新)写一边DataGrid_Delete(object sender, DataGridCommandEventArgs e)方法
    4、自己写一个Button试试看根据以上几点,前后对比一下,估计你的问题就解决了!GoodLuck!
      

  5.   

    樓主﹐你不用DataGrid中的Delete在頁面另外加一個刪除按鈕在DataGrid中添加一列復選框﹐然后根據復選框這一列判斷是否選中?﹐根據DataGrid中的單元格的序號循環找出選中的行將它們刪除
      

  6.   

    public void DataGrid_Delete(object sender, DataGridCommandEventArgs e)
    {
    // 构造SQL语句
    String strSQL="delete from 供求信息 where id=" + e.item.cell(0).text;//连接字符串
    string con=ConfigurationSettings.AppSettings["dsn"];
    SqlConnection  conn = new SqlConnection(con);// 创建Command对象cm
    SqlCommand cm=new SqlCommand(strSQL,conn);// 给参数赋值// 打开连接
    cm.Connection.Open();try 
    {
    cm.ExecuteNonQuery();
    Label1.Text="删除成功";
    }
    catch (SqlException) 
    {
    Label1.Text="删除失败";
    }// 关闭连接
    cm.Connection.Close();
    // 更新DataGrid
    BindGrid();
      

  7.   

    检查一下你的private void InitializeComponent()
      

  8.   

    把DataGrid_Delete(object sender, DataGridCommandEventArgs e)
       里面相关的数据库操作先注析掉,打一个简单的Response.Write....,按照这个方法,发现 DataGrid_Delete 的确是运行了两次。是哪里出错了呢,继续检查,呵呵.......
      

  9.   

    问题初步解决:
    private void InitializeComponent()
    {    
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid_Delete);
    this.Load += new System.EventHandler(this.Page_Load);}把
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid_Delete);
    删掉,问题解决,初步判断就是系统自动生成的这句代码出的问题。
    具体原因正在查找中