Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
ASP.NET
<asp:GridView ID="GridView1" runat="server" Width="549px" 
        AutoGenerateColumns="False" Height="21px">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:CheckBox ID="chkSelect" runat="server"  ToolTip='<%# Eval("IDProduct") %>'/>
        <asp:TextBox ID="txtID" runat="server" Text='<%# Eval("IDProduct") %>'/>
        </ItemTemplate>
        </asp:TemplateField>
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" />
        </Columns>
    </asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

C#
protected void Button1_Click(object sender, EventArgs e)
{
        string id;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            TextBox txt = GridView1.Rows[i].Cells[0].FindControl("txtID") as TextBox;
            CheckBox cb = GridView1.Rows[i].Cells[0].FindControl("chkSelect") as CheckBox;
            if (cb.Checked)
            {
                id = cb.ToolTip;
                DataRow dr = dt.Rows.Find(id);
                if (dr != null)
                {
                    dr.Delete();
                }
                LoadData();
            }

I get Object reference not set to an instance of an object. at dt.Rows.Find(id);
I changed:DataRow dr = dt.Rows.Find(id.Trim());
Error:Table doesn't have a primary key.I don't know.My table has primary key.It is IDProduct.
I tried:dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
But I still get error after I select checkbox
Posted
Updated 11-Jun-12 1:36am
v3

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line (Here: at dt.Rows.Find(id)). Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

It looks like 'dt' is null. I don't see how you got the object 'dt' filled here before using Find on it's rows.
 
Share this answer
 
I tried:
C#
Product p=new Product();
void LoadData()
{
        if (dt.Rows.Count == 0)
        {
            dt = p.GetProduct("*");
        }
        //Set Primary key for dt
        dt.Columns[0].Unique = true;
        dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
        GridView1.DataSource = dt;
        GridView1.DataBind();
}

C#
protected void Button1_Click(object sender, EventArgs e)
{
        
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chk = GridView1.Rows[i].Cells[0].FindControl("chkSelect") as CheckBox;
            if (chk.Checked)
            {
                DataRow dr = dt.Rows.Find(chk.ToolTip.ToString());
                if (dr != null)
                {
                    //dr.Delete();
                    //get IDProduct selected
                    Label1.Text = dr[0].ToString();
                    dr.Delete();
                    dr.AcceptChanges();
                    dt.AcceptChanges();
                }
            }
                LoadData();
            }
        }

I am load product in database after deleting rows.I am recognized button1 delete rows in DataTable.The database not change after deleting row.thank you
 
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