Save (Insert) CheckBox Value to Database in ASP.Net example

Last Reply on Dec 20, 2013 02:10 AM By Azim

Posted on Dec 19, 2013 11:23 PM

Hi friends,

I am using a checkbox  when  CheckBox is selected value="1" should be inserted in GridView if Not checked value="0"

protected void btnsubmit_Click(object sender, EventArgs e)
    {
using (SqlConnection con = new SqlConnection(CS))
                {
                    SqlCommand cmd = new SqlCommand("insert into tblfacilitymaster(catid,facilityname,activestatus,filtertype)values('" +ddlcategory.Text+ "','" + txtname.Text + "','"+ dropstatus.Text + "','"+chksort.Text+"')", con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
                BindData();
}

chksort.Text is my checkbox value

 

THANKS IN ADVANCE

Posted on Dec 20, 2013 02:10 AM Modified on on Dec 20, 2013 02:18 AM

Reference: Save (Insert) CheckBox value to Database in ASP.Net using C# and VB.Net

HTML:

<form id="form1" runat="server">
    <div>
        Id
        <asp:TextBox ID="txtId" runat="server" /><br />
        Name
        <asp:TextBox ID="txtName" runat="server" /><br />
        City
        <asp:TextBox ID="txtCity" runat="server" /><br />
        <asp:CheckBox ID="chkStatus" Text="Maried?" runat="server" />
        <asp:Button ID="Button1" Text="Insert" OnClick="Insert" runat="server" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Status" HeaderText="Status" />
  </Columns> </asp:GridView> </div> </form>

C#:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Populate();
            }
        }

        protected void Insert(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
            using (SqlConnection _cn = new SqlConnection(constr))
            {
                using (SqlCommand _cmd = new SqlCommand("INSERT INTO employee VALUES(@Id,@Name,@City,@Status)", _cn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
                    {
                        _cn.Open();

                        _cmd.Parameters.AddWithValue("@Id", this.txtId.Text.Trim());
                        _cmd.Parameters.AddWithValue("@Name", this.txtName.Text.Trim());
                        _cmd.Parameters.AddWithValue("@City", this.txtCity.Text.Trim());
                        _cmd.Parameters.AddWithValue("@Status", this.chkStatus.Checked ? "1" : "0");
                        _cmd.ExecuteNonQuery();
                        _cn.Close();
                    }
                }
                this.Populate();
            }
        }

        private void Populate()
        {
            string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
            using (SqlConnection _cn = new SqlConnection(constr))
            {
                using (SqlCommand _cmd = new SqlCommand("SELECT * FROM employee", _cn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
                    {

                        _cn.Open();
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        GridView1.DataSource = ds;
                        GridView1.DataBind();

                        _cn.Close();
                    }
                }
            }
        }

SQL:

CREATE TABLE [dbo].[employee](
	[City] [varchar](30) NULL,
	[Name] [varchar](30) NULL,
	[Id] [varchar](30) NULL,
	[Status] [char](10) NULL
) ON [PRIMARY]

GO

Image: