How to insert data in Microsoft Access Database and bind it in Gridview using C# in APS.NET Application

By
Introduction :-
There are lots of database used for data storing purpose.Microsoft Access 2010 is one of them. The Microsoft provide two database for storage purpose 
  1. Microsoft SQL Server Management Studio
  2. Microsoft Access
We have already done insert,update and delete operations with SQL Server. Now we learn "How to Insert data in Microsoft Access database ". These two database are Microsoft  products so we have to know about its connectivity.These two are more compatible with .NET Framework. These are more reliable and secure database.But Nowadays Mysql and Oracle database are mostly used in the world. These are also more reliable and secure like sql server. Here i will insert some values in MS Access database and bind its data with gridview control. You can easily perform update and delete operations also but here i will perform only insert operation. 

There are some steps to implement this concepts in ASP.NET Application as given below:-
Step 1 :- First Open your visual studio --> File--> New --> website -->Select ASP.NET Empty Website-->OK --> Add New Web form (Default.aspx) -->Drag and Drop Label ,TextBox, Button and Gridview controls on the page from ToolBox as shown below:-
    
web form

Step 2 :- Now create table (student) in Microsoft Access database with following fields as shown below:-
column

Note :-
If are facing problem to create table in MS Access database then see the below link:-
Step 3 :- Now write the c# codes on each button (Submit and Display)'s click ( Default.aspx.cs ) as given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection my_con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\\Ramashanker\\Documents\\msdotnet.accdb");
        my_con.Open();
        OleDbCommand o_cmd = new OleDbCommand("insert into student values(@a,@b,@c,@d)", my_con);
        o_cmd.Parameters.AddWithValue("a",TextBox1.Text);
        o_cmd.Parameters.AddWithValue("b", TextBox2.Text);
        o_cmd.Parameters.AddWithValue("c", TextBox3.Text);
        o_cmd.Parameters.AddWithValue("d", TextBox4.Text);
        int i = o_cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Label1.Text = "Data inserted successfully";
        }
        my_con.Close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        OleDbConnection my_con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\\Ramashanker\\Documents\\msdotnet.accdb");
        my_con.Open();
        OleDbCommand o_cmd = new OleDbCommand("select * from student", my_con);
        OleDbDataAdapter da = new OleDbDataAdapter(o_cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Note :-
  • Here you have to provide full path of your database file.By default ,this database is stored in Your Computer's My Documents Folder. 
  •  You can change database path in any drive. 
  • You can perform delete and update operations with Ms Access Database also.
Step 4 :- Now Run the application (press F5) --> Enter Required field Details --> Press Submit button --> After that press Display Button --> You will see following output as shown below:-
OUTPUT

Step 5 :-Now open your Student table in MS Access database--> You will see that data is inserted in table (student) as shown below:-
table data

For More...
  1. How to Run C# Program on NotePad easily
  2. How to use WCF Services in asp.net 
  3. Form based authentication in asp.net
  4. How to create HTTP Handler in asp.net
  5. How implement validation controls in asp.net
  6. How to implement cookie concepts in asp.net website
If you want run this application on your system directly then you can download below files.
Download Whole Attached File
 Website          Access Database 

2 comments:

Powered by Blogger.