[dba-VB] SPAM-LOW: Re: C# Help with binding components

jwcolby jwcolby at colbyconsulting.com
Sun Mar 28 21:31:02 CDT 2010


Gustav,

How is the .FillByCustomerID method created?  This is in fact my "missing piece".

 >         private void FillCustomerEmailAddress()
 >         {
 >             this.customerEmailTableAdapter.FillByCustomerId(this.karnelia.CustomerEmail, 
_customerId);
 >         }


John W. Colby
www.ColbyConsulting.com


Gustav Brock wrote:
> Hi John
> 
> Did you work through the tutorials referred to through the years here?
> 
> You can look up the thread "Old Dog - New Tricks" from January 2008 where Shamil and others posted some excellent links - including this:
> 
>   http://www.asp.net/learn/data-access/ 
> 
> This is an absolute must which will teach you about DataTables and TableAdapters and Data Access Layers (and much more) and lift you off all the SqlClient and SqlCommand stuff found everywhere which is waste of time to deal with except for very special purposes.
> 
> Also, I enjoyed some of the free videos from this site:
> 
>   http://www.learnvisualstudio.net/ 
> 
> The "navigation widget" is the binding navigator. Those buttons you don't use can either be deleted or made invisible.
> 
> To look up a record here is one method I use. 
> ChangeCustomerSelection is called at OnChange of comboBoxCustomer:
> 
> <code>
>        private void ChangeCustomerSelection()
>         {
>             if (this.comboBoxCustomer.SelectedValue != null)
>             {
>                 _customerId = Convert.ToInt32(this.comboBoxCustomer.SelectedValue);
>                 this.FillCustomerEmailAddress();
>             }
>             else
>             {
>                 this.textBoxEmailOrganisation.Text = null;
>             }
>             
>             this.bindingNavigatorSaveItem.Enabled = false;
>         }
> 
>         private void FillCustomerEmailAddress()
>         {
>             this.customerEmailTableAdapter.FillByCustomerId(this.karnelia.CustomerEmail, _customerId);
>         }
> </code>
> 
> Saving a record is done via the table adapter. Here's an example where this.karnelia.CustomerEmail is the dataset updated by the user:
> 
> <code>
> 
>         private void SaveRow()
>         {
>             if (this.bindingNavigatorSaveItem.Enabled)
>             {
>                 // Only save a new media isssue if textBoxMediaId.Text has been properly set.
>                 if (this.textBoxCustomerId.Text.Equals(_customerId.ToString()))
>                 {
>                     bool enableSave =
>                         this.textBoxName.Text.Length > 0 &&
>                         this.textBoxEmailAddress.Text.Length > 0;
>                     if (enableSave)
>                     {
>                         this.Validate(false);
>                         if (FormValidated())
>                         {
>                             this.customerEmailBindingSource.EndEdit();
>                             this.customerEmailTableAdapter.Update(this.karnelia.CustomerEmail);
>                             this.bindingNavigatorAddNewItem.Enabled = true;
>                             // Enable selection of customer.
>                             this.comboBoxCustomer.Enabled = true;
>                         }
>                     }
>                     this.bindingNavigatorSaveItem.Enabled = false;
>                 }
>             }
>         }
> 
>        private bool FormValidated()
>         {
>             // Checks if no error is displayed for validated controls.
>             if (errorProvider1.GetError(textBoxName).Length > 0)
>             {
>                 return false;
>             }
>             if (errorProvider1.GetError(textBoxEmailAddress).Length > 0)
>             {
>                 return false;
>             }
>             return true;
>         }
> 
> </code>
> 
> This uses the ErrorProvider object which it sounds like you have found. Very useful.
> 
> As always with .Net, this is only one method. If you have more elaborate validation tests, you could apply DAL, data access layer. A more novel approach is to apply the Entity Framework.
> 
> /gustav
> 
> 
>>>> jwcolby at colbyconsulting.com 28-03-2010 14:36 >>>
> I want to load one record in a main form - client, product etc.  I want to use a combo which SELECTS 
> the client and on the change event, do some undefined thing to cause that client form to load the 
> record selected with the client id from the combo.
> 
> Seems simple enough.
> 
> Except I am having difficulty understanding which of the binding objects hold what parts of the 
> equation.
> 
> I created a "bound form" which pulls the entire table but displays the current record in individual 
> controls.
> 
> So for starters, now that I have that, how do I set a sql statement to load just one record?  What 
> component does that and what property?
> 
> Next, I have a navigation widget that causes the form to move through the records.  If I only have a 
> single record I do not need that so I want to get rid of it.  I assume it is safe to just delete it?
> 
> Except that object also has the add new / delete and save buttons.  Hmm...
> 
> And finally (for now), have found code to watch the controls for changes and set a dirty flag.  How 
> do I save the record?  What object / property?
> 
> Once I see this done one time I will be good to go but I am not finding code on the web that does 
> this.  Everything assumes loading the entire recordset then filtering down.
> 
> Any assistance greatly appreciated.



More information about the dba-VB mailing list