Databinding problem

Hi everybody.

I have a form which contains a user control.
When the form loads, I'm populating some DataSet, and assigning the dataset refernece to one of the control properties.

The control contains other control that are bound to various paths on that dataset, adding new rows, modifying, etc.

Now, I want to update the changes back to the database with dataadapters.
I can put a Update button on the control itself, or in the form containing it.
When I call the adapter's Update method in the context of the form (i.e. in response to form's button click), the db doesn't update.
When the same thing happens at the control, it updates as well.

It is important to mention, that both the form and the control contains the reference to the same dataset, and the ds contains the exaxct same information in both cases. (the rows has the same state, etc.)

.
Any help would be appriciated.

Thanks,
Yaakov


Answer this question

Databinding problem

  • grumpy3b

    I don't want to remove the dtp, and I don't care if it won't repro.

    From what I see tilll now, is as somebody wrote upon, "databinding is the devil".
    You don't have any control or idea when or why things happen. There is no documentation, too.

    Thanks a lot for youe help. However, it seems I would need to replace the data model.

    Yaakov

  • kasperstorm

    I understand your frustration as data binding is complex and takes time to understand.  On the other hand, our data binding model is extremely powerful and flexible.  I'd recommend you take some time and try and understand how the model works - the links below may help.  In my opinion, it will take you far less time to understand how the model works than it will to create your own.

    http://support.microsoft.com/default.aspx scid=kb;en-us;Q313482
    http://samples.gotdotnet.com/quickstart/winforms/doc/winformsdata.aspx
    http://www.code-magazine.com/Article.aspx quickid=0409051
    http://www.15seconds.com/issue/040614.htm

    Joe

  • Leif Greenman

    The same Update is done.

    Here is the code. Sorry abbout the length, I didn't find a way do upload the files.



    public partial class RequestEditor : UserControl
     {
      BudgetDataSet dataSource;


      public BudgetDataSet DataSource
      {
       get { return dataSource; }
       set
       {
        if (value == null)
         throw new ArgumentNullException("dataSoucre");
        dataSource = value;
        BindControls();
       }
      }

      public int RowIndex
      {
       get { return requestBindingSource.Position; }
       set
       {
        if (dataSource == null)
         throw new ArgumentException("DataSource is not defined");
        requestBindingSource.Position = value;
       }
      }

      public RequestEditor()
      {
       InitializeComponent();
       dtpDate.Value = DateTime.Now;
       dgvDetails.AutoGenerateColumns = false;
      }
      public void AddNew()
      {
       requestBindingSource.AddNew();
       dtpDate.Value = DateTime.Now;
      }
      public void Save()
      {
      
       requestBindingSource.EndEdit();

       //***************************************
       //-- When I do it here, the data updates as well, perfectly
    //I'm commmenting the update lines in the form when I uncommenting this, of course.

       //new RequestTableAdapter.Update(DataSource.Request);
       //new RequestDetailTableAdapter().Update(DataSource.RequestDetail);
       //new RequestTargetsTableAdapter().Update(DataSource.RequestTargets);

      }
      public void Cancel()
      {
       requestBindingSource.CancelEdit();
      }

      private void BindControls()
      {
      
       applicantsBindingSource.DataSource = DataSource;
       productBindingSource.DataSource = DataSource;
       targetBindingSource.DataSource = DataSource;
       requestBindingSource.DataSource = DataSource;

      }

     }


     partial class RequestForm : Form
     {

      RequestEditor re;   
      public RequestForm()
      {
       InitializeComponent();
       re = new RequestEditor();
       re.Dock = DockStyle.Fill;

       Controls.Add(re);
      }

      private void RequestEditForm_Load(object sender, EventArgs e)
      {
       budgetDataSet.EnforceConstraints = false;
       this.productTableAdapter.Fill(this.budgetDataSet.Product);
       this.targetTableAdapter.Fill(this.budgetDataSet.Target);
       this.applicantsTableAdapter.Fill(this.budgetDataSet.Applicants);

       re.DataSource = budgetDataSet;
       re.AddNew();
      }

      protected override void OnClosing(CancelEventArgs e)
      {

       re.Save(); //calls requestBindingSource.EndEdit() on re.

    //------ here it doesn't work, only the Request row is updated, partially.
    //In partially I mean that not all control values are returned to the dataSource.


      new RequestTableAdapter.Update(budgetDataSet.Request);
       new RequestDetailTableAdapter().Update(budgetDataSet.RequestDetail);
       new RequestTargetsTableAdapter().Update(budgetDataSet.RequestTargets);
      
      }
     }


     


  • slimbim

    If you're passing in the DataSet reference, it shouldn't matter.  I assuming your doing the same Update command in both cases

    Can you post sample code the demonstrates the issue

    Thanks,

    Joe

  • Semyon P

    Well, I have no dobut on your tries to help.
    So, please tell me how can I upload the files. You can look at them and tell me what is wrong.


    Yaakov

  • MetroP

    I'm really sorry, you're right.
    I mixed up things in the copy-paste proccess.
    I fixed the attached code, so please have a second look.

    In addition, here is some more details:
    In the requestEditor, I have few controls bound to the current (or new) Request row (like DateTimePicker, text box and more), a datagridView bound to RequestDetails binding source (which is in turn bound to the FK_RequestDetails_Request relation), and a Listbox bounded to RequestTargets bindingsource, which is another 'slave' table of Request.



    Thanks again, and sorry for the english.. (I'm not an american).
    Yaakov

  • RussellReed

    Log a bug at the link below and attach a sample - respond to this post with the bug ID (or URL to the bug) and I'll take a look.

    Thanks,

    Joe

    http://lab.msdn.microsoft.com/vs2005/


  • David Broman

    I'm sorry, but those links are totaly useless. In fact, you insulted me with them. (well, almost).
    I know the basic implementatuin, features, and the new "cool stuff in .Net 2.0", and that is the only thing those articles describe. I read them, and more.

    None of them is explaining what's going on behind the scenes, and it seems nobody knows.

    When it comes to simple behaviors, databinding is great. When the app goes a little bit different from MS in-mind, things goes mad. Yes, Drag-Once (and mess with it forever).

    I wonder if MS guys ever wrote a real world Win-app using their own tools.

    Yaakov

  • deval bhavsar

    I don't see anything obvious however I'm guessing it has something to do with how/when the data is committed or exceptions being thrown during the commit.  Does is still repro if you scale back the Request Editor to just the RequestRow   If so, remove the DateTimePicker and see if it still repros

    Joe


  • ryanlcs

    Sorry, I'm not sure I'm following.  In the original post you imply the Form save path doesn't work but in the code sample above, you imply it does so I'm not clear on what is/isn't working.  Can you scale the sample down to just the Request part (or some other table that doesn't work) and re-iterate the problem with just that part   Also, can you describe the scenario - are you binding to a DataGridView in RequestEditor and are the changes you make not persisted   Or is it just the last change that's not persisted

    Thanks,

    Joe

  • mr pitiful

    OK, it's unfortunate you feel that way.  I am an "MS guy" and I've written real world data applications using our tools.  In addition, I meet with customers on a regular basis that use our tools and runtime to build real-world applications.  I also know a lot about what goes on behind the scenes and I'm trying to help you.  It's unfortunate you've misunderstood my attempts at helping you.

    Our design time and runtime is not perfect nor is it easy to understand. There are conceptual issues you'll need to overcome to understand how it works.  I'm trying to work with you to help you overcome those conceptual hurdles.

    Joe

  • Databinding problem