Solved

Copy table data to another table

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
export sql results to csv 6 36
SSRS 2012 r2 - Parm Drop Down has Date/Time 12 33
C# Gridview 1 33
How to seperate number and letters from a string 3 11
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

803 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