locked
Dynamically calling a TableAdapter's Update method RRS feed

  • Question

  • Hi,
    Uses: VS 2005; Oracle 9i, ODP.NET;

    I would like to revoke a topic which was discussed earlier (Source URL: http://www.developmentnow.com/g/36_2008_3_0_0_1052002/Dynamically-calling-a-TableAdapters-Update-method.htm) under above topic name.

    In this, the User would like to have a functionality to update tableadapter collection as described in a pseudocode:

    foreach(Component tableAdapt in TableAdapterCollection)
    {
    tableAdapt.Update(...); <----- This can't happen because the items
    stored in the collection are type of Components and can't cast them to TableAdapters
    }

    I've created my base class to accomodate this:
    NB: _oDaPrimary is where I'm creating a collection of TableAdapters
    1 public partial class GenericBusinessForm : EquityBroker32.com.jksb.GenericBaseForm  
    2     {  
    3         private DataSet _oDsPrimary = null;  
    4         private System.ComponentModel.Component[] _oDaPrimary;  
    5         private BindingSource _oBsPrimary;  
    6         private string[] _sKeyDBFields;  
    7         private System.Windows.Forms.Control[] _oWinControl;  
    8  
    9  
    10         public BindingSource PrimaryBindingSource  
    11         {  
    12             set { _oBsPrimary = value; }  
    13         }  
    14           
    15         public System.Windows.Forms.Control[] KeyControl  
    16         {  
    17             set { _oWinControl = value; }  
    18         }  
    19  
    20         public string[] KeyFields  
    21         {  
    22             set { _sKeyDBFields = value; }  
    23         }  
    24  
    25         public System.ComponentModel.Component[] PrimaryDataAdapter  
    26         {  
    27             set { _oDaPrimary = value; }  
    28         }  
    29  
    30         public DataSet PrimaryDataSet  
    31         {  
    32             set { _oDsPrimary = value; }  
    33         }  
    34  
    35         public GenericBusinessForm()  
    36         {  
    37             InitializeComponent();  
    38         }  
    39  
    40         protected void FormSave()  
    41         {  
    42             try 
    43             {  
    44                 this.Validate();  
    45                 for (int i = 0; i < _oDaPrimary.Length; i++)  
    46                 {  
    47                     (this._oDaPrimary[i] as DBDataAdapter).Update(this._oDsPrimary);  
    48                 }  
    49                 this._oDsPrimary.AcceptChanges();  
    50                 MessageBox.Show("Done");  
    51             }  
    52             catch (Exception ex)  
    53             {  
    54                 this._oDsPrimary.RejectChanges();  
    55                 throw ex;  
    56             }  
    57               
    58         }  
    59  
    60         protected void OnKeyFieldLostFocus(object sender, System.EventArgs e)  
    61         {  
    62             int pos = 0;  
    63             if ((sender is TextBox) || (sender is MaskedTextBox))   
    64             {  
    65                 string s = (sender as TextBoxBase).Text;  
    66                 if (!String.IsNullOrEmpty(s) && (this._oBsPrimary != null))  
    67                 {   
    68                     pos = this._oBsPrimary.Find(_sKeyDBFields[0].ToString().ToUpper(), (sender as TextBoxBase).Text);  
    69  
    70                     if (pos > 0)  
    71                     {  
    72                         this._oBsPrimary.CancelEdit();  
    73                         this._oBsPrimary.Position = pos;  
    74                     }  
    75                     else 
    76                     {  
    77                         this._oBsPrimary.CancelEdit();  
    78                         this._oBsPrimary.AddNew();  
    79                     }  
    80                 }  
    81             }  
    82         }  
    83  
    84         protected void SetKeyDBControl()  
    85         {  
    86             for (int i = 0; i < _oWinControl.Length; i++)  
    87             {  
    88                 _oWinControl[i].LostFocus += new System.EventHandler(OnKeyFieldLostFocus);  
    89             }  
    90         }        
    91     } 

    What my concerns are that if I took the collection of TableAdapters as System.ComponentModel.Component, then VS Says that "Update" method is not available and if I took it as DBDataAdapter then things Build without errors but does not get updated without giving any errors.

    Any suggestions?

    Thanks & Regards,

    Hifni



    Hifni Shahzard Nazeer M.
    Thursday, March 19, 2009 7:24 AM