samedi 28 octobre 2017

values from unchecked checkboxes are also inserted into database

i have problem while making my application in windows form csharp regarding my checkbox in datagridview when i 1st time insert the value with options selected in checkbox it gets inserted fine in the database but on the 2nd time when i insert the value with new items checked in checked boxes and old ones unchecked it adds the new one along with the old ones which are unchecked. My code for your kind information and Help

protected void btnAssign_Click(object sender, EventArgs e)
    {
        int trainer;
        List<tbl_Trainer_Workshop_Mapping> ls = new List<tbl_Trainer_Workshop_Mapping>();

        int workshopid = Convert.ToInt32(dataGridViewtbl_Workshop.CurrentRow.Cells["UsWorkShopId"].Value.ToString());

        foreach (DataGridViewRow row in dataGridViewSelectTrainer.Rows)
        {


            if (row.Cells[0].Value == null)
            {
                row.Cells[0].Value = false;
            }
            else
            {
                trainer = Convert.ToInt32(row.Cells[1].Value.ToString());
                tbl_Trainer_Workshop_Mapping twm = new tbl_Trainer_Workshop_Mapping()
                { WorkShopId = workshopid, TrainerID = trainer };
                ls.Add(twm);
            }
        }


        if (ls.Count() > 0)
        {
            WorkshopBusiness Wb = new WorkshopBusiness();
            Wb.AssignTrainersToTheWorkShop(ls);
        }

        foreach (DataGridViewRow row1 in dataGridViewSelectTrainer.Rows)
        {
            row1.Cells[0].Value = false;
        }

    }

1st time when two options(value 1& 2) checked and shown in database against workshop id 4

Values entered in the database correctly for the 1st time

but on 2nd time it adds the value checked in the checkbox along with the options with are unchecked

Only one option (value 3) checked and the other two (value 1 & 2) unchecked

In DB one required is there along with the two which are not required

please guide regards




Aucun commentaire:

Enregistrer un commentaire