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
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