Home / Learn programming language / How To Create LAN login form in c# (Step by Step Guide)

How To Create LAN login form in c# (Step by Step Guide)

This project is a combination of MS access connection string in c#, networking ms access database over LAN and also how to create a lan login form in c# with access database. Our aim is to ensure you have solid understanding networking database over LAN and also c# code for login form using database. Let go straight to the point.

Open a new project and call it LANDB. Am sure you know how to create a project in visual studio 2008, 10, 12 and 15. After the opening of the project rename your form to login.cs and place two labels and two textboxes and a button.

Labels Text: Username and Password

Textboxes name: Username and Password

Button name and text: loginbtn and Login

This is the full sample code, well tested and run fine. And you can download a sample project of this code at 500 naira by contacting me via prisdco@gmail.com. Thanks.

The sample project of how to create a LAN Login Form in c# is below:

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.OleDb;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

using System.Data.SqlClient;

using System.Xml.Serialization;

using System.IO;

using System.Web;

using System.Web.UI;

using System.Threading;

using System.Collections;

 

namespace LANDB

{

public partial class LoginForm : Form

{

String db = “”;

static String PATH = “.\\bin\\”;

DataSet memoryDataset;

private OleDbConnection con = null;

private OleDbCommand command = null;

private OleDbDataAdapter dataAdapter = null;

public LoginForm()

{

InitializeComponent();

memoryDataset = new DataSet();

}

 

// INITIALIZE AND CONFIGURE DATABASE

public void InitiateDatabaseConfiguration(String connect)

{

try

{

//Initialize SQLDB.

con = null;

con = new OleDbConnection(connect);

}

catch (Exception)

{

MessageBox.Show(“Unable to Connect to the Database.”, “”, MessageBoxButtons.OK);

}

 

}

 

public object[] LoadDataArrayByQuery(String Query, String connection)

{

 

InitiateDatabaseConfiguration(connection);

 

DataSet dataSet = new DataSet();

 

try

{

//Initialize OleDbCommand to Execute SQL Query

command = new OleDbCommand(Query, con); // Execute ‘query’ from con instance using

// OleDbCommand

 

 

//Initialize OleDbDataAdapter to retrieve and Populate Dataset with the Data in the

// OleDbCommand

dataAdapter = new OleDbDataAdapter(command);

 

// Before Filling the Dataset from the dataAdapter

// open the OleDbConnection ‘con’ first

con.Open();

// Then Fill the dataset with the data in the dataAdapter

dataAdapter.Fill(dataSet);

 

con.Close();

}

catch (Exception)

{

//”Unable to Load the DataSet.”

//MessageBox.Show(e.Message, “”, MessageBoxButtons.OK, MessageBoxIcon.Error);

}

 

//Return the Dataset Object

try

{

return dataSet.Tables[0].Rows[0].ItemArray;

}

catch (Exception)

{

return null;

}

 

}

 

private void loginbtn_Click(object sender, EventArgs e)

{

String err = null;

 

if (Username.Text.Equals(“”))

{

err += “Username, “;

}

if (Password.Text.Equals(“”))

{

err += “Password “;

}

 

if (err != null)

{

MessageBox.Show(“The following feilds are required:\n” + err, “Bambam Error Detector”, MessageBoxButtons.OK, MessageBoxIcon.Error);

return;

}

//db = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\royal\\Documents\\Visual Studio 2008\\Projects\\Bambam\\Bambam\\bin\\Debug\\bin\\photo.mdb;Persist Security Info=True;User ID=admin; Jet OLEDB:Database Password=SecureEdu;”;

db = “Provider=Microsoft.JET.OLEDB.4.0;Jet OLEDB:Database Password=SecureEdu;data source=” + PATH;

 

String chk = “SELECT * FROM Administrator WHERE Username='” + Username.Text + “‘ AND Password='” + Password.Text + “‘”;

object[] data = LoadDataArrayByQuery(chk, db);

if (data == null)

{

MessageBox.Show(“Invalid Login. Try again.”, “Administrator”, MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

String userID = data[0].ToString();

MessageBox.Show(“Welcome ” + data[2].ToString() + “.”, “Administrator”, MessageBoxButtons.OK, MessageBoxIcon.Information);

this.Hide();

new HomePage().Show();

}

}

 

private void LoginForm_Load(object sender, EventArgs e)

{

 

}

 

private void btnbrowse_Click(object sender, EventArgs e)

{

try

{

openFileDialog1.Filter = “Microsoft Office Access Database|*.mdb”;

openFileDialog1.ShowDialog();

PATH = openFileDialog1.FileName;

if (!PATH.ToLower().EndsWith(“.mdb”))

{

MessageBox.Show(“Microsoft .mdb File Only.”, “Invalid Picture”, MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

catch { MessageBox.Show(“Select the MS DB File”, “SELECT DB”, MessageBoxButtons.OK, MessageBoxIcon.Error); }

}

}

}

 

data-matched-content-rows-num="2" data-matched-content-columns-num="2" data-matched-content-ui-type="image_stacked"

About Joseph Sam

Check Also

c# access database

How to Send email using c#

Emailing can be something very essential to some application maybe your client might be in …

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »