ASP.Net GridView Row Deleting
this is my web page-------------------------
<asp:GridView runat="server" OnRowDeleting="gv1_Deleting" ID="gv1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="USERID" HeaderText="uesrid" />
<asp:ButtonField CommandName="Delete" Text="Delete" />
</Columns>
</asp:GridView>
this is my code behind page:
------------------------------------
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataTable dt= GetData();
Session["data"] = dt;
gv1.DataSource = dt;
gv1.DataBind();
}
}
public DataTable GetData()
{
DataTable dt = new DataTable();
DataColumn[] keys = new DataColumn[1];
DataColumn column;
// Create column 1.
column = new DataColumn();
// column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "USERID";
keys[0] = column;
dt.Columns.Add(column);
//dt.Columns.Add("USERNAME");
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "USERNAME";
dt.Columns.Add(column);
dt.PrimaryKey = keys;
//dt.Columns[0];
List<User> obj = new List<User>();
for (int i = 3; i < 15; i++)
{
User o = new User();
o.UserId = i++;
o.UserName = "karthik";
obj.Add(o);
}
DataRow dr;
foreach (User u in obj)
{
dr = dt.NewRow();
dr[0] = u.UserId.ToString();
dr[1] = u.UserName.ToString();
dt.Rows.Add(dr);
// keys[0] = column;
}
return dt;
}
protected void gv1_Deleting(object sender, GridViewDeleteEventArgs e)
{
string str = gv1.Rows[e.RowIndex].Cells[0].Text;
DataTable dt = (DataTable)Session["data"];
dt.Rows.Find(Convert.ToInt32(str)).Delete();
Session["data"]=dt;
gv1.DataSource = dt;
gv1.DataBind();
}
when I am deleting any row and refreshing, error showing:-
"Object reference not set to an instance of an object"
so plz some one suggest me the solution,,, thank u...