Database connectivity in asp.net using ms access
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; // adding
namespace
using System.Data.OleDb; //
Adding namespace
public partial class _Default :
System.Web.UI.Page
{
public
OleDbConnection con; // declare public variable con
public
OleDbDataAdapter dad; // declare public
variable dad
public
OleDbCommand com; // declare public
variable com
public
DataSet ds;
// declare public variable ds
//Code
for Search Data
protected
void Button3_Click(object
sender, EventArgs e)
{
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
con= new
OleDbConnection ("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 =new
OleDbDataAdapter (data1 ,con);
ds = new
DataSet();
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
void Button2_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();
String
data1;
data1 ="update cust set name='"+TextBox2.Text+"',course='"+TextBox3.Text+"',contact='"+TextBox4.Text+"' where cid="+TextBox1.Text;
com = new OleDbCommand(data1, con);
com.ExecuteNonQuery();
Label1.Text = "Data has been Updated";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
// Code
for Inert Data
protected
void Button1_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();
String
data1;
data1 ="insert into cust values("+TextBox1.Text+",'"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";
com=new
OleDbCommand (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
void Button4_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();
String
data1;
data1 = "delete from cust where cid=" +
TextBox1.Text;
com=new
OleDbCommand (data1 ,con);
com.ExecuteNonQuery ();
con.Close();
Label1.Text="Delete Data Successfully";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
}



No comments: