RSS Feed

Inserting Multiple Checked Value from CheckBoxList to DataBase

January 17, 2012 by Sourabh Sharma

Our recent post about how to bind a CheckBoxList with database. I hope all you guys have got very useful information. Today i will tell you that how to insert the values that are selected from the CheckboxList. Sometimes we need to select multiple values from the checkbox and we want to save all the selected values in a single column of database table. Also we will learn that how to show again these values in checkboxlist control. I have used C# as front-end and Sqlserver 2008 as back-end.


Free Download:
  Insert Multiple CheckBox Value CheckBoxList : Demo Project

 

Source Code:

<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnGetCountryList" runat="server" OnClick="btnGetCountryList_Click"
        Text="Get List" />
    <asp:Label ID="lblerror" runat="server" Font-Bold="True" ForeColor="#CC3300" Text="lblerror"></asp:Label>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" BorderStyle="Groove" Height="400px"
        RepeatColumns="15" RepeatDirection="Horizontal" Width="500px">
    </asp:CheckBoxList>
    <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
    </form>
</body>

.ASPX.CS Code:

protected void btnInsert_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        try
        {
            for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
            {

                if (CheckBoxList1.Items[i].Selected == true)
                {
                    str = str + CheckBoxList1.Items[i].Text + ",";
                }
            }

            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=[DataDirectory]/Database.mdf;Integrated Security=True;User Instance=True";

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "GetSelectedCountry";
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@countrynames", str);
            con.Open();

            int j = com.ExecuteNonQuery();

            if (j > 0)
            {
                lblerror.Text = "insert successfully";
            }
            else
            {
                lblerror.Text = "Not Inserted";

            }
            con.Close();
        }

        catch (Exception ex)
        {
            lblerror.Text = ex.Message;
        }
    }

    protected void btnGetCountryList_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection();

            con.ConnectionString = "Data Source=[DataDirectory]/Database.mdf;Integrated Security=True;User Instance=True";

            SqlCommand com = new SqlCommand();

            com.Connection = con;

            com.CommandText = "GetList";

            com.CommandType = CommandType.StoredProcedure;

            con.Open();

            IDataReader dr = com.ExecuteReader();

            DataTable dt = new DataTable();

            dt.Load(dr);

            CheckBoxList1.DataSource = dt;

            CheckBoxList1.DataTextField = "Countryname";

            CheckBoxList1.DataValueField = "id";

            CheckBoxList1.DataBind();

            con.Close();
        }

        catch (Exception ex)
        {
            lblerror.Text = "Unable to fetch records..." + ex.Message;
        }
    }


3 Comments »

  1. samir says:

    Nice Example…!

  2. Hemalatha says:

    can u send me the stored procedure for it

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>