0
Answer

use multiple checked items from the CheckedListBox from DB

Arjun Walmiki

Arjun Walmiki

10y
978
1
Dear All,

 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();
                  
                     
                }

             

            }
          

        }

the above is my code i am using 2 diffrent database i want to select

multiple checked items from the CheckedListBox and insert in new table as per

CheckedListBox

checked item

pls help me