Tuesday, 23 December 2014

Login Form By UserType OR User Role In Windows Application In C#.Net , Linq

12/23/2014 - By Pranav Singh 0



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.


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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top