?
Solved

FormView OnInserted fires twice

Posted on 2010-08-12
11
Medium Priority
?
841 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:cgcmq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 33421117
make it   AutoEventWireup="false"  in aspx page
0
 

Author Comment

by:cgcmq
ID: 33421239
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 33421310
remove this as well from FV markup: OnInserted="NewRecordDataSource_Inserted"

or share your code
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:cgcmq
ID: 33421357
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 33421418
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
 

Author Comment

by:cgcmq
ID: 33421459
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
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 33421486
can you remove this from SDS:
 OnInserted="NewRecordDataSource_Inserted"
0
 

Author Comment

by:cgcmq
ID: 33421594
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
 

Author Closing Comment

by:cgcmq
ID: 33421608
Cheers
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33421669
---> 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
 

Author Comment

by:cgcmq
ID: 33421694
Doop!  Thanks for the background info.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question