Click here to Skip to main content
14,879,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the selected values from the checkbox list called CheckBoxList1 to get saved into the database with the rest of the data mentioned but only the first checked item gets saved into the database. Please help.

protected void Button3_Click1(object sender, EventArgs e)
       using (Stream fs = FileUpload1.PostedFile.InputStream)
           using (BinaryReader br = new BinaryReader(fs))

               byte[] bytes = br.ReadBytes((Int32)fs.Length);
               String strConnString = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
               String strQuery = "Insert into Form (Name, Test_Number, Course_Name, Program, Exam_Month, Exam_Year, Course_Code, Semester, Paper, CLOs) VALUES(@Name, @Test_Number, @Course_Name, @Program, @Exam_Month, @Exam_Year, @Course_Code, @Semester, @Paper, @CLOs)";
               SqlConnection con = new SqlConnection(strConnString);
               SqlCommand cmd = new SqlCommand();
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = strQuery;
               cmd.Connection = con;
               cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
               cmd.Parameters.AddWithValue("@Test_Number", DropDownList3.SelectedValue);
               cmd.Parameters.AddWithValue("@Course_Name", DropDownList5.SelectedValue);
               cmd.Parameters.AddWithValue("@Program", DropDownList4.SelectedValue);
               cmd.Parameters.AddWithValue("@Exam_Month", TextBox5.Text);
               cmd.Parameters.AddWithValue("@Exam_Year", TextBox6.Text);
               cmd.Parameters.AddWithValue("@Course_Code", TextBox8.Text);
               cmd.Parameters.AddWithValue("@Semester", DropDownList2.SelectedValue);
               cmd.Parameters.AddWithValue("@Paper", bytes);

               for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
                   if (CheckBoxList1.Items[i].Selected)
                       string var = string.Empty;
                       var += CheckBoxList1.Items[i].Text.ToString() + ",";
                       cmd.Parameters.AddWithValue("@CLOs", var);




           } Label15.Visible = true;


What I have tried:

Tried the above code, only the first selected value is getting saved to the database.
Updated 20-Jun-20 19:07pm

Although Garth's solution will work, it's a poor DB design to store comma delimited values in a single column of a database.

It duplicates information, wastes storage space, promotes errors, and makes the data very hard to work with later.

For example, if the course "Basic" needs to be renamed to "Visual Basic" later on, you would need to change it in every row, and be extremely careful not to affect "Basic Maths" when you do it.
Worse, when you need to change a course for an individual - he swaps from "Visual Basic" to "C#" for example - it's horribly complicated to do in SQL because it's string handlking is marginal at best.
Instead, you should be setting up two tables:

UserID    INT, Form.ID
CourseID  INT, foreign key to Courses.ID

Then SQL can do what it does best - process relationships between data - and you use JOIN when you want to retrieve the data.
It makes all processing easier, and only complicates the original INSERT operation a little.
Think about what the 'string var = string.Empty;' and cmd.Parameters.AddWithValue... is doing INSIDE your loop ... maybe you meant

[__b__]string var = string.Empty;
for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
  if (CheckBoxList1.Items[i].Selected)
    var += CheckBoxList1.Items[i].Text.ToString() + ",";
cmd.Parameters.AddWithValue("@CLOs", var);
candijen 21-Jun-20 5:02am
Hi, still only the first selection appears in the database along with a " , " and nothing else.

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