我有两个主从表,主表删除是没问题的~~删除的时候从表也能一起删掉~~但为什么我从表单个删除的时候~却总是提示下面这个错误啊?其中GirdView1是从表,GirdView3是主表,下面是我的代码~~有谁能帮我看一下吗?从表里面我wp_no  xs_no都设过主键的
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection sqlConnection1 = new SqlConnection(myStc);
        string sql;
       
        sql = "Delete from xsd_zc  Where wp_no='"+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
      
        SqlCommand cmd1 = new SqlCommand(sql, sqlConnection1);
        try
        {
            sqlConnection1.Open();
         
            cmd1.ExecuteNonQuery();
            sqlConnection1.Close();
            sqlConnection1.Dispose();        }
        catch (SqlException)
        {
            Label2.Text = "发生错误,没有正常删除记录";
        }
        string xs_no = Convert.ToString(GridView3.SelectedValue);
        string SQL = "SELECT distinct xs_no,wp_no,wp_name,wp_num,wp_jg,wp_dw FROM [xsd_zc] WHERE [xs_no]='" + xs_no + "'";
        GridView1.DataSource = Binding(SQL);
        GridView1.DataBind();
        GridView1.Visible = true;
    }
    protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection sqlConnection1 = new SqlConnection(myStc);
        string strSql,sql;
        strSql = "Delete from xsd  Where xs_no='" + GridView3.DataKeys[e.RowIndex].Value + "'";
        sql = "Delete from xsd_zc  Where xs_no='" + GridView3.DataKeys[e.RowIndex].Value + "'";
        SqlCommand cmd = new SqlCommand(strSql, sqlConnection1);
        SqlCommand cmd1 = new SqlCommand(sql, sqlConnection1);
        try
        {
            sqlConnection1.Open();
            cmd.ExecuteNonQuery();
            cmd1.ExecuteNonQuery();
            sqlConnection1.Close();
            sqlConnection1.Dispose();        }
        catch (SqlException)
        {
            Label1.Text = "发生错误,没有正常删除记录";
        }
        xsd();  
    }错误提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index源错误: 
行 116:        string sql;
行 117:       
行 118:        sql = "Delete from xsd_zc  Where wp_no='"+ GridView1.DataKeys[e.RowIndex].Value  + "'";
行 119:      
行 120:        SqlCommand cmd1 = new SqlCommand(sql, sqlConnection1);
 

解决方案 »

  1.   

    GridView1.DataKeys[e.RowIndex].Value  这句有问题,可能应该为e.RowIndex-1
      

  2.   

    我觉得也应该是索引index那里的错误
      

  3.   

    改成GridView1.DataKeys[e.RowIndex-1].Value  还是有问题啊~~我刚开始学·~不知道怎么调试啊~~能具体告诉我一下吗?怎么调试?我设置了断点~~不会在那上面停下的
      

  4.   

    GridView1.DataKeys[e.RowIndex].Value  
    5.设置主键 DataKeyNames="userid"
     取值方法
     string key = gv_base.DataKeys[e.RowIndex].Value.ToString();
     多个主键时的设置和取值方法
     设置
     DataKeyNames="userid,username"
     取值
     string key = gv_base.DataKeys[e.RowIndex].Values[0].ToString();
     string key1 = gv_base.DataKeys[e.RowIndex].Values[1].ToString();
    你的主键设置正确吗?设置了几个?
    http://blog.csdn.net/greatverve/archive/2008/12/01/3418755.aspx
    主表删除的时候是不是调用从表的删除函数?
      

  5.   

    从表翻页吗 ,GridView1.DataKeys[e.RowIndex%gridview1.pagesize]试试
      

  6.   

    在你的GridView1的DataKeyNames属性里面设置你对应表的主键名称
    试下