Doing a double insert using FormView insert and SqlDataSource

Last post 07-27-2007 8:55 AM by abhm. 3 replies.

Sort Posts:

  • Doing a double insert using FormView insert and SqlDataSource

    02-22-2007, 12:02 AM
    • Member
      3 point Member
    • winterstide
    • Member since 02-09-2007, 1:49 PM
    • Posts 6

    I currently have a formview which inserts the data into a SQL 2005 Express Database File.

    What I would like to be able to do, is do the original insert and follow that exactly after with a another insert, changing only 1 parameter all in the same insert click.

    Is this possible and if so how would I go about doing it? I assume by adding code into the onItemInserted event?

     (think newbie here)

  • Re: Doing a double insert using FormView insert and SqlDataSource

    02-22-2007, 12:43 AM
    • All-Star
      19,498 point All-Star
    • e_screw
    • Member since 10-20-2004, 1:22 PM
    • Women, Guitar, Russia, Billiards, Nature, .NET
    • Posts 3,893

    You need to override the FormView's ItemInserting event. Write you insert code in this

    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
      // write insert code here
    }

    If you know the parameters well in hand, you can also use a storedprocedure with multiple insert statements.

    Thanks

    Mark post(s) as "Answer" that helped you

    Electronic Screw
    Website||Blog||Dub@i.net
  • Re: Doing a double insert using FormView insert and SqlDataSource

    02-22-2007, 1:19 AM
    Answer
    • Member
      67 point Member
    • ryarbrou
    • Member since 12-06-2006, 6:40 AM
    • Posts 24

    If I'm reading you right, you would want something more like this...

    Protected Sub sqlProjectData_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles sqlProjectData.Inserted

    e.Command.Parameters("@ParamToChange").Value = "Some Value"

    e.Command.ExecuteNonQuery()

    End Sub

    Note that I put this in the inserted event instead of inserting because you stated that you wanted the original insert to run too.

  • Re: Doing a double insert using FormView insert and SqlDataSource

    07-27-2007, 8:55 AM
    • Member
      2 point Member
    • abhm
    • Member since 05-17-2007, 5:33 AM
    • Posts 1

     Execute scalar, would normally execute the defined sql command. Hence if you put e.Command.ExecuteScalar() in the ..._dataInserted Method of your datasource object or the formview's datainserted event it will cause a second insert.

    If you want to use the idendity or scope_idendity() have a look at this sp:

    ALTER procedure [news].[cnp_InsertCorporate](@Name varchar(1024), @DisplayName  varchar(1024), @Description  varchar(1024), @ContactEmail varchar(1024), @NoticeBoardTitle  varchar(1024), @ExternalAdminPass  varchar(1024), @BFASale  bit, @HomeAllSynopses bit, @HomeNumberSynopses  int, @HasForex  bit, @Newsletter_MoreLinkTitle varchar(1024), @Newsletter_TopLinkTitle varchar(1024))
    as

    -------------------------------------------------------------------------------
    -- cnp_InsertCorporate
    --
    -- Add new corporate to and based on @@identity insert new group and then news user.
    -- Also inserts clients weather & forex if it has weather and forex info.
    --
    -- Written:
    --   29/07/2007  abhm
    -------------------------------------------------------------------------------

    -- variables
    declare @corpid int;
    declare @groupid int;

    -- Insert corpoarate details
    INSERT INTO [tb_cnpCorporates]
    ([Name], [DisplayName], [Description], [ContactEmail], [NoticeBoardTitle], [ExternalAdminPass], [BFASale], [HomeAllSynopses], [HomeNumberSynopses], [HasForex], [Newsletter_MoreLinkTitle], [Newsletter_TopLinkTitle])
    VALUES
    (@Name, @DisplayName, @Description, @ContactEmail, @NoticeBoardTitle, @ExternalAdminPass, @BFASale, @HomeAllSynopses, @HomeNumberSynopses, @HasForex, @Newsletter_MoreLinkTitle, @Newsletter_TopLinkTitle)
    -- Get last inserted CorporateID and assign to @corpid
    SET @corpid =  SCOPE_IDENTITY();

    -- Insert group details
    INSERT INTO    corpnews.news.tb_cnpGroups
    ([CorporateID], [Name], [GroupEmail])
    VALUES       
    (@corpid, @Name, REPLACE('news-&@email.com','&', @Name) )

Page 1 of 1 (4 items)