• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • Last Modified:

Checks for Nulls In DataSet Partial Class

Hi,

I would like to remove validation checks I have in my forms and move them to the DataSet partial class, specifically checks for null entries when a user has not completed the fields in the windows form properly. My database columns do not allow null entries and I have been playing around with the setting "AllowDBNull" on the columns in my DataSet. If I set AllowDBNUll to False then as you would expect I get an error in the Windows Form which I can manage with an event handler, but how do I trap this error at DataSet level instead if I have removed the check in the form ?

I have the following code in my partial class:

   partial class DataSet1
    {
      partial class CPTY_COMPANYDataTable
        {
            protected new void OnTableNewRow(System.Data.DataTableNewRowEventArgs e)
            {
                 base.OnTableNewRow(e);
                 if (e.Row.IsNull("CPTY_CODE"))
                 {
                    e.Row.RowError.Equals("Code value is required");
                 }
           }
        }
   }

It doesn't seem to do anything because a Default Error Dialog Window is opened by C#.  I set AllowDBNull to True and now I get an exception returned by the Oracle DB. Do I perhaps need an Event Handler and if so how do I assign it for the partial class ? Also how do I trap the exception and return control back to the windows form, do I use e.Row.RejectChanges(); ?

Help would me much appreciated.
0
adlikon
Asked:
adlikon
  • 2
  • 2
1 Solution
 
aaronfengCommented:
Personally, I would suggest you do the check in the Form when the data was entered.  The purpose of DataSet is to store data, not validating data.  I never done it the way you are trying to, I would suspect it is possible, but you'll end up with more "hacks".  Only push data into DataSet if data was entered correctly.

Personally I'm not a big fan of putting any kind of logic in the Form, I use the MVP pattern: http://www.martinfowler.com/eaaDev/ModelViewPresenter.html 

But no matter what your design is, I feel the stuff you are trying to do doesn't belong in the DataSet.  If you decide to put the validation in the DataSet, you are tightly coupling your DataSet to the Form.  If you ever change DataSet to something else, you'll need to re-do the validation code.  The point is, the data should be independent, so you can change the data and the Form will still work.

That's my 2 cents.

Cheers,

Aaron
http://aaronfeng.blogspot.com
0
 
adlikonAuthor Commented:
Hi,

I spent some more time at msdn.com, and found this link which gives a walkthrough on how to achieve what I wanted. They are advocating the use of the DataSet for validation. http://msdn2.microsoft.com/en-us/library/ms171930(VS.80).aspx. I thought the use of the DataSet in this way was an approach to MVC, is my understanding correct ? Here is my code which works.

     partial class CPTY_COMPANYDataTable
        {
            public override void EndInit()
            {
                base.EndInit();
                CPTY_COMPANYRowChanged += CheckRowChangeEvent;
               
            }

            public void CheckRowChangeEvent(object sender,CPTY_COMPANYRowChangeEvent e)
            {
             if (e.Row.IsNull("CPTY_CODE"))
                {
                    e.Row.SetColumnError("CPTY_CODE", "Value Required");
                }

            }
}

I am interested to know how you would approach this using MVC as you suggested if the use of the DataSet in this way is not best practice. Taking a form called 'X' and the partial class syntax I have shown above, please show how you would implement using MVC, (note I am novice/intermed with C# and ooso please clarify the seprate objects/classe files etc I need. If you can do that and it works I will award you the points.

thanks
0
 
aaronfengCommented:
Personally I like MVP (suggest in my last post) because I feel like it is cleaner.  MVP it is just a spin off MVC, the concepts are very closely related.  In the end, how you want to store your data is irrelavent (DataSet or not) to what pattern you want to use.  In the MVP world, your View is as dumb as possible.  The View will always delegate to your Presenter to coordinate all actions.  

// Example implementation of your View.
public class MyView : UserControl{
  private MyViewPresenter presenter;
  public MyView() {
    // all the usual stuff here
    this.button1.Click += new EventHandler(button1_Click);
  }

  public MyViewPresenter Presenter {
    set {
      this.presenter = value;
      // now the Presenter has the View now
      this.presenter.View = this;
    }
  }

  // Assuming you want to validate data when the user click on the button
  void button1_Click(object sender, EventArgs e) {
    this.presenter.ValidateUserInputData(/* pass your data here */);
  }
}

All the work will happen inside of your Presenter, this way, you can make your code more testable and cleaner.  I typed all the code by hand, so there might be minor syntax errors.

Hope that helps,

Aaron
http://aaronfeng.blogspot.com
0
 
adlikonAuthor Commented:
I like that, thanks a lot
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now