Click here to Skip to main content
15,936,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to delete multiple rows in gridview with checkbox selection and with confirmation using

this code give an error and i cannot find the Exception:-

the exception is:-(Object reference not set to an instance of an object.)

The C# sharp code is :-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default3 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
        foreach (GridViewRow gv in GridView1.Rows)
            CheckBox arun = (CheckBox)gv.FindControl("arun");
            if (arun.Checked==true)
                int res = Convert.ToInt32(GridView1.DataKeys[gv.RowIndex].Value);
                SqlDataSource1.DeleteParameters["userid"].DefaultValue = res.ToString();


Updated 15-Jul-11 18:57pm
m@dhu 16-Jul-11 1:14am    
Use the debugger and check where the error raises and check whether the value is null or not initialised.

i think u forgot to set id as datakeyname

otherwise try this code

Instead of
int res = Convert.ToInt32(GridView1.DataKeys[gv.RowIndex].Value);

int res = Convert.ToInt32(GridView1.gv.Cells[1].Text);

//where cells[1] means column position of that id field
Share this answer
when it will throw this error i use :- int res = Convert.ToInt32(GridView1.gv.Cells[1].Text);

Error:- System.Web.UI.WebControls.GridView does not contain a definition for gv

please help me for this......
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900