databinding and updating sample

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant

    databinding and updating sample

    I need some sample code showing how to manipulate data in my access database
    using C#. This is what Im trying to do:

    Dropdownlist with datagrid both bound to datasource. When the drop down list
    value changes - the datagrid values change accordingly. I managed to get
    this working by setting the currencymanager to the dropdownlist
    selectedIndex (which is probably the wrong way to do it, bu the datagrid
    seems to update its values correctly)

    Im stuck with the Updatecommand, deleteCommand part of things. I set my
    update comand to something like this "update Something set somethingElse = ?
    WHERE this = ? AND that = ?" I get an error message with the update.

    Please opint me in the direction of smoe sample code. My internet connection
    runs like a 3 legged dog and I get too frustrated waiting for the inevitable
    internet timeout.

    Thanks heaps,
    Grant


  • Emma Middlebrook

    #2
    Re: databinding and updating sample

    Hi,

    What is your datasource? Is it a dataset or datatable that you have
    filled with the contents of your database table?

    If you use a dataset you can edit the data locally in your dataset and
    then commit the changes to the database from the dataset.

    Comment

    • Grant

      #3
      Re: databinding and updating sample

      Hi thanks for the reply,
      I believe its both. Here is my code which I cannot get to update, it throws
      the error: "No value given for one or more required parameters"

      ------------------Code start------------------------------
      private void OpenDataset()
      {
      //Connection string
      string connection = @"Provider=Micr osoft.Jet.OLEDB .4.0; "+
      @"Data Source=C:\stock .mdb";

      // Setup DB-Connection
      OleDbConnection conn = new OleDbConnection (connection);

      //SQL Select string
      //string query = "SELECT * FROM [Component-Codes]";

      // Fill the Dataset with Compnent details, map Default Tablename
      // "Table" to "Components ".
      adapter = new OleDbDataAdapte r("SELECT * FROM [Component-Codes]",conn);
      adapter.TableMa ppings.Add("Tab le","Components ");
      adapter.Fill(ds et);

      // Fill the Dataset with Compnent details, map Default Tablename
      // "Table" to "Products".
      adapter2 = new OleDbDataAdapte r("SELECT * FROM [Product-Codes]",conn);
      adapter2.TableM appings.Add("Ta ble","Products" );
      adapter2.Fill(d set);

      // Fill the Dataset with Compnent details, map Default Tablename
      // "Table" to "components ".
      adapter3 = new OleDbDataAdapte r("SELECT * FROM
      [Product-Component-Junction]",conn);

      //---------------Test area------------------------
      adapter3.Update Command = new OleDbCommand("U PDATE
      [Product-Component-Junction] SET Quantity = ? " +
      "WHERE [Component-Code] = ? AND [Product-Code] = ?" , conn);

      //Add Parameters and set values.
      //adapter3.Update Command.Paramet ers.Add("@Count ry",OleDbType.V arChar,
      15).Value = "UK";

      //selectCMD.Param eters.Add("@Cou ntry", OdbcType.VarCha r, 15).Value =
      "UK";
      //selectCMD.Param eters.Add("@Cit y", OdbcType.VarCha r, 15).Value =
      "London";


      //adapter3.Update Command = new OleDbCommand("U PDATE
      [Product-Component-Junction] SET Quantity = ? " , conn);
      //---------------Test area------------------------

      adapter3.TableM appings.Add("Ta ble","prod-comp-junction");
      adapter3.Fill(d set);

      // Establish the Relationship "ProdComp"
      // between Products ---< prod-comp-junction
      System.Data.Dat aRelation ProdComp;
      System.Data.Dat aColumn col_Products;
      System.Data.Dat aColumn col_JunctionTab le_Prods;
      col_Products = dset.Tables["Products"].Columns["Code"];
      col_JunctionTab le_Prods =
      dset.Tables["prod-comp-junction"].Columns["Product-Code"];
      ProdComp = new System.Data.Dat aRelation("Prod Comp",col_Produ cts,
      col_JunctionTab le_Prods);
      dset.Relations. Add(ProdComp);

      // Establish the Relationship "ProdComp"
      // between Components ---< prod-comp-junction
      System.Data.Dat aRelation Component_Produ ct_Junction;
      System.Data.Dat aColumn col_Components;
      System.Data.Dat aColumn col_JunctionTab le;
      col_Components = dset.Tables["Components "].Columns["Code"];
      col_JunctionTab le =
      dset.Tables["prod-comp-junction"].Columns["Component-Code"];
      Component_Produ ct_Junction = new
      System.Data.Dat aRelation("Comp onent_Product_J unction",col_Co mponents,col_Ju nctionTable);
      dset.Relations. Add(Component_P roduct_Junction );

      dsView = dset.DefaultVie wManager;

      // Grid Databinding
      dataGrid1.DataS ource = dsView;
      dataGrid1.DataM ember = "Products.ProdC omp";

      //--------------------Combobox---------------
      comboBox1.DataS ource = dset.Tables["Products"];
      comboBox1.Displ ayMember = "Code";
      comboBox1.Value Member = "Products.Code" ;

      //--------------------Combobox---------------

      }

      private void openDataSet()
      {
      string connection = @"Provider=Micr osoft.Jet.OLEDB .4.0; "+
      @"Data Source=C:stock. mdb";
      string query = "SELECT * FROM [Component-Codes]";

      OleDbConnection conn = new OleDbConnection (connection);
      OleDbDataAdapte r adapter = new OleDbDataAdapte r();
      adapter.SelectC ommand = new OleDbCommand(qu ery, conn);
      adapter.Fill(ds et);

      }

      private void BindControls()
      {
      comboBox1.DataS ource = dset.Tables["Table"];
      comboBox1.Displ ayMember = "Code";

      }


      private void comboBox1_Selec tedIndexChanged (object sender,
      System.EventArg s e)
      {

      CurrencyManager cm =
      (CurrencyManage r)this.BindingC ontext[dsView,"Product s"];
      cm.Position = comboBox1.Selec tedIndex;
      }

      private void comboBox1_Bindi ngContextChange d(object sender, EventArgs e)
      {
      CurrencyManager cm =
      (CurrencyManage r)this.BindingC ontext[dset,"Products"];
      cm.Position = cm.Count + 1;

      }

      private void btnUpdate_Click (object sender, System.EventArg s e)
      {
      try
      {
      //adapter.Update( dset);
      //adapter2.Update (dset);
      adapter3.Update (dset);
      }
      catch(Exception exc)
      {
      catchError(exc) ;
      }
      }
      -----------------------Code end-----------------------------------

      "Emma Middlebrook" <emzyme20@hotma il.com> wrote in message
      news:1138291117 .044360.63060@g 14g2000cwa.goog legroups.com...[color=blue]
      > Hi,
      >
      > What is your datasource? Is it a dataset or datatable that you have
      > filled with the contents of your database table?
      >
      > If you use a dataset you can edit the data locally in your dataset and
      > then commit the changes to the database from the dataset.
      >[/color]


      Comment

      • Emma Middlebrook

        #4
        Re: databinding and updating sample

        Have you tried using the OleDbCommandBui lder to help you construct the
        other statements? That might help you eliminate the possibility that
        your SQL syntax might be wrong..?

        The error seems to think that you have not supplied enough parameters
        for the update..

        Here's a sample from msdn on how it works:

        OleDbConnection myConn = new OleDbConnection (myConnection);
        OleDbDataAdapte r myDataAdapter = new OleDbDataAdapte r();
        myDataAdapter.S electCommand = new OleDbCommand(my SelectQuery,
        myConn);
        OleDbCommandBui lder cb = new OleDbCommandBui lder(myDataAdap ter);

        myConn.Open();

        DataSet ds = new DataSet();
        myDataAdapter.F ill(ds, myTableName);

        //code to modify data in DataSet here

        //Without the OleDbCommandBui lder this line would fail
        myDataAdapter.U pdate(ds, myTableName);

        myConn.Close();

        return ds;

        Comment

        Working...