mercredi 4 novembre 2020

checkboxlist from SQL not selecting first item

I have a form that has checkboxlists. I want this form to be updatable at a later point. So I store the data in SQL and then retrieve it later to fill the form as it was when it was saved. But for some reason, the checkbox list, selects all the options previously selected except for the first one.

This is my code

CheckBoxList8.Items.Clear();
CheckBoxList8.AppendDataBoundItems = true;
DateTime pickupDate = DateTime.Parse(SessionDateCalendar.Text);

SqlConnection con7 = new SqlConnection(strcon);
if (con7.State == ConnectionState.Closed)
{
    con7.Open();
}

// this sql command finds all the possible options for the checkbox list, as they are different depending on the customer. 

SqlCommand cmd7 = new SqlCommand("SELECT * from customer_outcomes WHERE NYEIS_ID='" + DropDownList1.SelectedValue + "' and @SessionDate >= start_date And  @SessionDate <= end_date;", con7);

cmd7.Parameters.AddWithValue("@SessionDate", pickupDate);
cmd7.CommandType = CommandType.Text;

SqlDataAdapter da7 = new SqlDataAdapter(cmd7);
DataTable dt7 = new DataTable();
da7.Fill(dt7);
if (dt7.Rows.Count >= 1)
{
    // this splits all the possible options, as they are previously stored with ";" as a separator and then fills the check box list. (this is done successfully. When debugging, all the data seems to be stored correctly. I will attach the debugging info below. 
    string[] outcomesObject = dt7.Rows[0]["outcomes"].ToString().Trim().Split(';');
    for (int i = 0; i < outcomesObject.Length; i++)
    {
        CheckBoxList8.Items.Add(outcomesObject[i]);
    }

    // this takes the data from a previous SQL cmd and splits the data from the saved sql table, it is split and then stored as a string called OutcomesSplitter. 
    string OutcomesSelector = dt.Rows[0]["outcomes"].ToString().Trim();
string[] OutcomesSplitter = OutcomesSelector.Split(new[] { ";" }, StringSplitOptions.None);

    //this selects all the options that were previously selected. 
    foreach (ListItem li in CheckBoxList8.Items)
        li.Selected = OutcomesSplitter.Contains(li.Text);

    CheckBoxList8.DataBind();

This is the debug info that I thought was relevant.

dt.Rows[0]["outcomes"].ToString().Trim();

[33] = "\r\n- remove simple clothing such as socks/shoes by self\t;\r\n- tolerate diaper changing\t;\r\n3. David will point/use approximation of words to express his wants/needs;\r\n- produce jargon w/ approximation to true words\t;\r\n- use approximation of words...

OutcomesSelector =

"- remove simple clothing such as socks/shoes by self\t;\r\n- tolerate diaper changing\t;\r\n3. David will point/use approximation of words to express his wants/needs;\r\n- produce jargon w/ approximation to true words\t;\r\n- use approximation of words to ...

OutcomesSplitter.Contains(li.Text);

[0] = "- remove simple clothing such as socks/shoes by self\t" [1] = "\r\n- tolerate diaper changing\t" [2] = "\r\n3. David will point/use approximation of words to express his wants/needs"

The only thing I can tell is that somewhere between when the code gets moved from >dt to >OutcomesSelector the \r\n gets removed. When I put it in manually during debug, that first item is then selected. Please help. I do not know how to get that first item selected as well as the others that are listed in >dt.rows

Thanks for any help.




Aucun commentaire:

Enregistrer un commentaire