Insert command inserts two rows instead of one.

For some reason, this code inserts two rows from an ASP.Net 2.0 form into a SQL Server 2005 database. I have tried with If Not Page.IsPostBack as well and it doesn't work at all that way. I also tried disabling the form submission function in the form tag itself, no luck. I just can't see what I am doing wrong... any ideas?

Note: the rows contain the exact same data.
'Code behind
Partial Class page
    Inherits System.Web.UI.Page
    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.Title = "Title"
    End Sub
 
    Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
        If Page.IsPostBack Then
            SqlDataSource1.InsertParameters("UserName").DefaultValue = User.Identity.Name
            SqlDataSource1.InsertParameters("Cat1").DefaultValue = Cat1.Text
            SqlDataSource1.InsertParameters("Cat2").DefaultValue = Cat2.Text
            SqlDataSource1.InsertParameters("Cat3").DefaultValue = Cat3.Text
            SqlDataSource1.InsertParameters("Title").DefaultValue = Title.Text
            SqlDataSource1.InsertParameters("Body").DefaultValue = Body.Text
            SqlDataSource1.Insert()
            ThisMsg.Text = User.Identity.Name
        End If
    End Sub
End Class
'---------------------------------------------------------------------
'Form Page
<%@ Page Language="VB" MasterPageFile="~/Master.master" AutoEventWireup="false" CodeFile="page.aspx.vb" Inherits="page" %>
<%@ Register TagPrefix="UserControl" TagName="DashBoardControl" Src="~/DashControl.ascx" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="BodyContent" Runat="Server">
 
<UserControl:DashBoardControl runat="server" />
 
<table border="1" style="width: 100%;">
<tr><td rowspan="2" style="width: 200px; height: 200px;">Image</td>
    <td style="width: 200px;"><asp:Label ID="UserNameLabel" runat="server" Text="UserName" /></td>
    <td style="width: 200px;"><asp:Label ID="EmailLabel" runat="server" Text="Email" /></td>
    <td style="width: 200px;"><asp:Label ID="DateLabel" runat="server" Text="Date" /></td>
    <td style="width: auto;">Flag</td></tr>
<tr><td colspan="4">
    <asp:Label ID="SelectCatLabel" runat="server" Text="Select at least one category." /><br />
    <asp:Label ID="thisMsg" runat="server" /><br />
    <asp:DropDownList ID="Cat1" runat="server">
    <asp:ListItem Value="" Text="Select One" Selected="True" />
    <asp:ListItem Value="Salvation" Text="Salvation" />
    </asp:DropDownList>
    <asp:DropDownList ID="Cat2" runat="server">
    <asp:ListItem Value="" Text="Select One" Selected="True" />
    <asp:ListItem Value="Salvation" Text="Salvation" />
    </asp:DropDownList>
    <asp:DropDownList ID="Cat3" runat="server">
    <asp:ListItem Value="" Text="Select One" Selected="True" />
    <asp:ListItem Value="Salvation" Text="Salvation" />
    </asp:DropDownList>
</td></tr>
<tr><td colspan="4"><asp:TextBox ID="Title" runat="server" TextMode="SingleLine" Columns="75" /></td>
    <td style="width: auto;"><asp:Button ID="submit" runat="server" Text="Save" OnClick="Submit_Click" UseSubmitBehavior="false" /></td></tr>
<tr><td colspan="5"><asp:TextBox ID="Body" runat="server" TextMode="MultiLine" Columns="100" Rows="25" /></td></tr>
</table>
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database %>"
            InsertCommand="INSERT INTO [Posts] ([UserName], [Cat1], [Cat2], [Cat3], [Title], [Body]) VALUES (@UserName, @Cat1, @Cat2, @Cat3, @Title, @Body)">
            <InsertParameters>
                <asp:Parameter Name="UserName" Type="String" />
                <asp:Parameter Name="Cat1" Type="String" />
                <asp:Parameter Name="Cat2" Type="String" />
                <asp:Parameter Name="Cat3" Type="String" />
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Body" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
</asp:Content>

Open in new window

LVL 3
xizwyckAsked:
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.

digitalZoCommented:
Do it this way:

Can you post the source code of the Submit button too?


   Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.Title = "Title"
 
        If Page.IsPostBack Then
        Call Submit_Click(Submit, Nothing)
        End If
    End Sub
 
    Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
       
            SqlDataSource1.InsertParameters("UserName").DefaultValue = User.Identity.Name
            SqlDataSource1.InsertParameters("Cat1").DefaultValue = Cat1.Text
            SqlDataSource1.InsertParameters("Cat2").DefaultValue = Cat2.Text
            SqlDataSource1.InsertParameters("Cat3").DefaultValue = Cat3.Text
            SqlDataSource1.InsertParameters("Title").DefaultValue = Title.Text
            SqlDataSource1.InsertParameters("Body").DefaultValue = Body.Text
            SqlDataSource1.Insert()
            ThisMsg.Text = User.Identity.Name
 End Sub

Open in new window

0
NazoUKCommented:
The problem is that you have an onclick="Submit_Click" in your button tag and a handles clause against your button handler which will cause it to fire twice. Use one or the other but not both together.
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
xizwyckAuthor Commented:
digitalZo: I have no idea if that would work or not but I included all of the applicable code.

Thank you NazoUK! You saved me a lot of time and helped me learn more about ASP.Net.

I guess I've been confused by the use of these functions and got them combined.

For the readers edification, I put the solution down below. Basically, I removed: "OnClick="Submit_Click" from the button tag.


'Code behind
Partial Class page
    Inherits System.Web.UI.Page
    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.Title = "Title"
    End Sub
 
    Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
        If Page.IsPostBack Then
            SqlDataSource1.InsertParameters("UserName").DefaultValue = User.Identity.Name
            SqlDataSource1.InsertParameters("Cat1").DefaultValue = Cat1.Text
            SqlDataSource1.InsertParameters("Cat2").DefaultValue = Cat2.Text
            SqlDataSource1.InsertParameters("Cat3").DefaultValue = Cat3.Text
            SqlDataSource1.InsertParameters("Title").DefaultValue = Title.Text
            SqlDataSource1.InsertParameters("Body").DefaultValue = Body.Text
            SqlDataSource1.Insert()
            ThisMsg.Text = User.Identity.Name
        End If
    End Sub
End Class
'---------------------------------------------------------------------
'Form Page
<%@ Page Language="VB" MasterPageFile="~/Master.master" AutoEventWireup="false" CodeFile="page.aspx.vb" Inherits="page" %>
<%@ Register TagPrefix="UserControl" TagName="DashBoardControl" Src="~/DashControl.ascx" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="BodyContent" Runat="Server">
 
<UserControl:DashBoardControl runat="server" />
 
<table border="1" style="width: 100%;">
<tr><td rowspan="2" style="width: 200px; height: 200px;">Image</td>
    <td style="width: 200px;"><asp:Label ID="UserNameLabel" runat="server" Text="UserName" /></td>
    <td style="width: 200px;"><asp:Label ID="EmailLabel" runat="server" Text="Email" /></td>
    <td style="width: 200px;"><asp:Label ID="DateLabel" runat="server" Text="Date" /></td>
    <td style="width: auto;">Flag</td></tr>
<tr><td colspan="4">
    <asp:Label ID="SelectCatLabel" runat="server" Text="Select at least one category." /><br />
    <asp:Label ID="thisMsg" runat="server" /><br />
    <asp:DropDownList ID="Cat1" runat="server">
    <asp:ListItem Value="" Text="Select One" Selected="True" />
    <asp:ListItem Value="Salvation" Text="Salvation" />
    </asp:DropDownList>
    <asp:DropDownList ID="Cat2" runat="server">
    <asp:ListItem Value="" Text="Select One" Selected="True" />
    <asp:ListItem Value="Salvation" Text="Salvation" />
    </asp:DropDownList>
    <asp:DropDownList ID="Cat3" runat="server">
    <asp:ListItem Value="" Text="Select One" Selected="True" />
    <asp:ListItem Value="Salvation" Text="Salvation" />
    </asp:DropDownList>
</td></tr>
<tr><td colspan="4"><asp:TextBox ID="Title" runat="server" TextMode="SingleLine" Columns="75" /></td>
    <td style="width: auto;"><asp:Button ID="submit" runat="server" Text="Save" /></td></tr>
<tr><td colspan="5"><asp:TextBox ID="Body" runat="server" TextMode="MultiLine" Columns="100" Rows="25" /></td></tr>
</table>
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database %>"
            InsertCommand="INSERT INTO [Posts] ([UserName], [Cat1], [Cat2], [Cat3], [Title], [Body]) VALUES (@UserName, @Cat1, @Cat2, @Cat3, @Title, @Body)">
            <InsertParameters>
                <asp:Parameter Name="UserName" Type="String" />
                <asp:Parameter Name="Cat1" Type="String" />
                <asp:Parameter Name="Cat2" Type="String" />
                <asp:Parameter Name="Cat3" Type="String" />
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Body" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
</asp:Content>

Open in new window

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
ASP.NET

From novice to tech pro — start learning today.