Programming for Everybody: March 2021

Programming with C # tutorial: Login Form in C # with SQL Server

 




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 login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data source=.; initial catalog=users;integrated security=true");

            SqlCommand cmd = new SqlCommand("select * from login1 where user1=@user1 and pass1=@pass1", conn);

            cmd.Parameters.Add("@user1", SqlDbType.VarChar).Value = textBox1.Text;

            cmd.Parameters.Add("@pass1", SqlDbType.VarChar).Value = textBox2.Text;


            SqlDataAdapter adapter1 = new SqlDataAdapter(cmd);

            DataTable table = new DataTable();

            adapter1.Fill(table);

            if (table.Rows.Count == 1)
            {

                Form2 gg = new Form2();

                gg.Show();

                this.Hide();

            }
            else
            {
                MessageBox.Show("Error username or password");


            }



        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBox1.Checked == true)
            {
                textBox2.UseSystemPasswordChar = false;
            }
            else
            {

                textBox2.UseSystemPasswordChar = true;
            }



        }

       
    }

    }