ASP.NET database Connectivity with ms Access
ASP.NET database Connectivity with ms Access
Database name – cust
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
using System.Data;
usingSystem.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
public OleDbConnectioncon;
public OleDbDataAdapter dad;
public OleDbCommandcom;
public DataSet ds;
//Code for Search Data
protected voidButton3_Click(object sender, EventArgs e)
{
TextBox2.Text = ""; TextBox3.Text = ""; TextBox4.Text = "";
con= newOleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Server.MapPath("~/App_Data/customer.accdb")+";Persist Security Info=True");
con.Open(); String data1;
data1 ="select *from cust where cid=" +TextBox1.Text;
dad =newOleDbDataAdapter (data1 ,con);
ds = newDataSet(); dad.Fill(ds);
if( ds.Tables[0].Rows.Count>=1)
{
Label2.Text="Record Found";
TextBox2.Text=ds.Tables[0].Rows[0]["name"].ToString();
TextBox3.Text=ds.Tables[0].Rows[0]["course"].ToString();
TextBox4.Text=ds.Tables[0].Rows[0]["contact"].ToString();
}
else
{
Label1.Text="Record Not Found";
}
con.Close ();
}
// Code For update Data
protected voidButton2_Click(object sender, EventArgs e)
{
con = newOleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/App_Data/customer.accdb") + ";Persist Security Info=True");
con.Open();
Stringdata1;
data1 ="update cust set name='"+TextBox2.Text+"',course='"+TextBox3.Text+"',contact='"+TextBox4.Text+"' where cid="+TextBox1.Text;
com = newOleDbCommand(data1, con);
com.ExecuteNonQuery();
Label1.Text = "Data has been Updated";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
// Code for Inert Data
protected voidButton1_Click(object sender, EventArgs e)
{
con = newOleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/App_Data/customer.accdb") + ";Persist Security Info=True");
con.Open();
Stringdata1;
data1 ="insert into cust values("+TextBox1.Text+",'"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";
com=newOleDbCommand (data1 ,con);
com.ExecuteNonQuery ();
con.Close();
Label1.Text="Insert Data Successfully";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
} // Code for Delete Data From Database table
protected voidButton4_Click(object sender, EventArgs e)
{
con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/App_Data/customer.accdb") + ";Persist Security Info=True");
con.Open();
Stringdata1;
data1 = "delete from cust where cid=" + TextBox1.Text;
com=newOleDbCommand (data1 ,con);
com.ExecuteNonQuery ();
con.Close();
Label1.Text="Delete Data Successfully";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}}
Comments
Post a Comment