I am trying to store a value from a selected row from one table into another. My code is as follows.
string CustomerID = textBox1.Text;
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
con1.Open();
SqlCommand myCommand = new SqlCommand("Select CustomerID, Name from TableCustomer WHERE (CustomerID like '" + CustomerID+ "')", con1);
SqlDataReader rdr = myCommand.ExecuteReader();
while (rdr.Read())
{
string Name = rdr["Name"].ToString();
}
SqlCommand cmd = new SqlCommand(@"insert into finalTable (AccountNumber, CustomerName) VALUES
(@CustomerID, @Name )", con);
cmd.ExecuteNonQuery();
con1.Close();
My code is not working properly. All is I want to insert Name and ID of customer in Name field of finalTable. What is wrong with this code?