KRayudu V

KRayudu V

  • NA
  • 155
  • 205.4k

duplicate record is inserted into the table

Dec 25 2012 12:11 AM
ASPX Page:

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

<!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>Registration</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 134px;
        }
        .style3
        {
            width: 167px;
        }
        .style4
        {
            width: 534px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td class="style2">
                <asp:Label ID="lblregid" runat="server" Text="RegistrationID"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtregid" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="reqid" runat="server" ErrorMessage="RegistrationID Can't be Blank"
                    ControlToValidate="txtregid" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblFirstName" runat="server" Text="FirstName"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="reqfs" runat="server" ErrorMessage="FirstName Can't be Blank"
                    ControlToValidate="txtfirstname" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblLastName" runat="server" Text="LastName"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtLastname" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="reqln" runat="server" ErrorMessage="LastName Can't be Blank"
                    ControlToValidate="txtLastname" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblUserName" runat="server" Text="UserName"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="requn" runat="server" ErrorMessage="UserName Can't be Blank"
                    ControlToValidate="txtusername" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblpasswd" runat="server" Text="Password"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="reqpwd" runat="server" ErrorMessage="Password Can't be Blank"
                    ControlToValidate="txtpwd" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblconformpwd" runat="server" Text="ConfirmPassword"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtcpwd" runat="server" TextMode="Password"></asp:TextBox>
            </td>
            <td>
                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtcpwd"
                    SetFocusOnError="true" ControlToValidate="txtpwd" ErrorMessage="Password and confirm password do not match"></asp:CompareValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblemailid" runat="server" Text="EmailID"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RegularExpressionValidator ID="regEmail" ControlToValidate="txtemailid" Text="(Invalid email)"
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" runat="server" />
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblAddress" runat="server" Text="Address"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtaddress" Height="22px" Width="128px" runat="server" TextMode="MultiLine"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="reqAddress" runat="server" ErrorMessage="Address Can't be Blank"
                    ControlToValidate="txtaddress" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblgender" runat="server" Text="Grender"></asp:Label>
            </td>
            <td class="style3">
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"
                    i>
                    <asp:ListItem Value="M">Male</asp:ListItem>
                    <asp:ListItem Value="F">Female</asp:ListItem>
                </asp:RadioButtonList>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="reqgender" runat="server" ErrorMessage="Gender Can't be Blank"
                    ControlToValidate="RadioButtonList1" SetFocusOnError="true" Text="*"></asp:RequiredFieldValidator>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Button ID="btnSignUp" runat="server" CausesValidation="false" Text="SignUp"
                    OnClick="btnSignUp_Click" />
            </td>
            <td class="style3">
                &nbsp;
                <asp:Label ID="lblresult" runat="server" ForeColor="Red"></asp:Label>
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
    <table>
        <tr>
            <td class="style4">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" RowStyle-BackColor="#A1DCF2"
                    HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" Width="610px">
                    <Columns>
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                        <asp:BoundField DataField="LastName" HeaderText="LastName" />
                        <asp:BoundField DataField="UserName" HeaderText="UserName" />
                        <asp:BoundField DataField="Password" HeaderText="Password" />
                        <asp:BoundField DataField="EmailId" HeaderText="EmailId" />
                        <asp:BoundField DataField="Address" HeaderText="Address" />
                        <asp:BoundField DataField="Gender" HeaderText="Gender" />
                        <asp:BoundField DataField="ConfirmPassword" HeaderText="ConfirmPassword" />
                        <asp:BoundField DataField="RegistrationID" HeaderText="RegistrationID" />
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


Code-Behind
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.Visible = false;
            BindData();
            Clear();
           
        }
    }
    protected void Clear()
    {
        txtregid.Text = "";
        txtfirstname.Text = "";
        txtLastname.Text = "";
        txtusername.Text = "";
        txtaddress.Text = "";
        txtemailid.Text = "";
        RadioButtonList1.SelectedIndex = -1;
    }
    protected void BindData()
    {
        using (SqlConnection con = new SqlConnection(DataBaseHelper.Connectionstring))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select FirstName,LastName,UserName,Password,EmailId,Address,Gender,ConfirmPassword,RegistrationID from Regestration", con);
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            con.Close();
        }
    }
    protected void btnSignUp_Click(object sender, EventArgs e)
    {
        string strinsert = "insert into Regestration (FirstName,LastName,UserName,Password,EmailId,Address,Gender,ConfirmPassword,RegistrationID)"
            + "values(@fn,@ln,@un,@pwd,@emailid,@address,@gender,@cpwd,@registrationid)";
        SqlConnection con = new SqlConnection(DataBaseHelper.Connectionstring);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strinsert;

        SqlParameter parafn = new SqlParameter("@fn", SqlDbType.VarChar, 50);
        SqlParameter paraln = new SqlParameter("@ln", SqlDbType.VarChar, 50);
        SqlParameter paraun = new SqlParameter("@un", SqlDbType.VarChar, 50);
        SqlParameter parapwd = new SqlParameter("@pwd", SqlDbType.VarChar, 50);
        SqlParameter paraemailid = new SqlParameter("@emailid", SqlDbType.VarChar, 50);
        SqlParameter paraaddress = new SqlParameter("@address", SqlDbType.VarChar, 50);
        SqlParameter paragender = new SqlParameter("@gender", SqlDbType.VarChar, 50);
        SqlParameter paracpwd = new SqlParameter("@cpwd", SqlDbType.VarChar, 50);
        SqlParameter pararegisid = new SqlParameter("@registrationid", SqlDbType.Int);
        cmd.Parameters.Add(parafn);
        cmd.Parameters.Add(paraln);
        cmd.Parameters.Add(paraun);
        cmd.Parameters.Add(parapwd);
        cmd.Parameters.Add(paraemailid);
        cmd.Parameters.Add(paraaddress);
        cmd.Parameters.Add(paragender);
        cmd.Parameters.Add(paracpwd);
        cmd.Parameters.Add(pararegisid);

        parafn.Value = txtfirstname.Text;
        paraln.Value = txtLastname.Text;
        paraun.Value = txtusername.Text;
        parapwd.Value = txtpwd.Text;
        paraemailid.Value = txtemailid.Text;
        paraaddress.Value = txtaddress.Text;
        paragender.Value = RadioButtonList1.SelectedItem.ToString();
        paracpwd.Value = txtcpwd.Text;
        pararegisid.Value =Convert.ToInt32( txtregid.Text);

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
         
            lblresult.Text = "Insert Sucess";
        }
        catch (SqlException ex)
        {
            string str = "Error in Registration Page";
            str += ex.Message;
            throw new Exception(str);
        }
        finally
        {
           
            con.Close();
          //  Response.Redirect(Request.Url.ToString(), false);
        }
        GridView1.Visible = true;
        BindData();
        Clear();
      

      
    }
   
}

DataBase:

USE [RegestrationUser]
GO

/****** Object:  Table [dbo].[Regestration]    Script Date: 12/25/2012 10:39:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Regestration](
    [FirstName] [varchar](50) NULL,
    [LastName] [varchar](50) NULL,
    [UserName] [varchar](50) NULL,
    [Password] [varchar](50) NULL,
    [EmailId] [varchar](50) NULL,
    [Address] [varchar](50) NULL,
    [Gender] [varchar](50) NULL,
    [ConfirmPassword] [varchar](50) NULL,
    [RegistrationID] [int] NOT NULL,
    [RegisID] [int] IDENTITY(1,1) NOT NULL,
PRIMARY KEY CLUSTERED
(
    [RegistrationID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
    [RegisID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


Error:
I have a C# ASP.NET page that submits back to itself to insert details
from a form into a database table. When / if the user refreshes the
page (and gets the standard warning that POST data will be
resubmitted), the previously submitted record is sumbitted again, and a
duplicate record is inserted into the table.




Answers (1)