简体   繁体   中英

Insert into two tables with one table having an auto increment primary key and that same key is used as a foreign key in another table

Hi guys can anyone please assist me with the following. I using C# , asp.net and SQL Server. I have two tables [MANAGER] and [USER]. I want to add a user into the [USER] table and at the same time add that same user into the [MANAGER] table. Here is my code:

    protected void btnManagerRegister_Click(object sender, EventArgs e) 
    {        

     using (SqlConnection con = new SqlConnection(CS))

    {            
     SqlCommand cmd = new SqlCommand("INSERT INTO [USER] (userFirstName, userSurname, userEmailAddress, userName, password, userType) VALUES(@userFirstName, @userSurname, @userEmailAddress, @userName, @password, @userType),INSERT INTO MANAGER (@IDENTITY,teamName) VALUES(@IDENTITY,@teamName);", con);

    cmd.Parameters.Add(new SqlParameter("@userFirstName", txtManagerName.Text));             cmd.Parameters.Add(new SqlParameter("@userSurname", txtManagerSurname.Text));             cmd.Parameters.Add(new SqlParameter("@userEmailAddress",txtManagerEmailAdd.Text));             cmd.Parameters.Add(new SqlParameter("@userName", txtManagerUserName.Text));             cmd.Parameters.Add(new SqlParameter("@password", txtManagerPassword.Text));
    cmd.Parameters.Add(new SqlParameter("@teamName", txtTeamName.Text));
    cmd.Parameters.Add(new SqlParameter("@userType", "M" ));  


    con.Open();              

    if (txtManagerPassword.Text != txtConfirmManagerPassword.Text)  

     {   

     }             

     else  cmd.ExecuteNonQuery();                         Response.Redirect("~/Login.aspx");          

    }    
         }

Your insert statement into Manager table is a bit strange, you have mentioned a variable name in the columns section, also capture the identity value into a variable before and then use that variable to insert into Manager table. Something like...

INSERT INTO [USER] (userFirstName, userSurname, userEmailAddress, userName, password, userType) 
VALUES (@userFirstName, @userSurname, @userEmailAddress, @userName, @password, @userType);

 SET @IDENTITY = SCOPE_IDENTITY();

INSERT INTO MANAGER (ManagerID , teamName) --<-- use the actual column name for ManagerID.
VALUES(@IDENTITY,@teamName);

Also use SCOPE_IDENTITY() function instead of @@Identity function.

I'm not sure what you're using the @IDENTITY for ? If your Manager table has an IDENTITY column, then you either specify it like this :

INSERT INTO MANAGER (IdentityColumn, teamName) VALUES(NEWID(), @teamName)

or exclude it, and SQL Server will generate a new ID automatically.

if you're trying to insert the last userid (which I think clear things up) (thanks marc_s). You can use SCOPE_IDENTITY()

INSERT INTO MANAGER (useridColumnName, teamName) VALUES(SCOPE_IDENTITY(), @teamName)

Here is the code :

if (txtManagerPassword.Text != txtConfirmManagerPassword.Text)  
{  
    //[TODO] Password doesn't match
}
else 
{

    using(SqlConnection con = new SqlConnection(CS))
    using(SqlCommand cmd = new SqlCommand("INSERT INTO [USER] (userFirstName, userSurname, userEmailAddress, userName, password, userType) VALUES(@userFirstName, @userSurname, @userEmailAddress, @userName, @password, @userType); INSERT INTO MANAGER (teamName) VALUES(@teamName)", con))
    {            
        cmd.Parameters.Add(new SqlParameter("@userFirstName", txtManagerName.Text));
        cmd.Parameters.Add(new SqlParameter("@userSurname", txtManagerSurname.Text));
        cmd.Parameters.Add(new SqlParameter("@userEmailAddress",txtManagerEmailAdd.Text));
        cmd.Parameters.Add(new SqlParameter("@userName", txtManagerUserName.Text));
        cmd.Parameters.Add(new SqlParameter("@password", txtManagerPassword.Text));
        cmd.Parameters.Add(new SqlParameter("@teamName", txtTeamName.Text));
        cmd.Parameters.Add(new SqlParameter("@userType", "M" ));  

        con.Open();              
        cmd.ExecuteNonQuery();

    } 

    Response.Redirect("~/Login.aspx");              
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM