- Registration screen
- Login screen
- Student details display screen
Once an user registers, he may login using the credentials he/she supplied at the time of registration, also on successful login, the user's name and location are displayed via label controls.
Welcome Screen
Source code for above form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinStudentLogin
{
public partial class WelcomeForm : Form
{
public WelcomeForm()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
Login lgn = new Login();
lgn.ShowDialog();
}
private void btnReg_Click(object sender, EventArgs e)
{
Register rgform = new Register();
rgform.ShowDialog();
}
}
}
Registration Screen
Source code for above form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WinStudentLogin
{
public partial class Register : Form
{
public Register()
{
InitializeComponent();
}
private void Submit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
// Create a connection object
con.ConnectionString = @"Data source=USERS35\SQLExpress;initial catalog = training1;integrated security =true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
//Best practise: Use stored procedures instead of direct row as shown below
//Important note: Change command type to stored procedure as shown below
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertRowStudent";
//"insert into student values(@id,@stuName,@pwd,@loc)";
cmd.Parameters.AddWithValue("@StuId", txtUid.Text);
cmd.Parameters.AddWithValue("@StuName", txtName.Text);
cmd.Parameters.AddWithValue("@pwd", txtPwd.Text);
cmd.Parameters.AddWithValue("@loc", txtLoc.Text);
try
{
if (txtPwd.Text == txtReType.Text)
{
con.Open();//establishes phy link between App and DB
cmd.ExecuteNonQuery();
MessageBox.Show("You have been registered");
Login lgn = new Login();
lgn.ShowDialog();
}
else
{
MessageBox.Show("The passwords do not match, please re type the password");
txtReType.Clear();
txtReType.Focus();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
}
Login Screen
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WinStudentLogin
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
// Create a connection object
con.ConnectionString = @"Data source=USERS35\SQLExpress;initial catalog = training1;integrated security =true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select id,stuName,Pwd,loc from student where id=" + txtUid.Text;
//0 1 2 3 4
try
{
con.Open();//establishes phy link between App and DB
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();//Reads one row from employee and return bool
if (dr["Pwd"].ToString() == txtPwd.Text)
{
MessageBox.Show("Success");
StudentDetails stuFrm = new StudentDetails(dr["stuName"].ToString(),dr["loc"].ToString());
stuFrm.ShowDialog();
}
else
{
MessageBox.Show("Login failed");
}
dr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
}
Student Details Screen (Shows student details based on login)
Source code for above form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinStudentLogin
{
public partial class StudentDetails : Form
{
public StudentDetails(string StuName,string loc)
{
InitializeComponent();
txtStuName.Text = StuName;
txtLoc.Text = loc;
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void StudentDetails_Load(object sender, EventArgs e)
{
}
}
}





