Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 12 checkbox the checkbox text is months name e.g jan,feb,march.....
i want if some of these checkbox is checked the value of text store in single column and then retrieve this if the user id search again.

my database code
C#
using (DbSQLServer db = new DbSQLServer(AppSetting.ConnectionString()))
{
    db.SaveOrUpdateRecord("Fee_Details", GetData());
}

and then set values in GetData funtion
C#
private MonthlyFeeStractures GetData()
       {
           MonthlyFeeStractures feeRecord = new MonthlyFeeStractures();
           feeRecord.StudentName = StudentNameLbl.Text;
           feeRecord.FatherName = FatherName.Text;
           feeRecord.RegistrationNumber = RegistrationTextBox.Text;
           feeRecord.ClassOfAdmission = FindStudentComboBox.Text;
           feeRecord.DateOfPay = FeePaymentDateDateTimePicker.Value;
           feeRecord.PaymentType = PaymentTypeComboBox.Text;
           feeRecord.FeeMonth = checkboxdata();
           feeRecord.Fee = FeeLabel.Text;
           feeRecord.Dues = DuesLabel.Text;
           feeRecord.Quantity = Convert.ToInt32(QuantityLabel.Text);
           feeRecord.TotalPayment = TotalAmountLabel.Text;
           feeRecord.Discount = DiscountTextBox.Text;
           feeRecord.PayBy = PaidByTextBox.Text;
           feeRecord.PromotionFee = PromoFeeTextBox.Text;
           feeRecord.PromotionFee = PromoClassCmoboBox.Text;
           feeRecord.ReceiptNumber = ReceiptNumberLabel.Text;

           return feeRecord;
       }


Other Data Store Correctly But The checkbox name is not store and when i Debug on this
C#
feeRecord.FeeMonth = checkboxdata();

the function return the vale of s but is is null

What I have tried:

C#
private string checkboxdata()
        {
            string s =Convert.ToString(0);
            foreach (Control c in MonthlyFee.Controls)
            {                            
                     CheckBox cb = c as CheckBox;
                    if (cb.Checked)
                    {
                        s = cb.Text ;
                    }                
            }
            return s;
            
        }
Posted
Updated 25-Feb-19 3:07am
v2
Comments
BillWoodruff 24-Feb-19 21:00pm
   
what database ?

Quote:
How store and retrieve multiple checkbox in single column in database in C#

You have basically 3 solutions:
- use 1 field per value
- use 1 row per value
- combine all checkbox in 1 value
your choice.

Your code:
- do not check if checkbox are months or not.
- only get the name of last checked checkbox.
- is missing the part that save data in database.
   
Without more detail, I can't be more specific, but this should give you some ideas:
StringBuilder sb = new StringBuilder();

foreach (CheckBox ckbx in MonthlyFee.Controls
    .OfType<CheckBox>()
    .Where(cbx => cbx.Checked))
{
    sb.AppendLine(ckbx.Name);
}

string checkedmonths = sb.ToString();
   
Write some database code that does something with "s" ... then come back with more questions.
   
Comments
BillWoodruff 24-Feb-19 20:59pm
   
My vote of #1: this is not a solution, it's an insulting remark.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900