Hi smile,
Check this example. Now please take its reference and correct your code.
Form Design
The Form consists of a CheckedListBox, Panel and Button Controls. Inside the Panel CheckBoxes are added.
Namespaces
C#
using System.Collections.Generic;
using System.Data.SqlClient;
VB.Net
Imports System.Collections.Generic
Imports System.Data.SqlClient
Code
C#
private void Delete(object sender, EventArgs e)
{
string query = "";
IEnumerable<CheckBox> checkBoxes = panel1.Controls.OfType<CheckBox>();
foreach (CheckBox control in checkBoxes)
{
if (control.Checked)
{
query += "TRUNCATE TABLE " + control.Text + Environment.NewLine;
}
}
foreach (object item in chkDatabaseTables.CheckedItems)
{
query += "TRUNCATE TABLE " + item + Environment.NewLine;
}
string conString = @"Data Source=.;Initial Catalog=Test;uid=sa;pwd=123;";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Private Sub Delete(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim query As String = ""
Dim checkBoxes As IEnumerable(Of CheckBox) = panel1.Controls.OfType(Of CheckBox)()
For Each control As CheckBox In checkBoxes
If control.Checked Then
query += "TRUNCATE TABLE " & control.Text + Environment.NewLine
End If
Next
For Each item As Object In chkDatabaseTables.CheckedItems
query += "TRUNCATE TABLE " & item & Environment.NewLine
Next
Dim conString As String = "Data Source=.;Initial Catalog=Test;uid=sa;pwd=123;"
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
On Button click records from Databae table is deleted for the checked CheckBoxes or CheckBoxList checked.