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
Solved

Copy table data to another table

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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