Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi frnds.
i've a table tblCustomerDetails.here i want to delete record on the basis of CustID.
All My data is showing in ListView.Now i wish to delete selected CustID from list as well as dataBase using Linq to SQL.
I made a class "Customer" where want to write delete function for this.
plz help me how can i do it.
how i'll pass selected listview control in function.
my code of class Customer:

C#
public void Deletecustomer(tblCustomerDetails1 CustID)
    {

        CustomerDataContext db = new CustomerDataContext();

        db.tblCustomerDetails1s.DeleteOnSubmit(CustID);
        db.SubmitChanges();
    }



Altough i found ListView control on button click,now how to pass in above function??

Button code:

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       foreach (ListViewItem itemRow in lvCustomer.Items )
       {
           //for (int i = 0; i < itemRow.SubItems.Count; i++)

           Label  CustID = ((Label)itemRow.FindControl("Cust_IDLabel"));
           int a = int.Parse (CustID.Text);

                  }
   }
Posted

You can pass the custID as int to the deletecustomer function and delete the record having custID.
After the function call, you can reload/refresh the listview, in the button1_click().

C#
public void Deletecustomer(int CustID)
    {

        CustomerDataContext db = new CustomerDataContext();
        Table<customerdetails> tblCustomerDetails = db.Gettable<customerdetails>();
        CustomerDetails selectedCustomer = (CustomerDetails)tblCustomerDetails.Select(c=>c.custID == CustID);
        db.tblCustomerDetails.DeleteOnSubmit(selectedCustomer);
        db.SubmitChanges();
    }


Does this help you?

Regards
Swami
 
Share this answer
 
v2
Rewite Button1_Click following way....

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       CustomerDataContext db = new CustomerDataContext();
       
       foreach (ListViewItem itemRow in lvCustomer.Items )
       {

           Label  CustID = ((Label)itemRow.FindControl("Cust_IDLabel"));
           int a = int.Parse (CustID.Text);
	   db.tblCustomerDetails1s.DeleteAllOnSubmit(db.tblCustomerDetails1s.Where(c => c.CustID == a));

        }
	db.SubmitChanges();

   }
 
Share this answer
 
v3

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