CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2002
    Location
    Madrid - Spain
    Posts
    28

    DataGrid does not update mdb file

    I'm first trying to use ADO.NET and C#. I try to connect a simple mdb Daba Base to a DataGrid control, and everything works OK, but any change I make to the table I see is not upodated into the DataBase.
    I've tried both programatically and draging controls. This is the programatic way: Is there something I'm forgotting?

    Thanks!


    System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=C:\Documents and Settings\Rikardo\Mis documentos\GruposUtopia.mdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDBatabase Password="";Jet OLEDB:Engine Type=5;Jet OLEDBatabase Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDBon't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False");

    System.Data.OleDb.OleDbCommand selectComm = new System.Data.OleDb.OleDbCommand("SELECT * FROM ADEUDOS");

    selectComm.Connection = conn;

    System.Data.DataSet ds = new System.Data.DataSet("Tabla de adeudos");

    ds.Tables.Add("ADEUDOS");

    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(selectComm);

    adapter.Fill(ds.Tables["ADEUDOS"]);

    dataGrid1.SetDataBinding(ds.Tables["ADEUDOS"], "");

  2. #2
    Join Date
    Oct 2001
    Posts
    36
    The data table is disconnected from the record source. It cannot update the database because it doesn't know how to connect to it. The connected layer is the data adapter. You will have to call the Update() command for the data adapter.

    *** In order for the Update() method to work you must set the INSERT and UPDATE commands on the data adapter the same way that you did the SELECT command. Or since you are only selecting from one table you could use the OleDbCommandBuilder class to automatical generate the INSERT and UPDATE commands.

    ex.
    // create adapter
    OleDbDataAdapter dadAdeudos = new OleDbDataAdapter();
    // create select command
    dadAdeudos .SelectCommand = new OleDbCommand(sqlAdeudos, conn);
    // use command builder
    OleDbCommandBuilder cbAdeudos = new OleDbCommandBuilder(dadAdeudos );
    // Create Data Set
    DataSet dsAdeudos = new DataSet();
    // Fill Data Set
    dadAdeudos .Fill(dsAdeudos, "ADEUDOS");

    Now bind the table to the grid control and make any modifications to it. When you are ready to update the database....

    // Send updates to database through data adapter
    dadAdeudos .Update(dsAdeudos, "ADEUDOS");

    In order for the Command Builder to work I believe that you must have a primary key defined for the table that the select command is getting the data from.

    I hope that this helps...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured