In this blog we will know how to insert multiple checkedListBox values to database in windows.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace InsertmultipleValueCheckBoxList
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
checkedListBox1.Items.Add("Raj");
checkedListBox1.Items.Add("Ravi");
checkedListBox1.Items.Add("Rahul ");
}
private void btn_insert_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
foreach (var checkedItem in this.checkedListBox1.CheckedItems)
{
com = new OleDbCommand("Insert into test values('" + checkedItem + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
MessageBox.Show("Inserted Successfully");
}
}
}

Arjun DhilodPosted Feb 12, 2014, 1:48 AM
private void btnSave_Click(object sender, EventArgs e) { string str = string.Empty; DataSet ds1 = new DataSet(); DataView dv; string clientdata=""; foreach (var clientItem in checkedListBoxClientTableField.CheckedItems) { if (checkedListBoxClientTableField.SelectedIndex > -1) { foreach(DataRowView dataFromClient in checkedListBoxClientTableField.CheckedItems) { // clientdata=dataFromClient[0].ToString(); clientdata ="clientdata='" dataFromClient[0].ToString() "'or"; } clientdata = clientdata.Substring(0, clientdata.Length - 4); MySqlConnection myconn3; string SelectClientData = "Data Source=" lblSaveSeverIpClient.Text ";Initial Catalog=" lblSqlDatabaseClient.Text ";user id=" lblSaveUserNameClient.Text ";password=" lblSavePasswordClient.Text ""; myconn3 = new MySqlConnection(SelectClientData); myconn3.Open(); str = clientdata.ToString(); string query = "Select " str " from " lblSqlDatabaseClient.Text "." lblMySqlTableNameClient.Text ""; MySqlDataAdapter mydata = new MySqlDataAdapter(query, myconn3); DataTable dTable = new DataTable(); mydata.Fill(ds1, str); dv = ds1.Tables[0].DefaultView; DataTable table = ds1.Tables[0]; dv.Sort = "" str " DESC"; myconn3.Close(); } // int a = 0; foreach (DataTable dt in ds1.Tables) { } if (ds1.Tables.Count > 0) { string strForSqlServer = string.Empty; DataSet dsForSqlServer = new DataSet(); string strInt=""; foreach (var listOFSQLIntelect in checkedListBoxIntTable.CheckedItems) { if (checkedListBoxIntTable.SelectedIndex > -1) { foreach(DataRowView DriInternal in checkedListBoxIntTable.CheckedItems) { strInt=DriInternal[0].ToString(); } SqlConnection sqlconForMigration; string SqlServerInsert = "Data Source=" lblSaveSeverIpIntellect.Text ";Initial Catalog=" lblSqlDatabase.Text ";user id=" lblSaveUserNameIntellect.Text ";password=" lblSavePasswordIntellect.Text ""; // string SqlServerInsert = "Data Source=localhost;Initial Catalog=" lblSqlDatabase.Text ";user id=root;password=root"; foreach (DataRow dr in ds1.Tables[0].Rows) { sqlconForMigration = new SqlConnection(SqlServerInsert); sqlconForMigration.Open(); strForSqlServer = strInt.ToString(); string query = "insert into " lblSqlTableName.Text "(" strForSqlServer ") values ('" dr[0].ToString() "')"; SqlCommand cmd = new SqlCommand(query, sqlconForMigration); cmd.ExecuteNonQuery(); sqlconForMigration.Close(); } } } string message = "Record Added In MSSQL Table"; string title = "Title"; MessageBox.Show(message); createtablenames(); } } //string path = "D:\\Expences.xml"; //XmlDocument doc = new XmlDocument(); ////If there is no current file, then create a new one //if (!System.IO.File.Exists(path)) //{ // //Create neccessary nodes // XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"); // XmlComment comment = doc.CreateComment("This is an XML Generated File"); // doc.AppendChild(declaration); // doc.AppendChild(comment); //} //else //If there is already a file //{ // doc.Load(path); //} } this is my code i want to do the same but want select multiple field from one table and insert in to anothere single colum wt i do
Arjun DhilodPosted Feb 12, 2014, 1:46 AM
hi