Retrieve CheckBoxList value to CheckBoxList Control

Hello Experts,

I have a form that uses the CheckBoxList Control. The data that gets displayed for the CheckBoxList control is populated from a table in my database.

Then a user will click on each value they see fit on the CheckBoxList Control and those values they select get stored in another table in my database.

The problem that I have is that I don't know how to retrieve the values that the user selected which are stored in my database back to my CheckBoxList Control for me to view.

Below is what I'm using now to retrieve all my data from multiple tables back to the form based on the employee's unique ID in the database. Everything works fine except retrieving the values back to the CheckBoxList control to visually see what they selected. I checked my stored procedure out and it works fine.

protected void Page_Load(object sender, EventArgs e)
    {
        RetrieveCandidateByID();
    }



protected void RetrieveCandidateByID()
    {
        int cid = Convert.ToInt32(Request.QueryString["c_id"].ToString());

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TMF"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "RetrieveCandidateByID";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.Add("@c_id", SqlDbType.Int).Value = cid;

            DataTable dtExtenuating = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter();

            try
            {
                conn.Open();

                adp.SelectCommand = cmd;
                adp.Fill(dtExtenuating);

                if ((dtExtenuating != null))
                {
                    DataRow data = dtExtenuating.Rows[0];

                    txtFirstName.Text = data["c_fname"].ToString();
                    txtLastName.Text = data["c_lname"].ToString();
                    txtAddress.Text = data["c_address"].ToString();
                    ddlState.SelectedValue = data["state_id"].ToString();
                    ddlCity.SelectedValue = data["city_id"].ToString();
                    ddlZipCode.SelectedValue = data["zip_id"].ToString();
                    txtPhone.Text = data["c_phone"].ToString();
                    txtEmail.Text = data["c_email"].ToString();
                    ddlSchool.SelectedValue = data["sch_id"].ToString();
                    ddlGrade.SelectedValue = data["grade_id"].ToString();
                    ddlAge.SelectedValue = data["age_id"].ToString();
                    txtPurposeApp.Text = data["c_purpose_desc"].ToString();
                    txtAmountRequested.Text = data["c_amount_requested"].ToString();
                    ddlHearAboutUs.SelectedValue = data["hau_id"].ToString();

                    txtCurrentlyReceiveOtherInfo.Text = data["c_crname"].ToString();
                    txtExtenuatingDesc.Text = data["c_extenuating_desc"].ToString();
                    txtParentGuardianFirstLastName.Text = data["c_parentguardian_name"].ToString();

                    foreach (ListItem item in cblCurrentlyReceiveValues.Items)
                    {
                        cblCurrentlyReceiveValues.SelectedItem.Value = data["cr_id"].ToString();
                    }
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
    }

Open in new window


You will see at the bottom of my CodeBehind that I try to retrieve the values to the CheckBoxList control which is labeled as "cblCurrentlyReceiveValues".
LVL 4
asp_net2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unknown_routineCommented:
You need to have CheckboxList EnableviewState Property set to true. This should preserve the values.
0
asp_net2Author Commented:
even during a Page_Load call based on a users ID from another page?
0
asp_net2Author Commented:
@unknown_routine,

Also, do you know what I need to include in the CodeBehind that I provided with the EnableViewState property set to true which I just now added and it did nothing different. The problem obviously lies in my CodeBehind.
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

unknown_routineCommented:
There  is probelm in your code behind:

foreach (ListItem item in cblCurrentlyReceiveValues.Items)
                    {
                        cblCurrentlyReceiveValues.SelectedItem.Value = data["cr_id"].ToString();
                    }

Open in new window



In above you are not using the iterator object which is item(cblCurrentlyReceiveValues.SelectedItem.Value won't change in iterations)

You need to change it to:


foreach (ListItem item in cblCurrentlyReceiveValues.Items)
{
	item.Value=data["cr_id"].ToString();
}

Open in new window

0
asp_net2Author Commented:
@unknown_routine,

That did not work. I did not receive any errors, nothing is getting selected on the CheckBoxList Control from the values in the database.
0
unknown_routineCommented:
what do you expect to see. The foreach above only sets the values.
If you put a breakpoint what do you see as value of

data["cr_id"] ? is it a bool?

If so change the code to :

foreach (ListItem item in cblCurrentlyReceiveValues.Items)
{
      item.selected=(bool)data["cr_id"]
}



This should work. if not, let me know what is value of
data["cr_id"].ToString();
in the above code.
0
asp_net2Author Commented:
@unknown_routine,

I need to retrieve values from a table in my database to a CheckBoxList Control based on the value "cr_id" which is an "int" value.

So for example, I have and employee in which his primary ID in the table is called "cr_id" and the "cr_id" value for example is 10034. So I need to retrieve all the entries that he initially submitted using the CheckBoxList control to be shown on the CheckBoxList Control during Page_Load based on the value of "cr_id" getting passed in.

So I should be able to retrieve all of the CheckBoxList values that cr_id 10034 has in the database and I would like to have the CheckBoxList Control display those by means of displaying the check on the CheckBoxList control.
0
asp_net2Author Commented:
@unknown_routine,

I found the following code below that will display one of my values in the database to the CheckBoxList Control but it does not display all of them. It only displays the first value in the CheckBoxList Control and not the others.

CheckBoxList chkbx = (CheckBoxList)form1.FindControl("cblCurrentlyReceiveValues");
                    ListItem currentCheckBox = chkbx.Items.FindByValue(data["cr_id"].ToString());
                    if (currentCheckBox != null)
                    {
                        currentCheckBox.Selected = true;
                    }

Open in new window

0
asp_net2Author Commented:
I was able to figure out the solution on my own. The following code below binds the values for the CheckBoxList Control from the DB.

foreach (DataRow row in dtExtenuating.Rows)
                    {
                        cblFinancialAssistanceValues.Items.FindByValue(row["fa_id"].ToString()).Selected = true;
                    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asp_net2Author Commented:
I figured out the issue myself. I'm very disappointed in the lack of support that I received from EE.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.