• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

CheckBoxList saving in database

Hello,

I am looking for the most efficient (in asp.net c#) way to save checkbox list values to a database.  I have a form that needs to do this when someone saves.  However, I also need to be able to show the selected checkboxes if the user pulls the form up again to edit.  My thought is to create some sort of delimited string to save in the database.

I have the following HTML:

Payment Plan:
<asp:checkboxlist id="cblPaymentPlan" runat="server" RepeatDirection="Horizontal">
      <asp:ListItem Value="Fully Earned">Fully Earned</asp:ListItem>
      <asp:ListItem Value="Annual">Annual</asp:ListItem>
      <asp:ListItem Value="Semi Annual">Semi Annual</asp:ListItem>
      <asp:ListItem Value="Quarterly">Quarterly</asp:ListItem>
</asp:checkboxlist>

I have it so that it inserts properly:

string cblValue = "";
for(int i=0; i<cblPaymentPlan.Items.Count; i++){
      if(cblPaymentPlan.Items[i].Selected==true)
            cblValue += cblPaymentPlan.Items[i].Value + "|";
}

So what I'm looking for (assuming the above way is the most efficient) is:

- asp.net c# code to select the appropriate check boxes based on a '|' delimited string

I hope I am clear.  If not please let me know.

Thanks in advance.
0
russomr
Asked:
russomr
  • 3
  • 2
1 Solution
 
REA_ANDREWCommented:
ok first I have made a check box list to use so you can see the references

        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem Value="This" Text="This" Selected></asp:ListItem>
        <asp:ListItem Value="This1" Text="This1" Selected></asp:ListItem>
        <asp:ListItem Value="This2" Text="This2" Selected></asp:ListItem>
        <asp:ListItem Value="This3" Text="This3" Selected></asp:ListItem>
        <asp:ListItem Value="This4" Text="This4"></asp:ListItem>
        </asp:CheckBoxList>

in the cs code.  First you need a function for this so

    protected string[] CheckCount(CheckBoxList Target)
    {
        string[] MyTickedValues = { "" };
        for (int i = 0; i < Target.Items.Count; i++)
        {
            if (Target.Items[i].Selected == true)
            {
                if (MyTickedValues.Length < 1)
                {
                    MyTickedValues[0] = Target.Items[i].Text;
                }
                else
                {
                    MyTickedValues[MyTickedValues.Length - 1] = Target.Items[i].Text;
                }
            }
        }
        return MyTickedValues;
    }

this function then returns an Array of the selected Items Text. e.g. Text1,Text2,Text3

here you then have your Array which you can then store in your Database. To use this function all you need to do in your code, is write this

string [] CheckItems = CheckCount(The_Id_Of_Your_Check_List_Here);

then when you want to retrieve the items use this function

    protected void AssignChecks(string[] Values, CheckBoxList Target)
    {
        for (int i = 0; i < Values.Length; i++)
        {
            Target.Items.FindByText(Values[i].ToString()).Selected = true;
        }
    }

Again to use this function you supply the array and also the checkListId you refer to.

this will then put ticks on the boxes that are used.

I have learnt something myself on this one. I have never had to work with a check box list before.
0
 
TornadoVCommented:
The following will set your checkbox based on a "|"-delimited string that you've retrieved from a database:

string stringFromDB = string.Empty;
        string[] checklistSource =  stringFromDB.split('|');

        foreach (string s in checklistSource)
        {
            if (CheckBoxList1.Items.FindByValue(s) != null)
            {
                CheckBoxList1.Items.FindByValue(s).Selected = true;
            }
        }
0
 
TornadoVCommented:
Simply replace string.Empty with actual string from the database

string stringFromDB = string.Empty;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
REA_ANDREWCommented:
My example of this second function achieves this

    protected void AssignChecks(string[] Values, CheckBoxList Target)
    {
        for (int i = 0; i < Values.Length; i++)
        {
            Target.Items.FindByText(Values[i].ToString()).Selected = true;
        }
    }

Also it can be used for any checkbox list with lesser code. It is used as so.

AssignChecks(YourArrayHere,YourCHeckBox_ID_Here);

thats it
0
 
russomrAuthor Commented:
Thank you both for your submissions however TornadoV gave the exact answer to my question with the most efficient code.

I did have to captialize the 'S' in Split, otherwise I got an error message.  I'm just noting this in case this answer is found by someone looking to do the same.
0
 
TornadoVCommented:
Thanks, and sorry about 'split()' with lower case 's', I was using a public PC without any dev tools installed and couldn't test it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now