Monday, August 12, 2019

User registeration in asp.net c#

Create data table in sql server by the following query

create table tblUser(
id int identity(1,1) primary key,
u_Name nvarchar(200),
u_Password nvarchar(200),
u_Email nvarchar(200)

Now create the store procedure for register the user and check the user name already exists.

create proc SP_Register
@u_Name nvarchar(200),
@u_Password nvarchar(200),
@u_Email nvarchar(200)
as 
begin 
Declare @Count int 
Declare @ReturnCode int
select @Count=COUNT(u_Name)
from tblUser
where u_Name=@u_Name
if @Count>0
begin
set @ReturnCode=-1
end
else
begin
set @ReturnCode=1
insert into tblUser values(
@u_Name,@u_Password,@u_Email)
end
select @ReturnCode as ReturnCode
end

Add the connection string into web.config file
    
 <appSettings>
        <add key="ConnectionString" value="Data Source=LAPTOP-ISNBQ76D;Initial Catalog=tutorial;Integrated Security=True"/>
  </appSettings>

Now add the Register.aspx web page for register the user and add the following code.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div>
            <table>
                <tr>
                    <td>User Name:</td>
                    <td><input type="text" id="txtUserName" runat="server" /> </td>
                </tr>
                 <tr>
                    <td>Password:</td>
                    <td><input type="password" id="txtPassword" runat="server" /> </td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type="email" id="txtEmail" runat="server" /> </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblError" runat="server"></asp:Label>
                    </td>
                </tr>
                 <tr>
                    <td><asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" /></td>
                    <td><asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td>
                </tr>
            </table>
        </div>
        </div>
    </form>
</body>
</html>

Now add the following code into the Register.aspx.cs file 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace authenticate
{
    public partial class Register : System.Web.UI.Page
    {
        public static string StringCon = null;
        SqlConnection conn = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            StringCon = ConfigurationSettings.AppSettings["ConnectionString"];
            conn = new SqlConnection(StringCon);
        }
        protected void btnRegister_Click(object sender,EventArgs e)
        {
            try
            {
                string sql = "SP_Register";
                SqlCommand cmd = new SqlCommand(sql,conn);
                string encriptedPass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Value, "SHA1");
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@u_Name",txtUserName.Value);
                cmd.Parameters.AddWithValue("@u_Password", encriptedPass);
                cmd.Parameters.AddWithValue("@u_Email", txtEmail.Value);
                conn.Open();
                int returnValue = (int)cmd.ExecuteScalar();
                if (returnValue == -1)
                {
                    lblError.Text = "User Name Already Exist! Please Try Another One!";
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
            catch(Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
        }
    }
}




Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...