click event handler executed twice

D

dsvick

Hi everyone:

I got simple aspx with a user control in it that posts some entered
data to a database. The problem I'm seeing is that when the submit
button is clicked the code is executed twice and I get duplicate rows
in the database.

The including aspx page is this:

<%@ Page language="c#" Codebehind="test.aspx.cs"
AutoEventWireup="false" Inherits="DirectedInsight.test" %>
<%@ Register TagPrefix="uc1" TagName="comments"
Src="includes/comments.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>test</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET
7.1">
<meta name="CODE_LANGUAGE" content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<uc1:comments id="Comments1" runat="server"></uc1:comments>
</form>
</body>
</html>

The user control looks like this:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="comments.ascx.cs"
Inherits="DirectedInsight.includes.comments"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table class="normalText">
<tr>
<td>Your name:
</td>
<td><asp:textbox id="submitName"
runat="server"></asp:textbox></td>
<td>Your email:
</td>
<td><asp:textbox id="submitEmail"
runat="server"></asp:textbox></td>
</tr>
<tr>
<td>Comment:
</td>
<td colspan="3"><asp:textbox id="comment" runat="server"
textmode="MultiLine" rows="6" columns="50"></asp:textbox></td>
</tr>
<tr>
<td align="center" colspan="4">
<asp:button id="Button1" onclick="submitComment"
runat="server" cssclass="commonButton" text="Submit
Comment"></asp:button>
</td>
</tr>
</table>

And the event handler is this:

public void submitComment(object sender, System.EventArgs e) {
string connStr =
"SERVER=SQLServer;UID=USER;PWD=Password;DATABASE=Database;";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insertComment";
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter param =
cmd.Parameters.Add("@articleID",SqlDbType.Int, 4);
param.Direction = ParameterDirection.Input;
param.Value = cArticleID;

param = cmd.Parameters.Add("@submitName",SqlDbType.VarChar,
30);
param.Direction = ParameterDirection.Input;
param.Value = submitName.Text;

param =
cmd.Parameters.Add("@submitEmail",SqlDbType.VarChar, 100);
param.Direction = ParameterDirection.Input;
param.Value = submitEmail.Text;

param = cmd.Parameters.Add("@comment",SqlDbType.Text,
1000);
param.Direction = ParameterDirection.Input;
param.Value = comment.Text;

cmd.ExecuteNonQuery();
}

The stored procedure is single insert statement.

Everytime I click the submit button I end up with two identical rows in
the database. I've googled and can find nothing similar on it. Any help
would be greatly appreciated!!

thanks in advance

Dave
 
M

Marina

Is your event handler attached twice? You are not showing how it is
attached.

Also, shouldn't there be a primary key on your table to make it impossible
for 2 identical rows to end up in the database?
 
W

W.G. Ryan - MVP

Hi everyone:

I got simple aspx with a user control in it that posts some entered
data to a database. The problem I'm seeing is that when the submit
button is clicked the code is executed twice and I get duplicate rows
in the database.
--There are multiple problems here if I understand you correctly, one is
that you have a UI issue, allowing the user to click twice, the other is
that the db is allowing it. Obviously you may have some complexities I'm
unaware of, but a key should stop the duplicate values from being inserted.
 
D

dsvick

Sorry for not explaining further. I did not include a primary key in
the table, there is a timedate field that I use for sorting, but I did
not make it a key. I can certainly do that but I'd still like to know
why it is happening. It is still in development so I've not included
any restraints on multiple clicks, for now I'm the only one testing and
I know for sure I've not clicked it twice.

As for the event handler getting wired, it is only being done once.
There is nothing on the including aspx page for it. The complete code
for the control is:

namespace DirectedInsight.includes
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for comments.
/// </summary>
public class comments : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox submitName;
protected System.Web.UI.WebControls.TextBox submitEmail;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox comment;

public int cArticleID;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.submitComment);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

public int articleID {
get {return cArticleID;}
set {cArticleID = value;}
}

public void submitComment(object sender, System.EventArgs e) {
string connStr =
"SERVER=SQLserver;UID=userName;PWD=password;DATABASE=DatabaseName;";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insertComment";
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter param =
cmd.Parameters.Add("@articleID",SqlDbType.Int, 4);
param.Direction = ParameterDirection.Input;
param.Value = cArticleID;

param = cmd.Parameters.Add("@submitName",SqlDbType.VarChar,
30);
param.Direction = ParameterDirection.Input;
param.Value = submitName.Text;

param =
cmd.Parameters.Add("@submitEmail",SqlDbType.VarChar, 100);
param.Direction = ParameterDirection.Input;
param.Value = submitEmail.Text;

param = cmd.Parameters.Add("@comment",SqlDbType.Text,
1000);
param.Direction = ParameterDirection.Input;
param.Value = comment.Text;

cmd.ExecuteNonQuery();
}
}
}
 
D

dsvick

One other thing, the datetime field value is generated in the stored
procedure with getDate(). All of the duplicate rows have the exact same
value.
 
W

W.G. Ryan eMVP

DSVick:

I'm sorry for not getting back to you sooner - got stuck with something at
work. Lemme take a look at it now with the additional knowledge of your
last post and see what I can come up with.
 
D

dsvick

Thanks for you time and help on this. I'm sure it is something I've
done wrong or not understood in the whole process. I appreciate your
help.
 
D

dsvick

Could this problem have anything to do with:

AutoEventWireup="false" on the including page.

I'm sort of at an impass here and the more I try different things the
more confused I get :)

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top