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

解决方案 »

  1.   

    foreach (DataGridItem item in dbStaffList.Items)
    {
    if(((CheckBox)item.FindControl(controlID)).Checked==true )
             {
         ...
             }
    }
      

  2.   

    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();
      

  3.   

    CheckBox ckb = (CheckBox)datagrid.Items[e.Item.ItemIndex].FindControl("CheckBox");
    我的是2003的,使用的datagrid,不知道2005里是否一样,你可以试试
      

  4.   

    老大,如果你 的
         if (ckb.Checked)
                {
                    strDelId = GridView1.DataKeys[i.RowIndex].Value.ToString();
                }
    不执行,那么strDelId为空
      

  5.   

    另外string delsql = "delete from userreg where id=" + strDelId;
    应该改写成
    string delsql = "delete from userreg where id='" + strDelId+"'";
    如果你的id是字符型的
      

  6.   

    foreach (GridViewRow i in GridView1.Rows)
            {
                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();            
                }
            }
      

  7.   

    应该把           string delsql = "delete from userreg where id=" + strDelId;
                OleDbCommand cmd = new OleDbCommand(delsql,myConn);
                cmd.ExecuteNonQuery();            
    放入if语句中
      

  8.   

    foreach (GridViewRow i in GridView1.Rows)
            {
                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();            
                    }
                }
            }
      

  9.   

    行 67:             CheckBox ckb = (CheckBox)i.FindControl("CheckBox");把("CheckBox") 改成("CheckBox1")
      

  10.   

    估计是找不到CheckBox的原因。
    试下直接引用
     CheckBox ckb = (CheckBox)i.Controls[0];