0

I am trying to read datas from one database table and then add it to another table. This is my example and code.

This is the table I am reading from: enter image description here

And this is the table when I save it:

enter image description here

This is the code I use:

protected void Button1_Click(object sender, EventArgs e)
    {
        var db = Database.Open("ConnectionString");
        var FJSCR = db.Query("SELECT * FROM [SALESAGENT]");

        foreach (var item in FJSCR)
        {
            Label1.Text += item.CODE;
            //Label1.Text += "<hr/>";

            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                con .Open();
                string FJ = "EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'";
                FJ = "insert into MAG(COD) VALUES (@COD)";
                SqlCommand kom = new SqlCommand(FJ, con);
                kom.Parameters.AddWithValue("@COD", Label1.Text);
                kom.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                Response.Write("Error " + ex.ToString());
            }
        }  
    }

Any help? Thank You!

4
  • So what you want to achieve ? Commented Jul 12, 2014 at 4:51
  • Is that repeated Data is Problem ? Commented Jul 12, 2014 at 4:52
  • @user2767299 , I want to add the datas from the first picture to another table which is the second picture. Commented Jul 12, 2014 at 4:59
  • @Grant Winney , Nope, just as-is. I want this way. This is the way I was requested to do. Commented Jul 12, 2014 at 5:02

1 Answer 1

2

If you just straight away COPY the data from one table to another, no need to use label in between.

just change ;

kom.Parameters.AddWithValue("@COD", Label1.Text);

To

kom.Parameters.AddWithValue("@COD", item.CODE);

OR

If you want to use LABEL then GET New record value in label every time, Do not APPEND;

In that case, replace;

 Label1.Text += item.CODE;

TO

 Label1.Text = item.CODE;

Hope this is what you are looking for..!!!

Sign up to request clarification or add additional context in comments.

1 Comment

Tried the first one. It worked. Thanks a lot. You saved me. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.