Click here to Skip to main content
16,005,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two tables in SQL server database. The first one(db1) contains information such as username, password, address, phone no etc and the other one(db2) contains information such as username, education, key skills, hobbies etc. The username is the common key between the two.
Ive made two pages in asp.net website for registration. First one has a form with fields username,password, address, phone no. The second one has a form with fields as education, key skills, hobbies. The code is in C#.In the code behind(C#) of second form, I want to compare that if the username from db1 is equal to username from db2, then fill in the details in the table2(db2). How to accomplish this task ? Help!!!
Thnks
Posted
Updated 11-Oct-11 0:02am
v3

AS you are using SQL Server I would pass the information to a stored procedure and then using T-SQL I would do

1. Compare the usernames from the 2 databases i.e.

SQL
declare @UserName VARCHAR(50) = null

select @UserName = a.username
--tableName is the name of the table in database1
from tableName b inner join database2.dbo.TableName b
on a.username = b.username


2. If the name exists in database 2 then do the insert

SQL
if @UserName is not null
begin
 insert into database2.dbo.tablename
 select columnnames
 from tablename
end


Further Reading
Create Stored Procedure[^]

Calling Stored Procedure using ADO.NET[^]
This article is in C# but it is very easy to convert
 
Share this answer
 
Comments
angel 2 11-Oct-11 5:21am    
thank u so much :)
You can query another database within another database connection with the following code:
SQL
select * from database2.dbo.tablename -- execute from database1 connection
 
Share this answer
 
Comments
angel 2 11-Oct-11 5:22am    
thank u so much :)

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