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 CCharpProject
{
    public partial class FrmLogin : Form
    {
//------CUSTOM GENERATE CLASS----------------------------------------------------------------
        string connstr = "Data Source=PC-NAME\SQLEXPRESS;Initial Catalog=test_db;"+
		                 "Integrated Security=True;Pooling=False";
        string chk;

        private String Login(string _user, string _pass)
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            string _sql = "select *"+
                          " from TBuser"+ 
                          " where user1 = "+"'"+_user+"'"+
                          " and pass = "+"'"+_pass+"'";

            SqlCommand comm = new SqlCommand(_sql, conn);
            SqlDataReader dr;
            try
            {                
                dr = comm.ExecuteReader();
                dr.Read();
                chk = dr["user1"].ToString();

                return "";
            }
            catch
                (Exception ex)
            {
                chk = ex.Message.ToString();
                return chk;
            }

        }

        private void Openform()
        {
            FrmMain frm = new FrmMain();
            frm.StartPosition = FormStartPosition.CenterParent;
            this.Hide();
            frm.ShowDialog();
            this.Close();
        }

        private void Cleartext()
        {            
            txtPass.Text = "";
            txtUser.Text = "";
        }

//------AUTO GENERATE CLASS----------------------------------------------------------------
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btok_Click(object sender, EventArgs e)
        {
            lbStatus.Text = Login(txtUser.Text, txtPass.Text);

            if (lbStatus.Text == "")
            {
                Openform();
            }
            else
            {
                lbStatus.Text = "invalid username or password.n"+"("+chk+")";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Cleartext();
        }
    }
}
C# Beginner : Log in Form

Comments

comments