Multiple checkbox selection value in Asp.Net C#

In this tutorial we have learn Multiple checkbox selection value in Asp.Net C#,here we have bind multiple checkbox in asp.net c# and select checkbox value and insert checkbox value to SQL server database

Step 1 : Create sql server database

CREATE TABLE [dbo].[EMP] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Hobby] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

Step :2  Now Create Data Layer means create class DAL.cs

namespace Check_Box_Control
{
public class DAL
{
public int ID { get; set; }
public string Name { get; set; }
public string hobby { get; set; }
}
}

Step 3: Now Create Busseness layer means BAL.cs

namespace Check_Box_Control
{
public class BAL
{
SqlConnection con = new SqlConnection(@”Data\Database1.mdf;Integrated Security=True“);

public void Insert(DAL DA)
{
con.Open();
SqlCommand cmd = new SqlCommand(“Insert into EMP values(‘”+DA.Name+”‘,'”+DA.hobby+”‘)”,con);
cmd.ExecuteNonQuery();
con.Close();
}

public DataTable Editdata(DAL da)
{
SqlDataAdapter adp = new SqlDataAdapter();
DataTable dt = new DataTable();
adp = new SqlDataAdapter(“select * from EMP Where Name='” + da.Name + “‘ “, con);
adp.Fill(dt);
return dt;
}
}

Step 4 : Add WebForm and codebehind file as below code

public partial class WebForm1 : System.Web.UI.Page
{
DAL da = new DAL();
BAL ba = new BAL();
string str = “”;
DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
da.Name = TextBox1.Text;

foreach (ListItem dd in CheckBoxList1.Items)
{
if (dd.Selected)
{
str += dd.Text + “,”;
}

}
da.hobby = str;
ba.Insert(da);
Response.Write(“data“);
}

protected void Button2_Click(object sender, EventArgs e)
{
da.Name = TextBox1.Text;
dt= ba.Editdata(da);

TextBox1.Text = dt.Rows[0][“Name“].ToString();
CheckBoxList1.Text = dt.Rows[0][“Hobby“].ToString()
}
}

Step 5 : Now run asp.net web project and see output
Multiple checkbox selection value in Asp.Net C#

SEE MORE

4 Comments

Leave a Reply

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