F How to create login using separate Class in C# | CodeTheta

How to create login using separate Class in C#

February 17, 2019

1. Create a folder within your project folder / solution.
2. Example : MainClasses. Within this folder create a class named CoreLIB.cs

Put the connectionString to your App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="connstrng" connectionString="Data Source=.;Initial Catalog=database;Integrated Security=True;"/>
</connectionStrings>
</configuration>

use Getter and Setter to CoreLIB.cs file.
public string Username { get; set; }
public string Password { get; set; }
static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

use following namespace system CoreLIB.cs file
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

Use login function
public bool Login(CoreLIB c)
        {
            bool isSuccess = false;
            SqlConnection conn = new SqlConnection(myconnstrng);
            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT * FROM tablename WHERE Username=@Username AND Password=@Password";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@Username", c.Username);
                cmd.Parameters.AddWithValue("@Password", c.Password);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                adapter.Fill(dt);

                if(dt.Rows[0][0].ToString() == "1")
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }

            }
            catch(Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return isSuccess;
        }

Login button code
use the following namespace system
using IJIRALIB.IJIRAClasses;
using System.Configuration;
using System.Data;


CoreLIB c = new CoreLIB();
private void btnLogin_Click(object sender, EventArgs e)
        {
            c.Username = txtUsername.Text;
            c.Password = txtPassword.Text;
            bool success = c.Login(c);
            if(success==true)
            {
                this.Hide();
                frmDashboard frm = new frmDashboard();
                frm.Show();
            }
            else
            {
                MessageBox.Show("Invalid Login");
            }
        }


IDE Used To Test This Code : Visual Studio 2012.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment

PRODUCTS

LISTS OF PRODUCTS

SERVICES

SKILLS
SOFTWARE DEVELOPMENT
WEBSITE DEVELOPMENT
WEB HOSTING
BULK SMS SERVICE
SEO SERVICE
ANDROID APPS
QR CODE / BARCODE
HARDWARE SERVICE
OUR WORK AREA