Solved

Copy table data to another table

Posted on 2011-02-23
5
411 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
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now