How to insert and Bind Ms-Access Database Code.

Jayakumars
Posted by Jayakumars under ASP.NET AJAX category on | Points: 40 | Views : 932
hi
here i post the code.How to insert and Bind The Ms-Access Database 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.OleDb;
using System.Data;

public partial class Default6 : System.Web.UI.Page
{
OleDbConnection con;

OleDbCommand com;

string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
protected void BtSubmit_Click(object sender, EventArgs e)
{
try
{
com = new OleDbCommand();
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Test\\Test.mdb';Persist Security Info=False;");
con.Open();
com.Connection = con;
str = "Insert into Table1 values('" + TxtName.Text + "','" + TxtEmpno.Text + "','" + TxtAmount.Text + "')";
com.CommandText = str;
com.ExecuteNonQuery();
con.Close();
Response.Write("Records inserted successfully");
bindgrid();
}

catch (Exception ex)
{

ex.ToString();

}
}

void bindgrid()
{
DataTable dt;
OleDbDataAdapter Olesqlda;
com = new OleDbCommand();
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Test\\Test.mdb';Persist Security Info=False;");
con.Open();
str = "Select * from Table1";
com = new OleDbCommand (str, con);
Olesqlda = new OleDbDataAdapter (com);
dt = new DataTable();
Olesqlda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}

Comments or Responses

Login to post response