You must Sign In to post a response.
  • Category: ASP.NET

    How to insert three table in one button

    Dear All,

    I have 3 table in my database,in 3 table is related to each other and i have one asp.net page. in this page 3 table field is available so what i want to do.
    when i will click submit button i want save on 3 table.how to do.
  • #690960
    Hello,
    You can save into 3 tables with 3 query and you need to combine all those 3 query using ';'.
    Ex :

    insert into Table1 Values('abc','bcd');insert into Table2 Values('abc','bcd');insert into Table3 Values('abc','bcd','xyz');

    If it helps you then Rate this.

    Best Regards,
    Manoranjan Sahoo
    http://www.dotnetsquare.com
    Blog: http://blog.msahoo.net

  • #690962
    Prepare a Stored Procedure using joins if there are any relationship with other tables.

    Invoke Stored Procedure in your front end.

  • #690963
    shaik,

    1) First option you can write separate query under the same button click event and execute it to insert like above example.

    2) Write trigger to perform after inser in one table to other like below example. I have insert same data in other table but that same data update in the other table too


    create table leave(id int,Name varchar(50),days varchar(40))

    create table leave_permit(id int,Name varchar(50),days varchar(40))

    select * from leave
    select * from leave_permit

    CREATE TRIGGER [dbo].[TRGLEAVE]
    ON [dbo].[leave]
    AFTER INSERT
    AS
    BEGIN
    SET NOCOUNT ON;
    DECLARE @id INT
    DECLARE @Name VARCHAR(50)
    DECLARE @days VARCHAR(50)
    Select @id = id,@Name=Name,@days=days FROM Inserted
    insert into leave_permit values(@id,@Name,@days)
    END


    Now insert new record in leave table automatically leave_permit table too got that records

    Regards
    N.Ravindran
    Your Hard work never fails

  • #690966
    Hi,
    You can insert individually to all the three tables. make sure to insert parent table first and then child tables. Let us know if you are facing any issue while inserting the record in three tables.


    Regards,
    Asheej T K

  • #691010
    Hai Shaik,
    If the tables are related to each other then why do you need to insert the data in to all three tables?
    You just need to insert in to one table and other will get reference from that table.
    Lets support, you want to insert the Employee details who is from a particular city, State and Country.
    Then you don't need to insert the data for the State and country. You only need to insert it for City as the city belongs to particular state and the state belong to particular country.
    So if your scenarios is something else, then you need to insert the records in to first table and then based on the key value you can insert to the second and similarly you can insert to the 3rd table.
    Lets suppose, you have the product table, Category table and the customer details.
    When the customer buys a product, the data should be inserted to the customer table. What the product bought, should be reduced from the products table as it is sold out and at the same time, the shipment for the product should be inserted to the the shipping table.
    So for this type of scenario, first you need to insert in to customer table with the product id. Based on this product id, the Products table will reduce the item by using the delete command and based on this customer id, the new row will be inserted with the product details to the Shipping table.
    Hope it will give you some idea in the implementation.
    let me know if anything is unclear.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.