How to insert multiple Checkboxlist value into database in asp.net c#

In this article, we will save many check box values in one data base column. Many times we need to save value multiple values on one column so we can use this below code  and you can also learn how to fetch that values means How to fetch database values in checkbox list in asp.net c#
Design:-



Table Design:-


Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chkboxlist.aspx.cs" Inherits="chkboxlist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <fieldset style="width: 150px">
            <legend>Do you have </legend>
            <asp:CheckBoxList ID="CBLID" runat="server" AutoPostBack="True">
                <asp:ListItem>PAN Card</asp:ListItem>
                <asp:ListItem>Passport</asp:ListItem>
                <asp:ListItem>Student ID</asp:ListItem>
                <asp:ListItem>Voter ID</asp:ListItem>
                <asp:ListItem>Aadhar Card</asp:ListItem>
            </asp:CheckBoxList>
            <asp:Button ID="BtnSubmit" runat="server" Text="Submit"
                onclick="BtnSubmit_Click" />
        </fieldset>
    </div>
    </form>
</body>
</html>


Code behind(C#):-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class chkboxlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        String str = "";

        for (int i = 0; i <= CBLID.Items.Count - 1; i++)
        {
            if (CBLID.Items[i].Selected)
            {

                if (str == "")
                {

                    str = CBLID.Items[i].Text;

                }

                else
                {

                    str += "," + CBLID.Items[i].Text;

                }
            }
        }

        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into reg(IDcard) values('" + str + "')", con);
        cmd.ExecuteNonQuery();
        Response.Write("Your data Sucessfully saved");
        CBLID.SelectedIndex=-1;
    }
}


Out-Put:-





How to insert multiple Checkboxlist value into database in asp.net c# How to insert multiple Checkboxlist value into database in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 1:08:00 AM Rating: 5

6 comments:

  1. tell about the different between sql code and mysql ???

    ReplyDelete
  2. i am start the develop the website and basically i work on sqldata base but our company use mysql database server so i get trouble to writing code ..

    ReplyDelete
  3. Its good spinnet,very helpfull and easy to implement.

    thank you sooo much..

    ReplyDelete
  4. now same way how to display store value again in checkbox

    ReplyDelete
    Replies
    1. this code will help you

      http://www.neerajcodesolutions.com/2013/12/how-to-fetch-database-values-in.html

      Delete
  5. Neeraj sir i request to you please upload comment post with reply button in c#,sql for website please..........

    ReplyDelete

Powered by Blogger.