FormView OnInserted fires twice

I have a FormView in which the SQLDataSource for it (SqlDataSource1) includes:

OnInserted="NewRecordDataSource_Inserted"

The Protected Sub NewRecordDataSource_Inserted calls another sub that sends out an email.  This email is being sent twice and I suspect that it may be due to 'OnInserted' firing twice.

That or something else is causing the Sub NewRecordDataSource_Inserted to fire twice.  Any ideas what may be causing it?  


Protected Sub NewRecordDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Inserted
        'Read the value of the @NewID OUTPUT parameter 
        Dim NC_ID As Integer = Convert.ToInt32(e.Command.Parameters("@NewID").Value)
        SqlDataSource1.SelectParameters("ID").DefaultValue = NC_ID
                
        Call SendMail()
    End Sub

Open in new window

cgcmqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kumaraswamy RCommented:
make it   AutoEventWireup="false"  in aspx page
0
cgcmqAuthor Commented:
I added that to the Page directive but it is still firing twice.

<%@ Page Title="Labour Day BBQ" Language="VB" MasterPageFile="~/Site.master" Debug="true" AutoEventWireup="false" %>

Note, that in the FormView I have a dropdownlist that is set to AutoPostBack="true"  

Could this be the culprit?
0
guru_samiCommented:
remove this as well from FV markup: OnInserted="NewRecordDataSource_Inserted"

or share your code
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

cgcmqAuthor Commented:
The entire code behind is attached.
Dim CompanyID As Integer
 
    Protected Sub NewRecordDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Inserted
        'Read the value of the @NewID OUTPUT parameter 
        Dim NC_ID As Integer = Convert.ToInt32(e.Command.Parameters("@NewID").Value)
        SqlDataSource1.SelectParameters("ID").DefaultValue = NC_ID
                
        Call HasBeenAdded_Inserted(NC_ID)
        Call SendMail()
    End Sub

    Protected Sub RecordUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Updated
        GridView1.DataBind()
    End Sub
    
    Protected Sub chgOverNighting(ByVal sender As Object, ByVal e As System.EventArgs)
        If CType(FormView1.FindControl("OverNighting"), DropDownList).SelectedValue = True Then
            CType(FormView1.FindControl("SleepingArrangement"), DropDownList).Enabled = True
            CType(FormView1.FindControl("SleepingArrangement"), DropDownList).Focus()
        Else
            CType(FormView1.FindControl("SleepingArrangement"), DropDownList).Enabled = False
            CType(FormView1.FindControl("Potluck"), TextBox).Focus()
        End If
        
    End Sub
    
    Sub HasBeenAdded_Inserted(ByVal NewID As Integer)
        HasBeenAdded.Visible = True
        FormView1.DefaultMode = FormViewMode.Edit
        GridView1.DataBind()        
    End Sub
    
    Sub SendMail()
        Dim sc As SmtpClient = New SmtpClient()
        'Dim msg As MailMessage = Nothing
        Dim SendToAddress As String = CType(FormView1.FindControl("email"), TextBox).Text.ToString
        Dim sb As StringBuilder = New StringBuilder()
        
        sb.Append("<p>" + CType(FormView1.FindControl("firstname"), TextBox).Text.ToString + ",</p>")
        sb.Append("<p>I received your RSVP.  We are looking forward to seeing you on Saturday September 4th.</p>")
                
        Dim msg As New MailMessage("me@work.com", SendToAddress, "Labour Day Weekend BBQ", sb.ToString)
        msg.IsBodyHtml = True
 
        sc.Send(msg)
        msg.Dispose()        
    End Sub

Open in new window

0
guru_samiCommented:
One way to troubleshoot would be to set breakpoints in your code to see when exactly it is firing twice.
Share you .aspx markup code as well.
0
cgcmqAuthor Commented:
Here is the markup
<form ID="form1" runat="server">
			    
			    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:SARtechPrimaryCS %>" 
                    SelectCommand="pBBQ_RSVP_SelectRecord"
                    SelectCommandType="StoredProcedure"
                    InsertCommand="pBBQInsertNewRecord" 
                    InsertCommandType="StoredProcedure" 
                    UpdateCommand="pBBQUpdateRecord" 
                    UpdateCommandType="StoredProcedure" 
                    OnInserted="NewRecordDataSource_Inserted" 
                    OnUpdated="RecordUpdated">
                    <SelectParameters>
                        <asp:Parameter Name="ID" Type="Int32" />
                    </SelectParameters>
                    
                    <UpdateParameters>
                        <asp:Parameter Name="ID" Type="Int32" />
                        <asp:Parameter Name="firstname" Type="String" />
                        <asp:Parameter Name="lastname" Type="String" />
                        <asp:Parameter Name="email" Type="String" />
                        <asp:Parameter Name="PartySize" Type="Int32" />
                        <asp:Parameter Name="OverNighting" Type="Boolean" />
                        <asp:Parameter Name="SleepingArrangement" Type="Int32" />
                        <asp:Parameter Name="Notes" Type="String" />
                        <asp:Parameter Name="Potluck" Type="String" />
                    </UpdateParameters>
                    
                    <InsertParameters>
                        <asp:Parameter Name="firstname" Type="String" />
                        <asp:Parameter Name="lastname" Type="String" />
                        <asp:Parameter Name="email" Type="String" />
                        <asp:Parameter Name="PartySize" Type="Int32" />
                        <asp:Parameter Name="OverNighting" Type="Boolean" />
                        <asp:Parameter Name="SleepingArrangement" Type="Int32" DefaultValue="0" />
                        <asp:Parameter Name="Notes" Type="String" />
                        <asp:Parameter Name="Potluck" Type="String" />                
                        <asp:Parameter Name="NewID" Type="Int32" Direction="Output" />
                    </InsertParameters>
                    
                </asp:SqlDataSource>
                
                <asp:Panel ID="HasBeenAdded" runat="server" Visible="false" CssClass="HighLight">
                    <h3 class="red">Thank you for your RSVP.</h3>
                </asp:Panel>

                <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"  
                    DefaultMode="Insert" CssClass="RSVPForm" 
                    DataSourceID="SqlDataSource1">
                
                    <InsertItemTemplate>
                        <div class="FormView">
                            <label>First Name<span class="red">*</span></label><asp:textbox ID="firstname" runat="server" Text='<%# Bind("firstname") %>' Width="200" MaxLength="40" /><br />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your first name" ControlToValidate="firstname" />
                        </div>
                        
                        <div class="FormView">
                            <label>Last Name<span class="red">*</span></label><asp:textbox ID="lastname" runat="server" Text='<%# Bind("lastname") %>' Width="200" MaxLength="40" /><br />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your last name" ControlToValidate="lastname" />                            
                        </div>
                        
                        <div class="FormView">
                            <label>Email<span class="red">*</span></label><asp:textbox ID="email" runat="server" Text='<%# Bind("email") %>' Width="200" MaxLength="45" /><br />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter your email address" ControlToValidate="email" Display="Dynamic" /><br />
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Opps, that does not appear to be a valid email address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="email" />                            
                        </div>
                        
                        <div class="FormView">
                            <label>Including yourself, how many people are in your party?<span class="red">*</span></label>
                            <asp:ListBox ID="PartySize" runat="server" Width="150" SelectedValue='<%# Bind("PartySize") %>'>
                                <asp:ListItem Text="One" Value="1" />
                                <asp:ListItem Text="Two" Value="2" />
                                <asp:ListItem Text="Three" Value="3" />
                                <asp:ListItem Text="Four" Value="4" />
                                <asp:ListItem Text="Five" Value="5" />
                                <asp:ListItem Text="Six or more" Value="6" />
                            </asp:ListBox>
                        </div>
                        
                        <div class="FormView clearfix">
                            <label>Will you be spending the night?<span class="red">*</span></label>
                            <asp:DropDownList ID="OverNighting" runat="server" SelectedValue='<%# Bind("OverNighting") %>' OnSelectedIndexChanged="chgOverNighting" AutoPostBack="true">
                                <asp:ListItem Text="No" Value="False" />
                                <asp:ListItem Text="Yes" Value="True" />
                            </asp:DropDownList>                            
                        </div>
                        
                        <div class="FormView clearfix">
                            <label>If Yes, which do you plan on bringing with you?</label>
                            <asp:DropDownList ID="SleepingArrangement" runat="server" SelectedValue='<%# Bind("SleepingArrangement") %>' Enabled='<%# IIF (Eval("OverNighting") = true, true, false) %>'>
                                <asp:ListItem Text="Tent" Value="1" />
                                <asp:ListItem Text="Trailer" Value="2" />
                                <asp:ListItem Text="RV" Value="3" />
                                <asp:ListItem Text="The floor" Value="4" />
                            </asp:DropDownList>                            
                        </div>

                        <p>&nbsp;</p>
                        <h3 class="red">Potluck salads &amp; desserts</h3> 
                        <p>Let us know if you plan on bringing anything.
                        At right is a list of what others plan on bringing.</p>                       
                        <div class="FormView clearfix"><asp:textbox ID="Potluck" runat="server" Text='<%# Bind("Potluck") %>' Width="400" MaxLength="40" /></div>
                        
                        <p style="margin-top:15px">Is there anything else you want to let us know about?</p>
                        <div class="FormView clearfix"><asp:textbox ID="Notes" runat="server" Text='<%# Bind("Notes") %>' Width="400" MaxLength="400" TextMode="MultiLine" /></div>
                        
                        <div class="FormView">
                            <asp:Button ID="InsertButton" runat="server" Text="RSVP" CausesValidation="True" CommandName="Insert" />&nbsp;&nbsp;
                            <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                        </div>                                                                        
                    </InsertItemTemplate>
                    
                    <EditItemTemplate>
                         <div class="FormView">
                            <label>First Name<span class="red">*</span></label><asp:textbox ID="firstname" runat="server" Text='<%# Bind("firstname") %>' Width="200" MaxLength="40" />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your first name" ControlToValidate="firstname" />
                        </div>
                        
                        <div class="FormView">
                            <label>Last Name<span class="red">*</span></label><asp:textbox ID="lastname" runat="server" Text='<%# Bind("lastname") %>' Width="200" MaxLength="40" />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your last name" ControlToValidate="lastname" />                            
                        </div>
                        
                        <div class="FormView">
                            <label>Email<span class="red">*</span></label><asp:textbox ID="email" runat="server" Text='<%# Bind("email") %>' Width="200" MaxLength="45" />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter your email address" ControlToValidate="email" />
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Opps, that does not appear to be a valid email address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="email" />                            
                        </div>
                        
                        <div class="FormView">
                            <label>Including yourself, how many people are in your party?<span class="red">*</span></label>
                            <asp:ListBox ID="PartySize" runat="server" Width="150" SelectedValue='<%# Bind("PartySize") %>'>
                                <asp:ListItem Text="One" Value="1" />
                                <asp:ListItem Text="Two" Value="2" />
                                <asp:ListItem Text="Three" Value="3" />
                                <asp:ListItem Text="Four" Value="4" />
                                <asp:ListItem Text="Five" Value="5" />
                                <asp:ListItem Text="Six or more" Value="6" />
                            </asp:ListBox>
                        </div>
                        
                        <div class="FormView clearfix">
                            <label>Will you be spending the night?<span class="red">*</span></label>
                            <asp:DropDownList ID="OverNighting" runat="server" SelectedValue='<%# Bind("OverNighting") %>' OnSelectedIndexChanged="chgOverNighting" AutoPostBack="true">
                                <asp:ListItem Text="No" Value="False" />
                                <asp:ListItem Text="Yes" Value="True" />
                            </asp:DropDownList>                            
                        </div>
                        
                        <div class="FormView clearfix">
                            <label>If Yes, which do you plan on bringing with you?</label>
                            <asp:DropDownList ID="SleepingArrangement" runat="server" SelectedValue='<%# Bind("SleepingArrangement") %>' Enabled='<%# IIF (Eval("OverNighting") = true, true, false) %>'>
                                <asp:ListItem Text="Tent" Value="1" />
                                <asp:ListItem Text="Trailer" Value="2" />
                                <asp:ListItem Text="RV" Value="3" />
                                <asp:ListItem Text="The floor" Value="4" />
                            </asp:DropDownList>                            
                        </div>

                        <p>&nbsp;</p>
                        <h3 class="red">Potluck salads &amp; desserts</h3> 
                        <p>Let us know if you plan on bringing anything.
                        At right is a list of what others plan on bringing.</p>                       
                        <div class="FormView clearfix"><asp:textbox ID="Potluck" runat="server" Text='<%# Bind("Potluck") %>' Width="400" MaxLength="40" /></div>
                        
                        <p style="margin-top:15px">Is there anything else you want to let us know about?</p>
                        <div class="FormView clearfix"><asp:textbox ID="Notes" runat="server" Text='<%# Bind("Notes") %>' Width="400" MaxLength="400" TextMode="MultiLine" /></div>
                        
                        <asp:HiddenField ID="ID" runat="server" Value='<%# Bind("ID") %>' />
                        
                        <div class="FormView">
                            <asp:Button ID="EditButton" runat="server" Text="Update" CausesValidation="True" CommandName="Update" />&nbsp;&nbsp;
                            <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                        </div>                   
                    </EditItemTemplate>
		        </asp:FormView>

                    
                <asp:GridView ID="GridView1" runat="server" CssClass="RSVPForm" Width="300" 
                    AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" 
                    BorderStyle="None" BorderWidth="1px" CellPadding="3" 
                    DataSourceID="SqlDataSource2">
                    <RowStyle ForeColor="#000066" />
                    <EmptyDataRowStyle BackColor="#006699" Font-Bold="True" ForeColor="White" 
                        HorizontalAlign="Center" VerticalAlign="Middle" />
                    <Columns>
                        <asp:BoundField DataField="Potluck" HeaderText="Potluck - this is what people are bringing so far" 
                            SortExpression="Potluck" />
                    </Columns>
                    <FooterStyle BackColor="White" ForeColor="#000066" />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                    <EmptyDataTemplate>
                        So far, no one is bringing anything in the way of pot luck.  Feel free to be the first
                    </EmptyDataTemplate>
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                </asp:GridView> 
                
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:SARtechPrimaryCS %>" 
                    SelectCommand="SELECT [Potluck] FROM [BBQ_RSVP] WHERE COALESCE([Potluck],'') <> '' ORDER BY ID"></asp:SqlDataSource>                
			</form>

Open in new window

0
guru_samiCommented:
can you remove this from SDS:
 OnInserted="NewRecordDataSource_Inserted"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cgcmqAuthor Commented:
That did it.  I guess by calling it directly in the SQLDataSource it caused the sub to fire twice.  I am assuming then that it gets fired automatically when a record is submitted.

Cheers
0
cgcmqAuthor Commented:
Cheers
0
guru_samiCommented:
---> I am assuming then that it gets fired automatically when a record is submitted.
Nope, it was being called twice because:
1: OnInserted="NewRecordDataSource_Inserted"
2: Handles SqlDataSource1.Inserted in your code-behind

In VB it behaves kind of differently.

So you might want to remove OnUpdated="RecordUpdated" as well because you have
Handles SqlDataSource1.Updated
for your Updated event handler
0
cgcmqAuthor Commented:
Doop!  Thanks for the background info.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.