Tuesday, 8 May 2012

How to insert data into Ms Access 2007 using Windows C# || Inserting and Binding of data into Ms Access in Windows C#.Net



To implement this concept you need to follow the below steps : 

Step1 :
First you need to design a table in MS Access 2007 Database to insert the records in database.

Step2:
On the File menu, click New Project. Select Windows Forms Application as your project type.
Design the Form using controls from Toolbox.

Step3:
Now open the Form.cs page and write the following source code.

Form.cs Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Visiting_Card
{
    public partial class Form1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\VisitingCard.accdb");
        public Form1()
        {
            InitializeComponent();
            Bind();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand("VisitingCard_Insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Names",txtName.Text);
            cmd.Parameters.AddWithValue("@Organization",txtOrganization.Text);
            cmd.Parameters.AddWithValue("@Location",txtLocation.Text);
            cmd.Parameters.AddWithValue("@PhoneNo",txtPhone.Text);
            cmd.Parameters.AddWithValue("@EmailId",txtEmail.Text);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result > 0)
            {
                MessageBox.Show("Inserted Successfully");
            }
            Bind();
        }

        private void Bind()
        {
            con.Open();
            OleDbCommand cmd1 = new OleDbCommand("VisitingCard_Bind", con);
            cmd1.CommandType = CommandType.StoredProcedure;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd1);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();

        }
    }
}


Step4:
Now build the Solution and Debug it for the output.

OutPut :

 

Ms Access Database:



2 comments:

  1. After using this code i get error "The Microsoft Access database engine cannot find the input table or query 'SignUp_Insert'. Make sure it exists and that its name is spelled correctly."

    My table name is "SignUp"

    ReplyDelete
    Replies
    1. Make sure that the database file must be added to App_Data folder and also the connection string.

      Delete

CRUD operations using AngularJS in Asp.Net MVC

In this article I will show you how to perform CRUD operations in Asp.Net MVC5 using AngularJS. Add AngularJS using the Manage Nuget Pack...