Design your own registration form. Below is the example of a simple registration form.
Then create a table in the database (I will post about how to create the table in the SQL database and connect to Visual Studio later).
Open the coding page and ensure that all these using directives are placed on top of the coding page.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
Then double click on the Sign Up button
connection.Open();
SqlCommand command = new SqlCommand("INSERT INTO YourTableName (fullname,username,[password]) VALUES (@nm, @un, @up)", connection);
command.Parameters.AddWithValue("@nm", txtfullname.Text);
command.Parameters.AddWithValue("@un", textBoxusername.Text);
command.Parameters.AddWithValue("@up", GetSha1(textBoxpwd.Text)); // hash function
if ((command.ExecuteNonQuery() == 0))
{
MessageBox.Show("Registration Completed Successfully", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
login loginform = new login();
loginform.Show();
}
else if (FullName.Trim().Equals(""))
{
MessageBox.Show("Please fill in all required details", "Registration",MessageBoxButtons.OK, MessageBoxIcon.Error);
txtfullname.Text = "";
textBoxusername.Text = "";
textBoxpwd.Text = "";
txtfullname.Focus();
}
else if (Username.Trim().Equals(""))
{
MessageBox.Show("Please fill in all required details", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtfullname.Text = "";
textBoxusername.Text = "";
textBoxpwd.Text = "";
txtfullname.Focus();
}
else if (UserPassword.Trim().Equals(""))
{
MessageBox.Show("Please fill in all required details", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtfullname.Text = "";
textBoxusername.Text = "";
textBoxpwd.Text = "";
txtfullname.Focus();
}
else if (cpwd.Trim().Equals(""))
{
MessageBox.Show("Please fill in all required details", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtfullname.Text = "";
textBoxusername.Text = "";
textBoxpwd.Text = "";
txtfullname.Focus();
}
else if ((textBoxpwd.Text != textBoxcpwd.Text)) //confirmation of password
{
MessageBox.Show("Password did not match", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
textBoxpwd.Text = "";
textBoxcpwd.Text = "";
textBoxpwd.Focus();
}
else
{
MessageBox.Show("Registration failed", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
connection.Close();
Then click the Clear Button
txtfullname.Clear();
textBoxusername.Clear();
textBoxpwd.Clear();
textBoxcpwd.Clear();
txtfullname.Focus();
For the hash function, I'm gonna use the SHA1 hash function.
public static string GetSha1(string value)
{
var data = Encoding.ASCII.GetBytes(value);
var hashData = new SHA1Managed().ComputeHash(data);
var hash = string.Empty;
foreach (var b in hashData)
{
hash += b.ToString("X2");
}
Thank you for reading and feel free to give your opinion or correct me if there is anything wrong with my post.
Comments
Post a Comment