Today, we will discuss about login screen in C#.
You know that in login screen,there is user id and password textbox.one login button and one exit button.
Let us start.
first open visual studio-->File-->New-->Project-->Windows-->Windows Form Application
Name project as Loginwindow.
Name form1 as Loginform.
take 2 textbox,name it as txtUserId and txtPassword.
please change property PasswordChar of txtPassword to *.It will make our password to * character like this ****.
2 button,
3 label ,name it as lbluserid, lblpassword, lblStatus.
and 1 panel and make layout of form as below .
When user enter correct user id and password,text of label lblStatus is "Login successful"
if user id and password is not correct,text of label lblStatus is "User id and password not match"
Open sql server management studio and create a table tblUser.create 2 column userid and password.
Script for creating table is
GO
CREATE TABLE [dbo].[tblUser](
[UresId] [nvarchar](50) NULL,
[Password] [int] NULL
) ON [PRIMARY]
GO
Insert userid and password in table.below is script
insert into tblUser (UresId,Password)values('test',1234)
now,we have to write code on login button click.
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=abc-PC;Initial Catalog=test;Integrated Security=True");
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from tblUser", con);
DataSet ds = new DataSet();
adp.Fill(ds);
con.Close();
if (txtUserId.Text == ds.Tables[0].Rows[0][0].ToString() && txtPassword.Text == ds.Tables[0].Rows[0][1].ToString())
{
lblStatus.Text = "Login successful";
}
else
{
lblStatus.Text = "User id and password not match";
}
}
Run the program.this window will open.
if user enter 'test' in userid textbox and 1234 in password textbox,label status text is login successful.
if user enter wrong password, status label text is "User id and password not match"
You know that in login screen,there is user id and password textbox.one login button and one exit button.
Let us start.
first open visual studio-->File-->New-->Project-->Windows-->Windows Form Application
Name project as Loginwindow.
Name form1 as Loginform.
take 2 textbox,name it as txtUserId and txtPassword.
please change property PasswordChar of txtPassword to *.It will make our password to * character like this ****.
2 button,
3 label ,name it as lbluserid, lblpassword, lblStatus.
and 1 panel and make layout of form as below .
When user enter correct user id and password,text of label lblStatus is "Login successful"
if user id and password is not correct,text of label lblStatus is "User id and password not match"
Open sql server management studio and create a table tblUser.create 2 column userid and password.
Script for creating table is
GO
CREATE TABLE [dbo].[tblUser](
[UresId] [nvarchar](50) NULL,
[Password] [int] NULL
) ON [PRIMARY]
GO
Insert userid and password in table.below is script
insert into tblUser (UresId,Password)values('test',1234)
now,we have to write code on login button click.
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=abc-PC;Initial Catalog=test;Integrated Security=True");
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from tblUser", con);
DataSet ds = new DataSet();
adp.Fill(ds);
con.Close();
if (txtUserId.Text == ds.Tables[0].Rows[0][0].ToString() && txtPassword.Text == ds.Tables[0].Rows[0][1].ToString())
{
lblStatus.Text = "Login successful";
}
else
{
lblStatus.Text = "User id and password not match";
}
}
Run the program.this window will open.
if user enter 'test' in userid textbox and 1234 in password textbox,label status text is login successful.
if user enter wrong password, status label text is "User id and password not match"
Abstraction and Encapsulation in OOPS
Inheritance in OOPS
No comments :
Post a Comment