Insert Checkbox Values in Database using C#

Insert Checkbox Values in Database using C#

Crete Connection with connection string.

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“PropertyGuruConnectionString”].ToString());    
//Make an Open Connection to the SQL Server database
//Here ‘PropertyGuruConnectionString’ is the name of the string used for connection with database
//Our aim is to add Checkbox values in Database
//Lets Add a three checkboxes with different values and a button to submit those values in database table.
//We need to create the database table for storing values as well.
On Button Click

Here Three Checkboxes have Respective Ids – chkApartment, chkVilla, chkRest

protected void btnPropType_Click(object sender, EventArgs e)
    {

        if (chkApartment.Checked == true)
        {
           lblRandom3.Text = “Apartment”;

        }else if (chkVilla.Checked == true)
        {
            lblRandom3.Text = “Villa”;

        }else if (chkRest.Checked == true)
        {
            lblRandom3.Text = “Rest”;

        }

        string mycommand = “insert into quality(name) values (‘” + lblRandom3.Text + “‘)”;
         SqlCommand cmd = new SqlCommand(mycommand, con);
         con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write(“inserted”);
       
    }

Here We are inserting the values in “name” column of quality table.

lblRandom3 is the id of a Label in which we are storing the selected value.

*This method is to insert one value at a time.

Social Share

Leave a Reply

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