hi, im trying to insert data using Checklist and it is only inserting one even if i selected all che checkboxes, please help.hr is my Code

protected void Button1_Click(object sender, EventArgs e)
        {

            //C:\Documents and Settings\PraiseM\My Documents\Visual Studio 2008\Projects\MiniProjectAcc\MiniProjectAcc\Employee.accdb

            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\PraiseM\My Documents\Visual Studio 2008\Projects\MiniProjectAcc\MiniProjectAcc\Employee.accdb";

            string LastName = txtEmp_LastName.Text;
            string FirstName = txtEmployeeName.Text;
            string Email = txtEmail.Text;
            string Phone = txtPhone.Text;
            string Address = txtAddress.Text;
            string Street = txtStreet.Text;
            string Code = txtCode.Text;
            string Roles = CheckBoxList1.Text;
            string Password = txtPass.Text;

            OleDbCommand cmd = new OleDbCommand("INSERT INTO tblEmployee (LastName, FirstName, Email, Phone, Address, Street, Code, Roles, [Password]) Values(@LastName, @FirstName, @Email, @Phone, @Address, @Street, @Code, @Roles, @Password)");
            cmd.Connection = conn;

            conn.Open();


            if (conn.State == ConnectionState.Open)
            {
                string Emp_role = "";

                for (int a = 0; a < CheckBoxList1.Items.Count - 1; a++)
                {
                    if (CheckBoxList1.Items[a].Selected)
                    {
                        Emp_role += CheckBoxList1.Items[a].Text.ToString() + ",";
                    }

                }
                cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = LastName;
                cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = FirstName;
                cmd.Parameters.Add("@Email", OleDbType.VarChar).Value = Email;
                cmd.Parameters.Add("@Phone", OleDbType.VarChar).Value = Phone;
                cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = Address;
                cmd.Parameters.Add("@Street", OleDbType.VarChar).Value = Street;
                cmd.Parameters.Add("@Code", OleDbType.VarChar).Value = Code;
                cmd.Parameters.Add("@Roles", OleDbType.VarChar).Value = Roles;
                cmd.Parameters.Add("@Password", OleDbType.VarChar).Value = Password;

                try
                {
                    cmd.ExecuteNonQuery();
                    lblDisplay.Text = ("Data Added");
                    conn.Close();
                }
                catch (OleDbException ex)
                {

                    conn.Close();
                }
            }
            else
            {
                lblDisplay.Text = ("Connection Failed");
            }
            }

Recommended Answers

All 3 Replies

What exactly do you have in your checklistbox? If you want an Insert Statement to run for each item in your checklistbox then you have to assign your parameters and execute inside your for (int a = 0; a < CheckBoxList1.Items.Count - 1; a++)

your code line numbered 44 should actually be as below.

cmd.Parameters.Add("@Roles", OleDbType.VarChar).Value = Emp_role;

this should work fine.

 protected void Button1_Click(object sender, EventArgs e)
        {

            //C:\Documents and Settings\PraiseM\My Documents\Visual Studio 2008\Projects\MiniProjectAcc\MiniProjectAcc\Employee.accdb

            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\PraiseM\My Documents\Visual Studio 2008\Projects\MiniProjectAcc\MiniProjectAcc\Employee.accdb";

            string LastName = txtEmp_LastName.Text;
            string FirstName = txtEmployeeName.Text;
            string Email = txtEmail.Text;
            string Phone = txtPhone.Text;
            string Address = txtAddress.Text;
            string Street = txtStreet.Text;
            string Code = txtCode.Text;
            string Roles = CheckBoxList1.Text;
            string Password = txtPass.Text;

            OleDbCommand cmd = new OleDbCommand("INSERT INTO tblEmployee (LastName, FirstName, Email, Phone, Address, Street, Code, Roles, [Password]) Values(@LastName, @FirstName, @Email, @Phone, @Address, @Street, @Code, @Roles, @Password)");
            cmd.Connection = conn;

            conn.Open();


            if (conn.State == ConnectionState.Open)
            {
                string Emp_role = "";

                for (int a = 0; a < CheckBoxList1.Items.Count - 1; a++)
                {
                    if (CheckBoxList1.Items[a].Selected)
                    {
                        Emp_role += CheckBoxList1.Items[a].Text.ToString() + ",";
                    }

                }
                cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = LastName;
                cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = FirstName;
                cmd.Parameters.Add("@Email", OleDbType.VarChar).Value = Email;
                cmd.Parameters.Add("@Phone", OleDbType.VarChar).Value = Phone;
                cmd.Parameters.Add("@Address", OleDbType.VarChar).Value = Address;
                cmd.Parameters.Add("@Street", OleDbType.VarChar).Value = Street;
                cmd.Parameters.Add("@Code", OleDbType.VarChar).Value = Code;
                cmd.Parameters.Add("@Roles", OleDbType.VarChar).Value = Emp_role;
                cmd.Parameters.Add("@Password", OleDbType.VarChar).Value = Password;

                try
                {
                    cmd.ExecuteNonQuery();
                    lblDisplay.Text = ("Data Added");
                    conn.Close();
                }
                catch (OleDbException ex)
                {

                    conn.Close();
                }
            }
            else
            {
                lblDisplay.Text = ("Connection Failed");
            }
        }

that is my code now.
Thanks Fenrir and arunkumars i have changed the "Role" to "Emp_role" and it is working,

thank you very much Highly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.