duplicate entry in table for single user registration. [Resolved]

Posted by Sharmagoldi781 under C# on 11/28/2013 | Points: 10 | Views : 1180 | Status : [Member] | Replies : 5
.cs Page code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=WSERVER\\SQLL2005;Initial Catalog=asd;User ID=sa;Password=vivek");
con.Open();
string str = ("insert into [deta](u_name,password,mobile_no,email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')");
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
MessageBox.Show(" Successfully registered");
con.Close();
}
}
Problem is that when i am running this code compiler is executing Button1_Click1 code twice as a result two entries are found everytime in the table for a single user registered.
why it is happening..
please reply





Responses

Posted by: Bandi on: 11/29/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Refer
http://myworkcodes.wordpress.com/2011/04/18/button-click-event-firing-twice/

Set AUTOEVENTWIreup = false

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharmagoldi781 on: 11/28/2013 [Member] Starter | Points: 25

Up
0
Down
this is the .aspx page source code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="newuser.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>

</head>
<body>
<form id="form1" runat="server">
<div style="height:396px">
<table style="width: 1068px">
<tr>
<td colspan="2" style="height: 21px">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<strong><span class="style13">Registration Page</span> </strong>
</td>
</tr>
<tr>
<td colspan="2">
&nbsp;</td>
</tr>


<tr>
<td align="Left">
<strong>User Name</strong></td>
<td style="width: 698px" >
<asp:TextBox ID="TextBox1" runat="server" Width="195px"></asp:TextBox>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="user name should not be Blank"
Font-Bold="True" Font-Size="Medium" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="Left">
<strong>Password</strong></td>
<td style="width: 698px" >
<asp:TextBox ID="TextBox2" runat="server" Width="195px" TextMode="Password" OnTextChanged="Button1_Click1"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="password should not be Blank"
Font-Bold="True" Font-Size="Medium" ForeColor="#FF3300"></asp:RequiredFieldValidator>
<asp:Label ID="Label2" runat="server" Text="error" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td align="Left">
<strong>ReType Password</strong></td>
<td style="width: 698px" >
<asp:TextBox ID="TextBox3" runat="server" Width="195px" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox2" ControlToValidate="TextBox3"
ErrorMessage="Enter correct password" Font-Bold="True" Font-Size="Medium"
ForeColor="#FF3300"></asp:CompareValidator>
</td>
</tr>
<tr>
<td align="Left">
<strong>Mobile Number</strong>r</td>
<td style="width: 698px">
<asp:TextBox ID="TextBox4" runat="server" MaxLength="10" Width="195px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Enter Valid Number"
Font-Bold="True" Font-Size="Medium" ForeColor="Red"
ValidationExpression="[7-9][0-9]{9}$"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Mobile Number can not be Blank"
Font-Bold="True"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<strong>Email Id</strong></td>
<td style="width: 698px">
<asp:TextBox ID="TextBox5" runat="server" Width="195px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox5" ErrorMessage="Enter valid Email Id"
Font-Bold="True" Font-Size="Medium" ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox5" ErrorMessage="Email Field can not be Blank"
Font-Bold="True"></asp:RequiredFieldValidator>
</td>
</tr>

<tr>
<td style="height: 33px">
&nbsp;</td>
<td style="width: 698px; height: 33px;">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
</td>
</tr>

</table>

</div>


</fo

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html
http://geekswithblogs.net/TimH/archive/2006/10/23/94874.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharmagoldi781 on: 11/29/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks A lot
its working all right now.will disturb you soon
:)

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
you most welcome :)

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response