none
CRUD Example with Dataset, DataRow and Access Mdb RRS feed

  • Question

  • I've created (using a wizzard, don't remember which) a dataset that points to a mdb file (Access 2007)
    The generated DataSet is named "EstacionarDataSet"

    Here's some code I have:

    EstacionarDataSet ed = new EstacionarDataSet();
    DataRow newRow = ed.Tables[0].NewRow();
    newRow["patente"] = tb_Patente.Text;
    newRow["numerobox"] = tb_Box.Text;
    newRow["fechahora_ingreso"] = DateTime.Now;
    ed.Tables[0].Rows.Add(newRow);
    * The above code is executed in SecondForm (not mainform). I'm trying to insert a new record into the only table mdb file has (this code doesn't work, but doesn't give any exception).

    * The mainform contains a System.Windows.Forms.DataGridView component, attached to this mdb file, which works fine, it shows the records correctly, etc. (PS: I added some sample records using Access)

    * I guess the wizzard has created all the necessary code, connectionstring, etc. Project source code is available here
    http://www.mediafire.com/?zwotmgwvzii

    * I'm needing some code samples for adding a new row, updating, and deleting... Thanks
    Beza - Custom Joomla Templates
    Friday, November 13, 2009 11:51 AM

Answers

All replies

  • It is really hard, if not impossible, to give code examples. It depends on many factors and what you want to achive.
    I would recommend to start here, this will contain lots of information on how to create, fill, update etc. when working with DataSets.

     "Using DataSets in ADO.NET" 
    http://msdn.microsoft.com/en-us/library/ss7fbaez(VS.80).aspx

    HTH
    //Michael
    This posting is provided "AS IS" with no warranties.
    • Marked as answer by Yichun_Feng Thursday, November 19, 2009 4:29 AM
    • Unmarked as answer by Enrique HTC S710 Thursday, November 19, 2009 12:29 PM
    Friday, November 13, 2009 12:34 PM
  • Thanks
    I'll check the link provided...
    Beza - Custom Joomla Templates
    Friday, November 13, 2009 7:06 PM

  • The links below will show you how to use ADO.NET with access Database. especially Insert, Update Delete

    Part One
    http://www.devcity.net/Articles/215/1/article.aspx


    Part Two
    http://www.devcity.net/Articles/240/1/article.aspx

    Part Three
    http://www.devcity.net/Articles/301/1/article.aspx
    John
    • Marked as answer by Yichun_Feng Thursday, November 19, 2009 4:29 AM
    Friday, November 13, 2009 7:22 PM
  • Hi John
    The tutorials are great, but I'm not using OleDb
    I've generated the project using Visual Studio 2008, and the IDE generated automatically all the code, etc.
    I see the project requires the following libraries(?):

    System.Data
    System.Data.DataSetExtensions

    Also, I can set DataGridView's DataSource property at designTime. The value is "myMDBTableName BindingSource"

    I've been doing research, and found that this piece of code works great when doing an update to a record contained in the DataGridView

    int nCurrentRow = dataGridView1.CurrentCellAddress.Y;
    DataRow dr;
    dr = estacionarDataSet.Estacionamiento.Rows[nCurrentRow];
    DateTime fechaEgreso = DateTime.Now;
    dr["fechahora_egreso"] = fechaEgreso;
    dr["importe"] = duracion.TotalHours * Int32.Parse(tb_PrecioHora.Text);
    estacionamientoTableAdapter.Update(dr);
    this.dataGridView1.EndEdit();
    this.estacionamientoTableAdapter.Fill(estacionarDataSet.Estacionamiento);
    this.dataGridView1.Refresh();

    Now, I only have to find out some code for deleting a record from the table/grid... I'll post in case I find it
    Itexa http://www.itexa.com.ar Custom Web Applications
    Thursday, November 19, 2009 1:01 PM
  • if your datagridview is bound to the datatable, when you perform delete on the datagridview it will delete the record automatically in the datatable.

    All you need next is to send back your deletion to the database.


    John
    Thursday, November 19, 2009 2:37 PM