Create Login Form With User Authentication in C# using PostgreSQL
rashid@rashicode.com
0 Comments
asp.net, asp.net login page using database c#, authentication, c# login form, c# login with user authentication, c# tutorial, create login page in asp.net mvc, create login page in asp.net using session, create login window in c# step by step, create login window with user authentication c# example, create login window with user authentication in c#, create login window with user authentication in c# step by step, creating a login and registration system using flask, creating a login system, creating a login system in python, creating a login system with php and mysql, how to create c# login form?, how to create login form in asp net with database, how to create login form in c#, how to create login form in c# with database, how to create login form in c# with sql database, how to create login page in asp.net mvc using ajax, how to create login page in asp.net mvc with database, how to create login page in asp.net using sql database, how to create login page in asp.net with database using c#, how to create multi user login form in asp.net, how to create user registration form in c#, login and registration form in asp.net mvc, login and registration form in asp.net mvc 5, login form in asp.net core, login form in asp.net core razor pages, login form in asp.net using database, login form in c#, login form in c# with sql database, login page in asp.net using c# using sql server database, login tutorial in php, login window in c# step by step tutorial, login windows authentication in c#, multi user login in asp.net, sql server authentication in login screen, tutorial, user authentication in c#

Create Login Form With User Authentication in C# using PostgreSQL
Hello friend’s this is Rashid Hussain come with another video tutorial. In this tutorial I’ll show you how to create a simple login form with user authentication and how to link a database. Simply follow the steps below to accomplish this.
PostgreSQL is the database I’ll be using for this project.
Step 1
Make a simple form with the username and password text fields and a login button.

Step 2
Open pgadmin 4 and create table

Step 3
Now open Server Explorer and click on connection with database. and fill the requirements.


Step 4
Now Connect your project with Database(Postgresql). add new class and paste the code below
datalayer.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using Devart.Data; using Devart.Data.PostgreSql; using System.Configuration; using System.Windows.Forms; using System.Drawing; namespace Whole_Saler.forms { public class Datalayer { PgSqlCommand cmd_; PgSqlConnection conn_; PgSqlDataAdapter adptr_; PgSqlDataReader reader_; DataTable dtable_; DataSet dset_; public string getmessage { get; set; } public Datalayer() { string cs = "User Id=postgres;Host=localhost;Database=Student;Port=5432;Initial Schema=public;password=mysys;"; conn_ = new PgSqlConnection(cs); cmd_ = new PgSqlCommand(); dtable_ = new DataTable(); adptr_ = new PgSqlDataAdapter(); dset_ = new DataSet(); } public bool connect() { try { conn_.Open(); getmessage = "successfully connected"; return true; } catch (Exception ex) { getmessage = "connection error" + ex.Message; return false; } } public bool disconnect() { try { conn_.Clone(); return true; } catch (Exception ex) { return false; } } public string InsertUpdateDeleteCreate(string query) { string ret = ""; string allquerys = query.ToLower(); try { cmd_.CommandText = query.ToLower(); cmd_.Connection = conn_; connect(); cmd_.ExecuteNonQuery(); if (allquerys.ToLower().Contains("insert into")) { ret = getmessage = (" inseteted successfully "); } else if (allquerys.Contains("Delete form")) { ret = getmessage = ("delete successfull"); } else if (allquerys.Contains("Update into") && allquerys.Contains("set")) { ret = getmessage = ("update successfull"); } else if (allquerys.Contains("Creat table")) { ret = getmessage = ("create table successful"); } } catch (Exception exp) { ret = getmessage = "failed to execute" + query + "\n resoin :" + exp.Message; } finally { disconnect(); } return ret; } public string getsingleColumnValueByIndex(string query, out string columndata, int index) { string ret, val = null; try { cmd_.Connection = conn_; cmd_.CommandText = query; connect(); reader_ = cmd_.ExecuteReader(); while (reader_.Read()) { val = reader_[index].ToString(); } ret = "Operation Successfull!"; getmessage = "values successfully got from getSingleValueAsArrayByindex() function"; } catch (Exception exp) { ret = "Error in datalayer -> getSingleValueAsArrayByIndex() Reason:" + exp.Message; getmessage = "Error in datalayer getSingleValueAsArrayByIndex() for reader_ \n" + exp.Message; } finally { disconnect(); } columndata = val; return ret; } } }
loginform.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Whole_Saler.forms;
namespace LoginWithAuthentication
{
public partial class Form1 : Form
{
Datalayer dl;
public Form1()
{
dl = new Datalayer();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string s_id;
string qry = "select l_id from public.login where u_name='"+txtname.Text+"' and u_pswd='"+txtpswd.Text+"'";
dl.getsingleColumnValueByIndex(qry, out s_id, 0);
if (s_id != null)
{
Student_Details sd = new Student_Details();
sd.Show();
}
else
{
MessageBox.Show("User Name Or PAssword Doesn't Match Please Try Again !","Error"
,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
Step 5
Add new Windows Forms Student_details.

Student_Details.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Whole_Saler.forms; namespace LoginWithAuthentication { public partial class Student_Details : Form { Datalayer dl; public Student_Details() { InitializeComponent(); dl = new Datalayer(); } private void textBox2_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string qry = "insert into public.student (s_name,s_f_name,s_address,s_phone,s_class,s_rollno" + ") values('"+txtname.Text+"','"+txtfname.Text+"','"+txtaddress.Text+"','"+txtphno.Text +"','"+txtclas.Text+"','"+txtrollno.Text+"')"; MessageBox.Show( dl.InsertUpdateDeleteCreate(qry),"Message"); } } }
Video tutorial
Thank you for reading this article. Please don’t forget to subscribe our official YouTube Channel RashiCode