Login Form Using LINQ in Windows Forms Application

Step 1: First we create a database and then Table in SQL Server. Here we create a table named “logintb”.



Now insert a name and Password into the logintb table.




Step 2: Open Visual Studio then select "File" -> "New" -> "Project..." then select "Windows Forms".

Drag and drop a Label for ID and a Label for password.
Drag and drop a TextBox for txtuser and a TextBox for txtPassword.
Drag and drop a Button for Login.



Step 3: Go to the project then right-click and seelct "Add" -> "New Item..." then select "Windows Form" -> "LINQ to SQL Classes" then click on "Add".



Now go to the Server Explorer, drag and drop the logintb table as in the following:



Open Server Explorer and select the database and then the table.





Step 4 : Now double-click on the button Login in the form and write the following code:

CS Code:
 

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 Login : Form

    {

        public Login()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (IsvalidUser(txtUser.Text, txtPassw.Text))

            {

                Form1 F = new Form1();

                F.Show();

 

            }

        }

 

        private bool IsvalidUser(string userName, string password)

        {

 

            DataClasses1DataContext context = new DataClasses1DataContext();

            var q = from p in context.logintbs

                    where p.name == txtUser.Text

                    && p.passw == txtPassw.Text

                    select p;

 

            if (q.Any())

            {

                return true;

            }

            else

            {

                return false;

            }

        }

    }

}


Similar Articles