Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i m working on one asp.net application..
i m facing one problem while working with ms sql
i want to insert the values in one table as i insert the values in another tabel..


like i have one tabel Test1 which contain field UID,fname,Lname
and another tabel Test2 which contain field UID ..
i want UID values in Test2 get incremented as i insert in records in Test1..
Posted
Updated 3-Mar-10 22:33pm
v3

You need triggers...

Read:
MSDN: Trigger[^]

Alternatively, if you need to add something else (you need some manual work) then you can use a stored procedure where you can have both inserts.
OR
have a transaction and commit once both the tables are updated.

Reading Good SQL book will be of great help.
 
Share this answer
 
try following two query


For table1

String str=Insert into test values('"+txtbox1.text+"','"+txtbox1.text+"'....);
sqlcommand com=new sqlcommand(str,connection);
com.executenonquery();


For table 2

first retrive the value of Uid

String str1="Select Max(UID) from table2";
sqlcommand com1=new sqlcommand(str1,connection);
int i=(convert.toint32(com.executeScaler().tostring()));
i=i+1;

then execute insert query;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900