This article will show you how you can create a login form
power with user type. In this we will use linq qiery to validate the user
detail as per there selected role in windows application using c#.net.
Some of my previous articles are as follows: Display
Message With Warning icon in Windows Application Using C#.Net, Stylish
ToolStrip Button Control With Image and Text Control In Windows Application Using
C#.Net, No
of Days Between Two Dates By Using TimeSpan In Windows Application In C#.Net
and VB.Net, Validate
Directory Exist or Not in C#.Net In Windows Application, Image
Save In XML and Load XML Bitmap Image File in Windows Application Using C#.Net,
How
to Save Record in XML File and Read XML to Display in DataGridview Using C#.net
in Windows Application, How
to Add HyperLink and Retrieve Row Value on Link Click in DataGridView #,
Windows application, How
to Add Button Control and Retrieve Row Value on Button Click in DataGridView
Using C#.Net, Windows Application, Save
Panel with Control Inside It as Image C#.net, VB.net in windows application.
So for this article first we will created a windows application
and create the login form as shown below.
Now we will create the class file and add the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public
class DataCollection
{
public string UserId { get;
set; }
public string Password { get;
set; }
public string UserType { get;
set; }
}
}
|
Now add the below code on button click.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
public
List<DataCollection>
UserDataCollection()
{
List<DataCollection> objDataCollection = new List<DataCollection>();
objDataCollection.Add(new DataCollection
{ UserId = "user1", Password = "p1", UserType = "Admin" });
objDataCollection.Add(new DataCollection
{ UserId = "user2", Password = "p2", UserType = "Admin" });
objDataCollection.Add(new DataCollection
{ UserId = "user3", Password = "p3", UserType = "User" });
objDataCollection.Add(new DataCollection
{ UserId = "user4", Password = "p4", UserType = "User" });
return
objDataCollection;
}
private
void btnclose_Click(object
sender, EventArgs e)
{
this.Close();
}
private
void btnlogin_Click(object
sender, EventArgs e)
{
List<DataCollection> objDataCollection = new List<DataCollection>();
objDataCollection = UserDataCollection();
var
verifyUser = (from data in objDataCollection
where data.UserId == txtUserId.Text &&
data.Password
== txtPassword.Text &&
data.UserType ==
ddlType.SelectedItem.ToString()
select data);
if
(verifyUser.Count() > 0)
{
MessageBox.Show("Correct User Id and Password.", "Message", MessageBoxButtons.OK,
MessageBoxIcon.None);
}
else
{
MessageBox.Show("Wrong User Id and Password.", "Message", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
|
In above code I have created a collection of the user id and
password and user type. Now on button
click I have used linq query to validate the detail. Now we have done run the
application to check the output.
0 comments:
Please let me know your view