connectseekers.com

Tuesday, 31 March 2020

Login Form In C# With SQL Database

3/31/2020 - By Pranav Singh 0


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 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

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