+91-90427 10472
         
Dot net training in Chennai

Dot net CRUD operation with Ms access database

Create a new MS database access file
Save the MS access db as db1.mdb file (Access 2000 Database file format) as shown below.

  • Create a table tblUserDetails in MS access with below columns.


Create a table tblStudentDetails with the below columns.

Create a new windows Application project MsAccessCRUD

Add a windows form AddLoginDetails.cs

Design the form as below,

Replace the AddLoginDetails class file as below,

public partial class AddLoginDetails : Form
{
public AddLoginDetails()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @”Provider=Microsoft.Jet.OLEDB.4.0;” +
@”Data source= D:\db1.mdb”;
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @”Insert Into tblUserDetails(UserName,[Password])VALUES(‘”+txtUserName.Text+”‘,'”+txtPassword.Text+”‘)”;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show(“Record Successfully Created”);
StudentLogin objStudentLogin = new StudentLogin();
objStudentLogin.Show();
this.Hide();
conn.Close();
}
}

Add New form StudentLogin.cs and design as below,

Replace the StudentLogin Class with below code,

public partial class StudentLogin : Form
{
public StudentLogin()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
// txtUserName.Text
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @”Provider=Microsoft.Jet.OLEDB.4.0;” +
@”Data source= D:\db1.mdb”;
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @”select * from tblUserDetails where Password='” +txtPassword.Text +”‘”;
cmd.Connection = conn;
conn.Open();
OleDbDataReader OleDBDR = cmd.ExecuteReader();
while (OleDBDR.Read())
{
string value= OleDBDR[0].ToString();
System.Windows.Forms.MessageBox.Show(“Login Successful”);
ShowStudentDetails objform = new ShowStudentDetails();
objform.Show();
this.Hide();
}
conn.Close();
}
private void Button1_Click(object sender, EventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
AddLoginDetails obj = new AddLoginDetails();
obj.Show();
this.Hide();
}
}

Add a new form ShowStudentDetails.cs and design the form as below,

Replace the ShowStudentDetails.cs class file with below code,

public partial class ShowStudentDetails : Form
{
public ShowStudentDetails()
{
InitializeComponent();
}
private void ShowStudentDetails_Load(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @”Provider=Microsoft.Jet.OLEDB.4.0;” +
@”Data source= D:\db1.mdb”;
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @”select * from tblStudentDetails”;
cmd.Connection = conn;
conn.Open();
OleDbDataAdapter ada = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds);
dgvStudentdetails.DataSource = ds.Tables[0];
conn.Close();
}
}

The solution explorer will look as below,

Replace the connection string for Ms access database before running the solution, now its d:\db1.mdb

The Output as below,

Click on Sign Up,

Click Add.

Ms Access DB as below,

Click below to download the solution,

https://1drv.ms/u/s!ArddhCoxftkQg9NHjhgsr8veFi056A