C#.Net SQL Login Form

C#.Net SQL Login Form

 


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;

using System.Data.SqlClient;

 

namespace SQLConnection_Full

{

    public partial class Form4 : Form

    {

        public Form4()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

 

            try

            {

                if (!(textBox1.Text == string.Empty))

                {

                    if (!(textBox2.Text == string.Empty))

                    {

                        SqlConnection con = new SqlConnection("Data Source=DESKTOP-31HBTKT\\SQLEXPRESS; Initial Catalog=ICTPT;Integrated Security=True");

 

                        con.Open();

 

                        SqlCommand cmd = new SqlCommand("SELECT username,password FROM login Where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'", con);

 

                        SqlDataReader dr = cmd.ExecuteReader();

 

                        int count = 0;

                        while (dr.Read())

                        {

                            count = count + 1;

                        }

                        if (count == 1)

                        {

                            //MessageBox.Show("username and password is correct");

                            Form1 fm = new Form1();

                            fm.Show();

                            this.Hide();

                        }

                        else if (count > 1)

                        {

                            MessageBox.Show("Duplicate username and password", "login page");

                        }

                        else

                        {

                            MessageBox.Show(" username and password incorrect", "login page");

                        }

                    }

                    else

                    {

                        MessageBox.Show(" password empty", "login page");

                    }

                }

 

                else

                {

                    MessageBox.Show(" username empty", "login page");

                }

 

            }

 

            catch (Exception es)

            {

                MessageBox.Show(es.Message);

            }

 

 

        }

    }

}

 

Post a Comment

0 Comments