Click here to Skip to main content
11,711,009 members (83,339 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
hi frnds,

am working on asp.net project.using C# SqlServer 2005

I have 2 tables in SqlServer database

I want to move all the data from table A to Table B when submit button clicks on my asp.net webpage.

Please can u help me how to do this ?

Thanks in advance
Posted 12-Apr-13 20:09pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

query="INSERT INTO Table2(Tb2Column1, Tb2Column2)
  SELECT Tb1Column1, Tb1Column2 FROM Table1"
SqlCommand cmd = new SqlCommand(query, con);

NOTE:
1. Data Type of column should match with data type of column.
2. You can select desired number of columns.
3. Column with Primary key can not be copied.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

CREATE TABLE YOUTABLE_B(ID BIGINT,NAME VARCHAR(200),ADDRESS VARCHAR(500))
CREATE TABLE YOUTABLE_A(ID BIGINT,NAME VARCHAR(200),ADDRESS VARCHAR(500),
					DELETED_TIME SMALLDATETIME NOT NULL DEFAULT GETDATE())
 
INSERT INTO YOUTABLE_B VALUES(1,'YOUR_NAME','YOUR_ADDRESS')
INSERT INTO YOUTABLE_B VALUES(2,'YOUR_NAME','YOUR_ADDRESS')
 
Create TRIGGER dbo.TRG_YOU_TRG_NAME
ON dbo.YOUTABLE_B
FOR DELETE
AS
BEGIN
   INSERT INTO dbo.YOUTABLE_A
   (ID,NAME,ADDRESS,DELETED_TIME)
   SELECT ID,NAME,ADDRESS,getdate()
   FROM deleted;
END;
GO
 
DELETE FROM YOUTABLE_B WHERE ID = '1' -- or delete all. 
--on submit button you write ^ above Query 
-- data of table B will be inserted in TAble B automatically when trigger fired on it

select * from YOUTABLE_B
select * from YOUTABLE_A

Hope so it will be usefull for you
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 549
1 OriginalGriff 255
2 Peter Leow 154
3 Maciej Los 120
4 Abhinav S 120
0 Maciej Los 95
1 Richard MacCutchan 90
2 Abhinav S 90
3 OriginalGriff 80
4 Member 4653839 50


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100