Solved

Copy table data to another table

Posted on 2011-02-23
5
399 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:shinuq
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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:
shinuq earned 500 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

10 Experts available now in Live!

Get 1:1 Help Now