OleDbConnection myConn = new OleDbConnection(Class1.ConnString);
myConn.Open();
string strDelId="";
foreach (GridViewRow i in GridView1.Rows)
{
CheckBox ckb = (CheckBox)i.FindControl("CheckBox");
if (ckb.Checked)
{
strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
}
string delsql = "delete from userreg where id=" + strDelId;
OleDbCommand cmd = new OleDbCommand(delsql,myConn);
cmd.ExecuteNonQuery();
}
myConn.Close();出错了显示下面错误:
--------------------------
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 66: {
行 67: CheckBox ckb = (CheckBox)i.FindControl("CheckBox");
行 68: if (ckb.Checked)
行 69: {
行 70: strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
源文件: f:\homework\netbest2\WebSite\checkbox.aspx.cs 行: 68
myConn.Open();
string strDelId="";
foreach (GridViewRow i in GridView1.Rows)
{
CheckBox ckb = (CheckBox)i.FindControl("CheckBox");
if (ckb.Checked)
{
strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
}
string delsql = "delete from userreg where id=" + strDelId;
OleDbCommand cmd = new OleDbCommand(delsql,myConn);
cmd.ExecuteNonQuery();
}
myConn.Close();出错了显示下面错误:
--------------------------
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 66: {
行 67: CheckBox ckb = (CheckBox)i.FindControl("CheckBox");
行 68: if (ckb.Checked)
行 69: {
行 70: strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
源文件: f:\homework\netbest2\WebSite\checkbox.aspx.cs 行: 68
解决方案 »
- ASCX做的head,footer控件在每个页面都要引用吗?有没有什么简单的方法?
- ASP.NET 2.0从入门到精通中文版下载
- 首页提交都 首页字体变大、控件位置变化的问题为什么想不明白??????????????????????????????????
- 最匹配的重载方法具有一些无效参数
- ````5555 菜鸟问题!!! 怎么把3个表放到一个DataGrid中
- 如果使用Crystal report 10,到底能得到什麽样的效果?(没用过想试试)
- ????????关于焦点的问题??????????????
- 在asp.net中,如何更改创建新页面的默认字体?
- 有两个问题请教大家,哎,简直郁闷死了!!!
- 请推荐几本.net的书,要beta2的!
- 如何在登录页面通过javascript来判断登录名是否为空?
- 火急~~~ 一个超简单的字符串转换问题!
{
if(((CheckBox)item.FindControl(controlID)).Checked==true )
{
...
}
}
myConn.Open();
string strDelId="";
foreach (GridViewRow i in GridView1.Rows)
{
CheckBox ckb = (CheckBox)i.FindControl("CheckBox");
if (ckb.Checked)
{
strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
string delsql = "delete from userreg where id=" + strDelId;
OleDbCommand cmd = new OleDbCommand(delsql,myConn);
cmd.ExecuteNonQuery();
}
}
myConn.Close();
我的是2003的,使用的datagrid,不知道2005里是否一样,你可以试试
if (ckb.Checked)
{
strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
}
不执行,那么strDelId为空
应该改写成
string delsql = "delete from userreg where id='" + strDelId+"'";
如果你的id是字符型的
{
CheckBox ckb = (CheckBox)i.FindControl("CheckBox");
if(ckb != null)
{
if (ckb.Checked)
{
strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
}
string delsql = "delete from userreg where id=" + strDelId;
OleDbCommand cmd = new OleDbCommand(delsql,myConn);
cmd.ExecuteNonQuery();
}
}
OleDbCommand cmd = new OleDbCommand(delsql,myConn);
cmd.ExecuteNonQuery();
放入if语句中
{
CheckBox ckb = (CheckBox)i.FindControl("CheckBox");
if(ckb != null)
{
if (ckb.Checked)
{
strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
string delsql = "delete from userreg where id=" + strDelId;
OleDbCommand cmd = new OleDbCommand(delsql,myConn);
cmd.ExecuteNonQuery();
}
}
}
试下直接引用
CheckBox ckb = (CheckBox)i.Controls[0];