Hi KatieNgoc,
Refer below sample.
HTML
<div>
Name :
<asp:TextBox runat="server" ID="txtName" />
<br />
Security Question
<br />
Your favourite Fruites :
<asp:CheckBox ID="CheckBox1" Text="Orange" runat="server" />
<asp:CheckBox ID="CheckBox2" Text="Apple" runat="server" />
<asp:CheckBox ID="CheckBox3" Text="Grapes" runat="server" />
<br />
<asp:Button Text="Login" runat="server" OnClick="Insert" />
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Imports System.Linq
Imports System.Collections.Generic
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name,Fruites FROM tblDatas", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
string fruites = dt.Rows[i][1].ToString();
foreach (var control in this.form1.Controls)
{
if (control is CheckBox)
{
string text = (control as CheckBox).Text;
List<string> fruits = fruites.Split(',').ToList();
if (fruits.Contains(text))
{
(control as CheckBox).Checked = true;
}
}
}
}
}
}
}
}
}
protected void Insert(object sender, EventArgs e)
{
string message = "";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblDatas(Name,Fruites) VALUES(@Name,@Fruites)", con))
{
foreach (var control in this.form1.Controls) // I guess this is your form
{
if (control is CheckBox)
{
if (((CheckBox)control).Checked)
{
message += ((CheckBox)control).Text + ",";
}
}
}
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Fruites", message.TrimEnd(','));
con.Open();
int i = Convert.ToInt16(cmd.ExecuteNonQuery());
con.Close();
if (i > 0)
{
Response.Redirect("Default.aspx");
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Name,Fruites FROM tblDatas", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
For i As Integer = 0 To dt.Rows.Count - 1
Dim fruites As String = dt.Rows(i)(1).ToString()
For Each control In Me.form1.Controls
If TypeOf control Is CheckBox Then
Dim text As String = (TryCast(control, CheckBox)).Text
Dim fruits As List(Of String) = fruites.Split(","c).ToList()
If fruits.Contains(text) Then
TryCast(control, CheckBox).Checked = True
End If
End If
Next
Next
End Using
End Using
End Using
End If
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim message As String = ""
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblDatas(Name,Fruites) VALUES(@Name,@Fruites)", con)
For Each control In Me.form1.Controls
If TypeOf control Is CheckBox Then
If (CType(control, CheckBox)).Checked Then
message += (CType(control, CheckBox)).Text & ","
End If
End If
Next
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Fruites", message.TrimEnd(","c))
con.Open()
Dim i As Integer = Convert.ToInt16(cmd.ExecuteNonQuery())
con.Close()
If i > 0 Then
Response.Redirect("Default.aspx")
End If
End Using
End Using
End Sub
Screenshot