Click here to Skip to main content
13,450,057 members (52,997 online)
Rate this:
Please Sign up or sign in to vote.
I am using ASP.NET 4.0 and SQL server 2008 R2 in my project. I have used membership provider for creating user.I have taken the primary key (UserId) from aspnet_Users table as a foreign key (u_id) in JUWUser table.
Now the problem is that I have to insert the user id from the primary key column to the foreign key column. I have tried using select and insert statement, but it is not working.
I am retrieving the value from the database in my label so that I can insert the id in its right place.

Following is my code:
	lblname.Text = Convert.ToString(Session["name"]);
	string myConnectionString = @"Data Source=(local);Initial Catalog=PortalDB;Integrated Security=True";

	SqlCommand cmd2 = new SqlCommand("select UserName from JUWUser", new SqlConnection(myConnectionString));
	lblcmpr.Text = cmd2.ExecuteScalar().ToString();

	if (lblname.Text == lblcmpr.Text)
		SqlCommand cmd = new SqlCommand("select UserId from aspnet_Users where UserName ='" + lblname.Text + "'", new SqlConnection(myConnectionString));
		lblID.Text = cmd.ExecuteScalar().ToString();
		SqlCommand cmd1 = new SqlCommand("insert into JUWUser(u_id) values ('" + lblID.Text + "')", new SqlConnection(myConnectionString));
		lblID.Text = "error";

This is the code of profile page.
In this code lblname is storing value from a session variable which contains the Username of the User that has logged in.
lbl cmpr is retrieving all the values from UserName column in the JUWUser table.
lblID is retrieving the ID from the aspnet_Users table where the UserName column value in aspnet_Users matches the value in lblname.text.

Now I have to insert this id in JUWUser table in u_id column where the UserName matches lblname.text

An insert and where statement cannot come together.

How should I do this?

Please Help
Posted 14-Nov-12 22:05pm
Updated 14-Nov-12 23:18pm
biswarup88 15-Nov-12 4:46am
try to simplify your question so that it will be easy for us to answer.
Sergey Alexandrovich Kryukov 25-Nov-12 17:06pm
Please don't post your comments as "solution". Comment the answers you got.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Instead of getting the code this much complex you can go for stored proceure
simply add lblname.Text as parameter to the procedure
And ur procedure shold be as:

create procedure proc1
@name varchar(50)
declare @name1 varchar(50)=null
declare @id1 int=0
select @name1=UserName from JUWUser
if @name=@name1
select @id1=UserId from aspnet_Users where UserName =@name
insert into JUWUser(u_id) values (@id1)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to test to see if there is already a record in that table for that user.

And use a stored procedure.

Count number of records from JUW where username = the user name.
if count == 0 then INSERT
if count == 1 then UPDATE[^] That shows you how to use the UPDATE, and also browse around for some other useful SQL info.

Good luck!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180318.3 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2018
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