Asp.Net Checkbox Control how to Select And Insert Multiple Item in the Ckeckbox Control in C# With SQL Server Database
Introduction : This Artical Explain How to Select Multiple items in ckeckbox and how to insert multiple item in Sql server Database In Asp.net 3-Tier
Step :1 Open Visual Studio and Create new Project and Add new Database as following
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)
);
[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; }
}
}
{
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 Source=(LocalDB)\v11.0;AttachDbFilename=
D:\program\Check_Box_Control\App_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;
}
}
{
public class BAL
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=
D:\program\Check_Box_Control\App_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 add code in codebehind file
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()
}
}
{
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 : Run program
Sign up here with your email
ConversionConversion EmoticonEmoticon