Hi,
I would like to do a Loop with these codes. But I need to explain very well what I try to do.
1) On my form I have a Datagrid, one TextBox and a Button
2) I have a table name “Test”with a 2 columns (Company_name and Company). This table have 5 records
3) After displaying each “Company” its should send all company’s names into one table named “Receive”
Let’s see what I do. Its works but without with loop:
Company_name Company
--------------------- ------------
Andrews Company1
McSean Company2
Nokia Company2
TelCell Company1
Flex Company1
- conn.Open();
- SqlCommand cmd1 = conn.CreateCommand();
- cmd1.CommandType = CommandType.Text;
- cmd1.CommandText = "select * from Test where company = @company;
- var param1 = new SqlParameter("@company", SqlDbType.NChar);
- param1.Value = txtBoxCompany.Text;
- {
- cmd1.Parameters.Add(param1);
- cmd1.Parameters.Add(param2);
- }
- cmd1.ExecuteNonQuery();
- DataTable dt1 = new DataTable();
- SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
- da1.Fill(dt1);
- conn.Close();
- dgvTest.DataSource = dt1;
- Then after displaying the result on my datagrid its should send in block names of company on other table “Receive”
- string myConnectionString;
- myConnectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Test\\WindowsFormsApplication1\\App_datas\\Accounting.mdf;Integrated Security = True;Integrated Security = True";
- using (var con = new SqlConnection())
- {
- con.ConnectionString = myConnectionString;
- con.Open();
- using (var cmd = new SqlCommand())
- {
- cmd.Connection = con;
- cmd.CommandType = System.Data.CommandType.Text;
- cmd.CommandText = @"INSERT INTO Test (company_name, company) SELECT company_name, company FROM test where company = @company";
- cmd.Parameters.AddWithValue("@company", txtBox_company.Text);
- cmd.ExecuteNonQuery();
- }
- con.Close();
- }
- Console.WriteLine("Done.");
But all of these should doing as a loop one by one record until read and transfering the last record. Many thanx.