ASP.NET - how to bind this in gridview using ms.access

Asked By Jahir on 31-May-12 06:41 AM

protected void Button1_Click(object sender, EventArgs e)

{

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\maddy\\nifty\\niftydb.mdb;";

OleDbConnection conn = new OleDbConnection(connectionString);

conn.Open();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = conn;

cmd.CommandText =

"INSERT INTO nifty ([Title], [Publisher]) VALUES (@duser, @computer)";

cmd.Parameters.Add(

"@duser", OleDbType.VarChar).Value = TextBox1.Text;

cmd.Parameters.Add(

"@computer", OleDbType.VarChar).Value = TextBox2.Text;

ShowAlertMessage(

"Inserted Successfully");

cmd.ExecuteNonQuery();

conn.Close();

bindgrid();

}

public void bindgrid()

{

//adap = new OleDbDataAdapter("select * from nifty", conn);

ds =

new DataSet();

adap.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

Somesh Yadav replied to Jahir on 31-May-12 07:08 AM

Hi,

use below code to bind it to gridview

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
string sFilePath = Server.MapPath("MSACCESS.mdb");
DataTable dt;
OleDbConnection Conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + ";");

using (Conn)
{
Conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM nifty", Conn);
OleDbDataAdapter oDA = new OleDbDataAdapter(cmd);
dt
= new DataTable();
oDA
.Fill(dt);
}

GridView1.DataSource = dt;
GridView1.DataBind();

}
}
TSN ... replied to Jahir on 31-May-12 07:10 AM

Hi...

i have written the bind grid method for you ...

here it is , try this and let me know any issues

public void bindgrid()
 
{
 
DataTable dt;
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\maddy\\nifty\\niftydb.mdb;";

OleDbConnection conn = new OleDbConnection(connectionString);

using (Conn)
{
Conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM nifty", Conn);
OleDbDataAdapter oDA = new OleDbDataAdapter(cmd);
dt = new DataTable();
oDA.Fill(dt);
}
 
GridView1.DataSource = dt;
 
GridView1.DataBind();
 
}
 

Goniey N (Mr. G) replied to Jahir on 31-May-12 08:14 AM
Try This :

public void bindgrid()

{

      OleDbConnection Conn = null;

      OleDbCommand CMD = null;

      try

      {

        OleDbDataReader dr;

        string selectQuery = "Select * From nifyt";

        Conn.Open();

        CMD = new OleDbCommand(selectQuery, Conn);

        dr = CMD.ExecuteReader();

        GridView1.DataSource = dr;

      }

      catch (Exception ex)

      {

        //Exception Handle....

      }

      finally

      {

        if (CMD != null)

          CMD.Dispose();

        if (Conn != null)

          Conn.Close();

      }

}




Hope this will helpful to you....