code for Insert checkedListBox Values Into a Single Column c# windows Application
Code for Insert Values Into a Single Column c# windows
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
code for Insert checkedListBox Values Into a Single Column c# windows Application
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 8, 2023, 5:32 AM EDT
using System; using System.Data.SqlClient; using System.Windows.Forms; namespace CheckedListBoxExample { public partial class Form1 : Form { private string connectionString = "Your_Connection_String_Here"; public Form1() { InitializeComponent(); } private void btnInsert_Click(object sender, EventArgs e) { try { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Get selected items from CheckedListBox string selectedItems = string.Join(",", checkedListBox1.CheckedItems); // Insert selected items into the database //Replace "Your_Connection_String_Here" with your actual SQL Server connection //string and "YourTableName" with the table name to insert the values string query = "INSERT INTO YourTableName (YourColumnName) VALUES (@SelectedItems)"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@SelectedItems", selectedItems); command.ExecuteNonQuery(); } MessageBox.Show("Selected items inserted successfully."); } } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message); } } } }