Thursday, 2 July 2015

Insert,Update,Delete for MS Access With C#.Net

Hi There.In this Blog I will explain how to perform simple Insert,Update and Delete Operation for Microsoft Access Database using C#.Net

Steps By Step Implementation :


  1. Open Microsoft Visual Studio 2010 go to File>New Project>Windows From Application & Name the project as "database".(You can give any name as per your choice).
  2. Now Open the toolbox (Click on View>Toolbox).You will see the various controls that you can use in application.
  3. In next step Add The following Controls in your Application
  • 3 Labels (First Name,Last Name,Mobile)
  • 3 Text Boxes
  • 4 Buttons (Insert,Update,Delete,Exit) respectively.
  • After inserting all the controls,your application should see like this below.
       4. Now next step is to Create The Database in Microsoft Access.
           Below are steps to create the database:
                         
  • Open Microsoft Office Access 2007 (Any Version). And follow the steps shown in below video.

  • Now Open Microsoft Visual Studio 2010 and click on View>>(right click) Add Connection.You will see the below window:
  • Click on "browse" & Select the database file that you have created.The destination of file is  C:\Users\SK\Documents\Visual Studio 2010\Projects\database\database\bin\Debug\database123.mdb.Remember that this path will be different for your computer.
  • After that,click on Test Connection,it will say "Test Connection Succeded".Then click "OK".Then you can see "database123" will appear under Data connection.This will end the part of creating the database & adding connection.
 
       5. In this step,we will learn actual Coding and Implementation of each query separately :

            1) INSERT : 
  • Double click on INSERT button & write the below code as it is.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simple
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection();
           private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SK\Documents\Visual Studio 2010\Projects\database\database\bin\Debug\database123.mdb";
                con.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandText = @"insert into databasetable (FNAME,LNAME,MOBILE) values('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "')";
                cmd.Connection = con;
                //con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Saved");
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("failed" + ex);
            }
        }
       

    }
}

 

  •  You will get the error on the connection string (Represent in Red words).Because it will be different for your computer.So this error can be solved using below simple step.
i) Click on "database123" under Data Connection.Then right     click & go to Properties.
                              ii) The first option in properties is Connection string.So simply copy that string
                                   and paste it in programme.
                                 
   
     6) So,Now you will get error free code,Then simply run the programme & write the records
        (firstname,lastname,mobile) &  click on INSERT.And then check whether the records are
        inserted succesfully or not.
       
                       

     7)   I will upload the next methods for performing Update & Delete operation as early as
            possible.Please give me some comments if it is useful or not.You can also ask me if you got
            any problem.Thank You !!!




2 comments: