我有两个主从表,主表删除是没问题的~~删除的时候从表也能一起删掉~~但为什么我从表单个删除的时候~却总是提示下面这个错误啊?其中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);
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);
解决方案 »
- Hibernate还需要学习吗
- 我装了ReSharperSetup.3.1.Full.VS80,在vs2008里的菜单项里也出现了ReSharper菜单,可是无法注册,注册项为灰色,怎么办?
- 请问哪位大侠用.NET开发过监播系统吗?提供点资料呀,谢谢!救命呀!
- 请问ASP.NET2.0中web.config配置节中的<assemblies>是干什么用的?
- 初学者问:怎么在.cs和javascript之间共享变量?
- 关于如何保持控件显示状态的问题
- 如何在datagrid中加入web控件?
- 框架中点击一个按钮刷新多个frame的问题,谢谢大家
- DATAGRID,ITEMCREATE响应函数中,这个ITEM指的是一个单元格,还是一行,或者一列?
- 如何在Crystal Report中动态修改其图表控件的属性,如将柱型图改为折线图!!!急!在线!
- 视频上传和播放
- 缓存在Cache里的数据安全吗?
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
主表删除的时候是不是调用从表的删除函数?
试下