Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. update excel using oledb (is primary key possible)

update excel using oledb (is primary key possible)

Scheduled Pinned Locked Moved ASP.NET
designtutorialquestionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amittinku
    wrote on last edited by
    #1

    Hi, I bind the gridview to excel in disconnected mode. works fine. MAke changes to gridview data. changes happen and reflected in dataset. Try to update the dataset using adapter (to transfer the changes to same excel in connected mode). commandbuilder thorws exception as it is not able to find a primary key in excel sheet. How to do it? public partial class excel_excel_disconnmode : System.Web.UI.Page { OleDbConnection oo; OleDbDataAdapter adp; DataSet ds; protected void Page_Load(object sender, EventArgs e) { oo = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ASP1\\Book1.xlsx;Extended Properties=Excel 12.0"); adp = new OleDbDataAdapter("select ID from [Sheet1$]", oo); if (!IsPostBack) { ds = new DataSet(); ViewState["ds"] = ds; adp.Fill(ds, "sheet1"); GridView1.DataSource = ds; GridView1.DataBind(); } else { ds = (DataSet)ViewState["ds"]; GridView1.DataSource = ds; } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; // turn to edit mode GridView1.DataBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int pos = e.RowIndex; ds.Tables[0].Rows[pos].Delete(); GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int pos = e.RowIndex; string name = ((TextBox)GridView1.Rows[pos].Cells[0].Controls[0]).Text; ds.Tables[0].Rows[pos][0] = name; GridView1.EditIndex = -1; GridView1.DataBind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; GridView1.DataBind(); } //to trasnfer the changes from disconnected to connected excel protected void Button1_Click(object sender, EventArgs e) { adp = new OleDbDataAdapter("select ID from [Sheet1$]", oo); OleDbCommandBuilder xx = new OleDbCommandBuilder(adp); adp.Update(ds, "sheet1"); } }

    M 1 Reply Last reply
    0
    • A amittinku

      Hi, I bind the gridview to excel in disconnected mode. works fine. MAke changes to gridview data. changes happen and reflected in dataset. Try to update the dataset using adapter (to transfer the changes to same excel in connected mode). commandbuilder thorws exception as it is not able to find a primary key in excel sheet. How to do it? public partial class excel_excel_disconnmode : System.Web.UI.Page { OleDbConnection oo; OleDbDataAdapter adp; DataSet ds; protected void Page_Load(object sender, EventArgs e) { oo = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ASP1\\Book1.xlsx;Extended Properties=Excel 12.0"); adp = new OleDbDataAdapter("select ID from [Sheet1$]", oo); if (!IsPostBack) { ds = new DataSet(); ViewState["ds"] = ds; adp.Fill(ds, "sheet1"); GridView1.DataSource = ds; GridView1.DataBind(); } else { ds = (DataSet)ViewState["ds"]; GridView1.DataSource = ds; } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; // turn to edit mode GridView1.DataBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int pos = e.RowIndex; ds.Tables[0].Rows[pos].Delete(); GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int pos = e.RowIndex; string name = ((TextBox)GridView1.Rows[pos].Cells[0].Controls[0]).Text; ds.Tables[0].Rows[pos][0] = name; GridView1.EditIndex = -1; GridView1.DataBind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; GridView1.DataBind(); } //to trasnfer the changes from disconnected to connected excel protected void Button1_Click(object sender, EventArgs e) { adp = new OleDbDataAdapter("select ID from [Sheet1$]", oo); OleDbCommandBuilder xx = new OleDbCommandBuilder(adp); adp.Update(ds, "sheet1"); } }

      M Offline
      M Offline
      mylogics
      wrote on last edited by
      #2

      make any one of the column as primary key in access table in which u want to make updates...

      A 1 Reply Last reply
      0
      • M mylogics

        make any one of the column as primary key in access table in which u want to make updates...

        A Offline
        A Offline
        amittinku
        wrote on last edited by
        #3

        I have to make change in EXCEL, not ACCESS. SO how to define a primary key in Excel? Or any other way to update the excel?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups