Thursday 4 April 2013

Simple Signup Form in Asp.net Using C#


Simple Signup Form in Asp.net Using C#
First you needed to create the table for registration in your sql server. here is he code for you sql table
/****** Object:  Table [dbo].[UserRegistration]    Script Date: 01/16/2013 23:47:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[UserRegistration](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [UserId] [varchar](50) NULL,
    [Password] [varchar](50) NULL,
    [Name] [varchar](50) NULL,
    [Address] [varchar](50) NULL,
    [ContactNo] [int] NULL,
 CONSTRAINT [PK_UserRegistration] PRIMARY KEY CLUSTERED
 
(
    [Id] 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

In design mode your table will look as shown below.

sql table

you will not get any data in your table.

sql table

Now you create a new asp.net application. add a .aspx page in it.
Now in this page add the below html code by crating the click event of the button,
<p><h2>
        <strong>Simple Sinup Form in Asp.net Using C#&nbsp;&nbsp;&nbsp;</h2></p>
    <p>
        &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
        User Id :<asp:TextBox ID="txtuserid" runat="server" Width="150px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
 
            CssClass="style1" ErrorMessage="Please enter user id"
 
            ControlToValidate="txtuserid" Display="Dynamic"></asp:RequiredFieldValidator>
    </p>
    <p>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
        Password :<asp:TextBox ID="txtpassword"
 
            runat="server" Width="150px" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
 
            CssClass="style1" ErrorMessage="Please enter password"
 
            ControlToValidate="txtpassword" Display="Dynamic"></asp:RequiredFieldValidator>
    </p>
    <p>
        Con. Password :<asp:TextBox ID="txtconformpassword" runat="server" Width="150px"
 
            TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
 
            CssClass="style1" ErrorMessage="Please enter confirm password."
 
            ControlToValidate="txtconformpassword" Display="Dynamic"></asp:RequiredFieldValidator>
        <asp:CompareValidator ID="CompareValidator1" runat="server"
 
            ControlToCompare="txtpassword" ControlToValidate="txtconformpassword"
 
            Display="Dynamic" ErrorMessage="Password and confirm password are not same."
 
            style="color: #FF0000"></asp:CompareValidator>
    </p>
    <p>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         Name :<asp:TextBox ID="txtname" runat="server" Width="150px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
 
            CssClass="style1" ErrorMessage="Please enter name."
 
            ControlToValidate="txtname" Display="Dynamic"></asp:RequiredFieldValidator>
    </p>
    <p>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
        Address:<asp:TextBox ID="txtaddress" runat="server" Width="150px"></asp:TextBox>
    </p>
    <p>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        Contact No:<asp:TextBox ID="txtcontactno" runat="server" Width="150px"></asp:TextBox>
    </p>
    <p>
        <asp:Label ID="lblmessage" runat="server" Style="color: #FFFFFF; background-color: #FF0000"
            Text=""></asp:Label>
    </p>
    <p>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" Width="103px" />
    </p>


In this i have added validation controls. As you run the page your page will look as shown below.

REGISTRATION FOR IN ASP.NET

Now click on button validation will occur. it will not allow you to make the post.

REGISTRATION FOR IN ASP.NET

In your web.config add the below connection string setting.
<connectionStrings>
    <add name="ConnectionString"
        connectionString="data source=SQLEXPRESS;database=DemoArticles;uid=so;pwd=123;"
        providerName="System.Data.SqlClient" />
  </connectionStrings>

Now come to you .cs page and add the below code on button click event.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection objcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
                DataTable objdatatable = new DataTable();
                string validationquery = "select * from UserRegistration where UserId=`" + txtuserid.Text + "`";
                SqlDataAdapter objdavalidate = new SqlDataAdapter(validationquery, objcon);
                objdavalidate.SelectCommand.CommandType = CommandType.Text;
                objdavalidate.Fill(objdatatable);
                if (objdatatable.Rows.Count > 0)
                {
                    lblmessage.Text = "User Id already exists.";
                    txtuserid.Text = "";
                }
                else
                {
                    try
                    {
                        string insertquery = "insert into UserRegistration(UserId,Password,Name,Address,ContactNo) values(`" + txtuserid.Text + "`,`" + txtpassword.Text + "`,`" + txtname.Text + "`,`" + txtaddress.Text + "`,`" + txtcontactno.Text + "`)";
                        SqlDataAdapter objda = new SqlDataAdapter(insertquery, objcon);
                        objda.SelectCommand.CommandType = CommandType.Text;
                        objcon.Open();
                        objda.SelectCommand.ExecuteNonQuery();
                        objcon.Close();
                        lblmessage.Text = "Data saved successfully.";
                    }
                    catch
                    {
                        lblmessage.Text = "Error while creating user id.";
                    }
                }
            }
            catch
            {
                lblmessage.Text = "Error while saving the record.";
            }
        }
    }
}


In above code first i have validated if user id already exist or not if not then save the user record. other wise it will throw the error message.

Now we have done our coding run the page. run the page after adding all details click on save button;

REGISTRATION FOR IN ASP.NET

if your password and confirm password will different then you will get the below error message.

REGISTRATION FOR IN ASP.NET

Now in you .cs file first we will check weather user id exist in system or not if not then you will not get any record.

REGISTRATION FOR IN ASP.NET

Now data will save and you will be able to see it in your sql table.

REGISTRATION FOR IN ASP.NET
now again try to register with same user id. you will get the error message that user id will appear. here i have shown that if user already exists on that case you will record in your collection.

REGISTRATION FOR IN ASP.NET

now final o/p

REGISTRATION FOR IN ASP.NET

This will prevent to create duplicate user id in your system.

_______________________________

DOWNLOAD
_______________________________





No comments:

Post a Comment