C# Multi CheckboxList update inserts checked records but doesn't delete unchecked records

Posted by DLL on Stack Overflow See other posts from Stack Overflow or by DLL
Published on 2013-07-03T17:02:48Z Indexed on 2013/07/03 17:05 UTC
Read the original article Hit count: 340

Filed under:
|
|

I have a multi checkboxlist on a formview. Both use queries in a tableadapter. I'm using VS 2012.

When the user updates the form, I use the following code to update the checkbox data. If a user checks a new box, a new record is inserted correctly, however if the user unchecks a box the existing record is not deleted.

The delete query works fine if I run it from the query builder in the table adapter, it's reaching the expected line in the code correctly, all values are correct and I receive no errors. I use a similar query to delete records when the form level data is deleted which works fine.

The very last line is the one that doesn't work.

Query: DELETE FROM [SLA_Categories] WHERE (([SLA_ID] = @SLA_ID) AND ([Choice_ID] = @Choice_ID))

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
if (FormView1.DataKey.Value != null)
{
Categs = (CheckBoxList)FormView1.FindControl("CheckBoxList1");
CurrentSLA_ID = (int)FormView1.DataKey.Value;
}
if (CurrentSLA_ID > 0)
{
foreach (ListItem li in Categs.Items)
{
// See if there's a record for the current sla in this category
int CurrentChoice_ID = Convert.ToInt32(li.Value);

SLADataSetTableAdapters.SLA_CategoriesTableAdapter myAdapter;
myAdapter = new SLADataSetTableAdapters.SLA_CategoriesTableAdapter();

int myCount = (int)myAdapter.FindCategoryBySLA_IDAndChoice_ID(CurrentSLA_ID,          CurrentChoice_ID);

// If this category is checked and there is not an existing rec, insert one
if (li.Selected == true
&& myCount < 1)
{
// Insert a rec for this sla
myAdapter.InsertCategory(CurrentChoice_ID, CurrentSLA_ID);
}

// If this category is unchecked and there is and existing rec, delete it
if (li.Selected == false
&& myCount > 0)
{
// Delete this rec
myAdapter.DeleteCategoryBySLA_IDAndChoice_ID(CurrentChoice_ID, CurrentSLA_ID);
}

}
}
} 

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET