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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
NazoUKConnect With a Mentor Commented:
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
 
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
 
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
All Courses

From novice to tech pro — start learning today.