1
Answer

Checkboxlist - Add values to Database

Kavi suja

Kavi suja

11y
1.1k
1
Hi,
    I have check box list in a page. I generate values for this check box  list from database in pageload as follows:


 CheckBoxList2.DataSource = objRegLogic.GetSPList("SP_Allergy");
                CheckBoxList2.DataTextField = "name";
                CheckBoxList2.DataValueField = "pkey";
                CheckBoxList2.DataBind();

  I want to add these checkboxlist value in to database in a button click.I use the following code.But when debugging " if (CheckBoxList2.Items[i].Selected == true)"  this condition can't work properly. Even i checked one item it doesn't enter in to the loop

 Can u please check this code,whether i did any wrong??

protected void labtestbtn_Click(object sender, EventArgs e)
        {
            ConsultingEntity objConsEntity = new ConsultingEntity();
            PatientConsultingBLLLogic objPatConsBLL = new PatientConsultingBLLLogic();

            string Pid = Request.QueryString["PID"] as string;
            if (Pid == null || Pid == string.Empty)
            {
                Pid = ddlPatient.SelectedValue;
            }
            string DocID = Convert.ToString(Session["pkey"]);
            if (DocID == null || DocID == string.Empty)
            {
                DocID = ddldoc.SelectedValue;
            }
            try
            {
                if (Pid == "")
                {

                }
                else
                {
                    for (int i = 0; i < CheckBoxList2.Items.Count; i++)
                    {
                        if (CheckBoxList2.Items[i].Selected == true)
                        {
                            string labtest = item.Value;
                            objConsEntity.LabTest = labtest;
                            objConsEntity.Doctorid = Convert.ToInt16(DocID);
                            objConsEntity.PatientId = Convert.ToInt16(Pid);
                            objPatConsBLL.InsertPatientLapTest(objConsEntity);
                         
                        }
                    }
                } 
               
            }

            catch (Exception ex)
            {

            }
         
        }

I didn't understand where is the wrong. Can anyone help me to do this?






Answers (1)