Click here to Skip to main content
15,897,718 members

primary and foreign key problem

workoholic91 asked:

Open original thread
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:
C#
{
	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));
	cmd2.Connection.Open();
	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));
		cmd.Connection.Open();
		lblID.Text = cmd.ExecuteScalar().ToString();
		SqlCommand cmd1 = new SqlCommand("insert into JUWUser(u_id) values ('" + lblID.Text + "')", new SqlConnection(myConnectionString));
		cmd1.Connection.Open();
		cmd1.ExecuteScalar().ToString();
		cmd.Connection.Close();
		cmd1.Connection.Close();
	}
	else
		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
Tags: C# (C# 4.0), ASP.NET, SQL Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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