Thursday 13 February 2014

how to connect MS access in asp.net

If  you want to 

-> Insert record in MS access   OR

-> Display record from MS access database  OR

-> Update record in MS access  OR

-> delete record in MS access

--> then do following

-->The following code Copy and paste in Your .aspx page
------------------------------------------------------------------------------------------------



<body>
    <form id="form1" runat="server">
   
Id
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br>           
Name
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br>          
Age
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br>           
City
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br><br>
                     
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Edit" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Delete" />

 <asp:GridView ID="GridView1" runat="server"  >
 </asp:GridView>
 </form>
 </body>


--> And  Your code behind file in paste the following code
------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    OleDbConnection cn;
    OleDbCommand cmd;
    OleDbDataAdapter da;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Path your data base \student_db.mdb");
        // complete database path where your access file is stored
        if (!IsPostBack)
        {
            disp();
        }
    }
    public void disp()
    {
        cmd = new OleDbCommand("select * from tbl ", cn);
        da = new OleDbDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new OleDbCommand("insert into tbl(id,s_name,age,city) values ('"+TextBox1.Text+"', '" + TextBox2.Text + "'," + TextBox3.Text + ",'" + TextBox4.Text + "' )", cn);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

        disp();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        cmd = new OleDbCommand("update tbl set s_name='" + TextBox2.Text + "',age=" + TextBox3.Text + ",city='" + TextBox4.Text + "' where id=" + TextBox1.Text + " ", cn);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

        disp();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        cmd = new OleDbCommand("delete from tbl where id=" + TextBox1.Text + " ", cn);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

        disp();
    }
}

No comments:

Post a Comment



Asp.net tutorials