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
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 lots of database used for data storing purpose.Microsoft Access 2010 is one of them. The Microsoft provide two database for storage purpose
- Microsoft SQL Server Management Studio
- Microsoft Access
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:-
Step 2 :- Now create table (student) in Microsoft Access database with following fields as shown below:-
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 5 :-Now open your Student table in MS Access database--> You will see that data is inserted in table (student) as shown below:-
For More...
- How to Run C# Program on NotePad easily
- How to use WCF Services in asp.net
- Form based authentication in asp.net
- How to create HTTP Handler in asp.net
- How implement validation controls in asp.net
- How to implement cookie concepts in asp.net website
Download Whole Attached File
Website Access Database
Awesome post :)
ReplyDeletehow to export data from database to excel in asp.net
ReplyDelete