Solved

Copy table data to another table

Posted on 2011-02-23
5
432 Views
Last Modified: 2012-05-11
Hi experts

Im using sql server 2008 and C#.net 2010 and where i populate a db table from my app i need to copy that data to another table in same db, anyone know how to do this?

Im wanting to copy the data when im inserting data to my table when from my button event

Thanks
0
Comment
Question by:razza_b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34967682
the better option seems to be using the Trigger in the database.
The event should be fired when there is a insert in the first table.

hope this helps
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34967684
Can you show us the code that you are using right now ?

There are 2 possibilities:
1. When performing an insert, also execute another insert-statement to the other table
2. Add a trigger to the table that copies the inserted row to another table
    http://www.tek-tips.com/viewthread.cfm?qid=1353219&page=423
0
 
LVL 14

Expert Comment

by:dejaanbu
ID: 34967690
you can use normal ado.net database method with the following query

INSERT INTO DestinationTable (COLUMNS)   SELECT COLUMN  FROM SourceTable

Open in new window


Or you can use a generic function to do this...so that u can reuse it...

refer:
http://www.codeproject.com/KB/database/GenericCopyTableDataFcn.aspx
0
 
LVL 1

Author Comment

by:razza_b
ID: 34967793
button insert snippet...
cmd2.Connection = cnn;
cmd2.CommandType = CommandType.Text;
cmd2.Parameters.AddWithValue("@MONUMBER", lblEnteredJob.Text);
cmd2.Parameters.AddWithValue("@MATERIAL", txtPartNumber.Text);
cmd2.Parameters.AddWithValue("@QTY", txtQty.Text);
cmd2.Parameters.AddWithValue("@SERIALNUMBER", txtSerialNumber.Text.ToString());
cmd2.Parameters.AddWithValue("@VENDORDATE", txtVendorDate.Text);
cmd2.Parameters.AddWithValue("@VENDORLOT", txtVendorLot.Text);
cmd2.Parameters.AddWithValue("@BATCHNUMBER", txtBatch.Text);
cmd2.Parameters.AddWithValue("@SAPDOC", txtSAPDoc.Text);
cmd2.Parameters.AddWithValue("@MPN", txtMPN.Text);
cmd2.CommandText = "INSERT INTO wp4tbl_Tblcvskitverify (MONUMBER,VendorLot,Qty,SerialNumber,VendorDate,Material,Batchnumber, sapdoc, mpn) VALUES (@MONUMBER" + ",@VENDORDATE,@QTY,@SERIALNUMBER,@VENDORLOT,@MATERIAL,@BATCHNUMBER,@SAPDOC,@MPN)";
cmd2.ExecuteNonQuery();

the other table has a lot more col's but all i need to to copy over is ",@VENDORDATE,@QTY,@SERIALNUMBER,@VENDORLOT,@MATERIAL,@BATCHNUMBER,@SAPDOC

Thanks
0
 
LVL 17

Accepted Solution

by:
Shinesh Premrajan earned 500 total points
ID: 34967862
Add this additional code below the script.

you need to set the table name correctly here

wp4tbl_Tblcvskitverify  should be replaced in the below query
cmd3.Connection = cnn;
cmd3.CommandType = CommandType.Text;
cmd3.Parameters.AddWithValue("@VENDORDATE", txtVendorDate.Text);
cmd3.Parameters.AddWithValue("@QTY", txtQty.Text);
cmd3.Parameters.AddWithValue("@SERIALNUMBER", txtSerialNumber.Text.ToString());
cmd3.Parameters.AddWithValue("@VENDORLOT", txtVendorLot.Text);
cmd3.Parameters.AddWithValue("@MATERIAL", txtPartNumber.Text);
cmd3.Parameters.AddWithValue("@BATCHNUMBER", txtBatch.Text);
cmd3.Parameters.AddWithValue("@SAPDOC", txtSAPDoc.Text);

cmd3.CommandText = "INSERT INTO wp4tbl_Tblcvskitverify (VendorDate,Qty,SerialNumber,VendorLot,Material,Batchnumber, sapdoc) VALUES (@VENDORDATE,@QTY,@SERIALNUMBER,@VENDORLOT,@MATERIAL,@BATCHNUMBER,@SAPDOC)";
cmd3.ExecuteNonQuery();

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question