In this article I will explain how to design a
registration form and how to save registration form information Database using
C#.
In registration form user needs to enter fields Username,
Password, Confirm Password, First Name, Last Name, Email, Location, Phone no.
These fields information need to save in Database. I am using stored procedure
for data saving purpose.
We need to create a
table in Database. Below is sample table structure.
Stored Procedure:
CREATE PROCEDURE SaveUserInformation
@UserName nvarchar(50),
@Password nvarchar(50),
@FirstName nvarchar(50),
@LastName nvarchar(50),
@Email nvarchar(50),
@PhoneNo nvarchar(50),
@City nvarchar(50)
AS
BEGIN
insert into registration values (@UserName ,
@Password ,
@FirstName ,
@LastName ,
@Email ,
@PhoneNo ,
@City )
select SCOPE_IDENTITY()'UserId'
END
GO
First we need to establish a connection in
Web.config file.
<connectionStrings>
<add name="connectionString" connectionString="Data Source=LocalHost;uid=sa1;password=Con@123;Initial Catalog=practice"/>
</connectionStrings>
Below is the page
design.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Registration Form</title>
</head>
<body>
<form id="form1" runat="server">
<table align="center">
<tr>
<td align="center" colspan="2">
<b style="color:maroon;font-size:large;">Registration Form</b>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lbluser" runat="server" Text="Username"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblpwd" runat="server" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblcnfmpwd" runat="server" Text="Confirm Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtcnmpwd" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator runat="server" ControlToCompare="txtpwd" ControlToValidate="txtcnmpwd" ID="cvCompare" ErrorMessage="Confirm Password
mismatch"></asp:CompareValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblfname" runat="server" Text="FirstName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lbllname" runat="server" Text="LastName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblemail" runat="server" Text="Email"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblCnt" runat="server" Text="Phone No"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lbladd" runat="server" Text="City"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnsubmit" runat="server" Text="Save"
OnClick="btnsubmit_Click" />
<asp:Button runat="server" ID="btnCancel" Text="Cancel" OnClick="btnCancel_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
Below is the total code for Registration Page
using
System;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
public partial class Registration : System.Web.UI.Page
{
SqlConnection conCascading = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
string
UserName = txtuser.Text;
string
Password = txtpwd.Text;
string
ConfirmPassword = txtcnmpwd.Text;
string
FirstName = txtfname.Text;
string
LastName = txtlname.Text;
string
Email = txtEmail.Text;
string
Phoneno = txtphone.Text;
string
City = txtCity.Text;
conCascading.Open();
SqlCommand
cmd = new SqlCommand("SaveUserInformation", conCascading);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.Parameters.AddWithValue("@FirstName", FirstName);
cmd.Parameters.AddWithValue("@LastName", LastName);
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.AddWithValue("@PhoneNo", Phoneno);
cmd.Parameters.AddWithValue("@City", City);
SqlDataAdapter daSave = new SqlDataAdapter(cmd);
DataSet
dsSave = new DataSet();
daSave.Fill(dsSave);
conCascading.Close();
if (dsSave.Tables.Count > 0
&& dsSave.Tables[0].Rows.Count > 0)
{
Session["UserId"] = Convert.ToInt32(dsSave.Tables[0].Rows[0]["UserId"].ToString());
string
Message = "Successfully registared
. UserID is =" + dsSave.Tables[0].Rows[0]["UserId"].ToString();
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('" + Message + "')</script>");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script
language='javascript'>alert('Failed')</script>");
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
txtuser.Text = string.Empty;
txtpwd.Text = string.Empty;
txtcnmpwd.Text = string.Empty;
txtfname.Text = string.Empty;
txtlname.Text = string.Empty;
txtEmail.Text = string.Empty;
txtphone.Text = string.Empty;
txtCity.Text = string.Empty;
}
}
Output:
Step 1: Before clicking Save button.
Step 2 : After Clicking Save button.
Step 3 : Saved in Database record.
No comments:
Post a Comment