Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Expert,

I am uday Satardekar,

I am developing asp.net website and i have to insert in 2 table in MySql.

First table is tbl_login and 2nd Table is tbl_user_role
Both tables are autoincremanted.

tbl_user_role contains user_id which will be last inserted id from tbl_login

My code for inserting in tbl_login is following


C#
string queryStr = "INSERT INTO tbl_login(username,password,name,designation,category_assign,email) VALUES (?username,?password,?name,?designation,?categoryAssign,?email)";


command1 = new MySqlCommand(queryStr, connect);
command1.Transaction = transaction;

command1.Parameters.AddWithValue("?username", username);
command1.Parameters.AddWithValue("?password", EncrptPwd);
command1.Parameters.AddWithValue("?name", name);
command1.Parameters.AddWithValue("?designation", designation);

command1.Parameters.AddWithValue("?categoryAssign", categoryAssign);
command1.Parameters.AddWithValue("?email", email);

command1.ExecuteNonQuery();



Now i have to insert in 2nd table tbl_user_role and I have to insert autoincremented id from tbl_login into table <b>tbl_user_role</b>


How To insert in 2 table with transaction?

Please Help me.

Thankssssssss in adv.
Posted

Hi Uday,
you want to insert last inserted user_id from tbl_login table into user_role table..?

you can write Trigger here to this.....
<pre lang="SQL">set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE TRIGGER [Trigger_Insert_IWO_ID] ON [dbo].[tbl_login]
FOR INSERT
AS

INSERT INTO user_role
        -- (col1, col2    , col3, user_id, user_name)
             (iwo_no)
    SELECT
        user_id
 
        FROM inserted
 
Share this answer
 
Comments
udusat13 17-Oct-11 7:44am    
Thanksssss For u r suggestion.

But Sir is it possible without triggers.
Is there any function like identity() present in sql server

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