This article will show you how you can create a login form
in windows application using c#.net. In this we will validate user from
database and if user enter wrong detail he will get error message or will get
success message.
Now here is you login form. This is a custom design loginform in c# windows application.
Now here is you login form. This is a custom design loginform in c# windows application.
Now please check the code.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
//Data base connection string
string connection = @"Server=LAPTOP-EVJEKV38\SQLEXPRESS;
Database=TestDB; Integrated Security=True";
SqlConnection con = new SqlConnection(connection);
try
{
DataTable dt = new DataTable();
//Sql query to validate the
detail
string query = "Select *
from UserLogin Where LoginId='" + txtLogin.Text + "' and
Password='" + txtPassword.Text + "';";
//Execute the query
SqlDataAdapter da = new SqlDataAdapter(query, con);
con.Open();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Login Successfully
Done..");
}
else
{
MessageBox.Show("No such user
found..");
}
//After login clear the
fields
ClearData();
}
catch
{
MessageBox.Show("Error
occured...");
}
finally
{
con.Close();
}
}
//Clear data after submit
private void ClearData()
{
txtLogin.Text = "";
txtPassword.Text = "";
}
}
}
|
In above code you just need to change the connection string
and the query as per your requirement. In above code I have collected the data
after executing the and if there is any record in datatable.
Now here is the record in base table.
Now run the application and enter a correct login detail.
Now enter the wrong detail and check.
Subscribe My YouTube Chanel
0 comments:
Please let me know your view