Creating a Simple Registration or Signup and Login or Signin Form using Asp.Net, C#.Net and Sqlserver
Introduction
This article explains about creating a simple registration or signup and login or sign in form using asp.net, c#.net and sqlserver.Creating Database:
- Create a databse in sqlserver with name 'TestDB'
- Next create a table with fields as shown in picture.
- Now create a new website using visual studio 2008 0r 2010.
- Design the registraion form and login form as shown in above pictures. In these two forms, validation controls are used to validate the fields as per requirement. To know about validation controls in asp.net read the following article.
- Add connection string property in web.config file as shown below.
<configuration> <connectionStrings> <add name="CM_Connection" connectionString="data source=localhost;Initial Catalog=TestDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web>
Note:In this web.config file, the connection string name is 'CM_Connection'. You can change data source to your local server name (here it is localhost). - Next in the 'CreateUser' button click event write the following code.
if (Page.IsValid) { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString)) { SqlCommand cmd = new SqlCommand(); Guid guid; guid = Guid.NewGuid(); string sql = "INSERT INTO TestDB_RegisterUser (registeruser_id,registeruser_username,registeruser_email,registeruser_password"; sql += " ,registeruser_createdon,registeruser_modifiedon,registeruser_rowstate)"; sql += "VALUES (@ID,@Username,@Email,@Password,@Createdon,@Modifiedon,@Rowstate)"; cmd.Parameters.AddWithValue("@ID", guid); cmd.Parameters.AddWithValue("@Username", UserName.Text.Trim()); cmd.Parameters.AddWithValue("@Email", Email.Text.Trim()); cmd.Parameters.AddWithValue("@Password", Password.Text); cmd.Parameters.AddWithValue("@Createdon", DateTime.Now); cmd.Parameters.AddWithValue("@Modifiedon", DateTime.Now); cmd.Parameters.AddWithValue("@Rowstate", 1); cmd.Connection = con; cmd.CommandText = sql; con.Open(); try { cmd.ExecuteNonQuery(); ErrorMessage.Text = "Registered successfully."; } catch(Exception ex) { throw new Exception(ex.Message); } } }
Note: You can check the validity of a Page by checking the Page.IsValid property. For this property to return true, all validation server controls in the current validation group must validate successfully. You should check this property only after you have called the Page.Validate method, or set the CausesValidation property to true in the OnServerClick event handler for an ASP.NET server control that initiates form processing. In simple words the Page.IsValid property tells you whether the validation succeeded or not.
Creating Signin or Login form: In the 'Log In' button click event write the code as shown below.
stringemail = UserName.Text.Trim(); string pwd = Password.Text; DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; // cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "SELECT registeruser_id,registeruser_username, registeruser_email,registeruser_password FROM TestDB_RegisterUser where registeruser_email='" + email + "' and registeruser_password='" + pwd + "' and registeruser_rowstate<3 "; con.Open(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(dt); if (dt.Rows.Count > 0) { Session.Add("UserID", dt.Rows[0][0].ToString()); Session.Add("Username", dt.Rows[0][1].ToString()); Session.Add("UserEmail", dt.Rows[0][2].ToString()); Response.Redirect("~/Default.aspx", false); } else { FailureText.Text = "Invalid username or password"; } }
Thus you can create a registration form and log in form using asp.net
Comments/Suggestions are invited. Happy coding......!
Source Code can be download from here : |
You can try these site also
ReplyDeleteSearchastrologers.com
Data Entry Jobs