Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have designed an address book where gridview bind with the address details with check option.there is also an dropdownlist out of the gridview which populate with the group. If you click on that dropdownlist then the gridview bind the data according to the selected group.
If I click on the selected all then the selected data of the particular grops data are deleted form the table "Addressbook" using linq

I've used 2 tables
1.groups
2.addressbook
groupid is fk in addressbook which is auto generated key.

I have already written code for delete with select all

the code is
C#
for (int i = 0; i < GridView1.Rows.Count; i++)
           {
               System.Web.UI.WebControls.CheckBox cbb = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("chkbox");
               if (cbb.Checked == true)
               {
                   int id = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);
                   alist.Add(id);
               }
           }
           using (RealEstateDataClassesDataContext db = new RealEstateDataClassesDataContext())
           {
               for (int j = 0; j < alist.Count; j++)
               {
                   int id = Convert.ToInt32(alist[j]);
                   var info = from p in db.AddressBooks
                              where p.Addbook_ID == id
                              select p;
                   foreach (var deleteinfo in info)
                   {
                       db.AddressBooks.DeleteOnSubmit(deleteinfo);
                   }
                   try
                   {
                       db.SubmitChanges();
                   }
                   catch (Exception ex)
                   {
                       Response.Write("Error:" + ex.Message);
                   }


when the dropdownlist selected value as all then it is works.
but when I select a particular group from dropdownlist and the select all and click on the button the it shows me error.
MSIL
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 148:
Line 149:                System.Web.UI.WebControls.CheckBox cbb = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("chkbox");
Line 150:                if (cbb.Checked == true)
Line 151:                {
Line 152:                    int id = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);



for more details please visit the site http://php-addressbook.sourceforge.net/demo/[^]

Thanks in advance!
Posted
Updated 3-Jan-11 19:07pm
v3

1 solution

Looks like the issue is with this line:
System.Web.UI.WebControls.CheckBox cbb = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("chkbox");

Are you sure that there is a checkbox in the rows? (You have not defined the column index the control should be.) I doubt that. After using FindControl, you should check for null value to be sure that you found it and then only you should use it.

Use Visual Studio DEBUGGER and see the execution workflow. I guess, you just want to check for a checkbox in the first/second column. Put the specific index after rows and get the control.
 
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