Welcome to Tech-Review.Org Sign in | Join | Help

.net_2.0

My coding blog entries. Typically will either be more complex coding examples or overcoming product issues / troubleshooting resolutions.
Generics continued - SQL Parm builder within Parent -> Child relationships

In a previous entry titled:  Building SQL Queries Automatically in C# using Generics - I spent alot of time composing a basic parameter builder so that I could createado commands for sql on the fly.  Naturally all of this was originally developed because I was revamping a form to be used in a ModalPopUp.  What makes this form different - is that it consists of 10 different user controls - each of which can be used as a standalone form.

So, in my quest not to replicate data - I wanted to leverage Generics even more by allowing only a single <List> to be passed from child form to parent form But not have to replicate the code in the parent for saving data.

For instance in a parent form you have several child controls.

Normally if you were to have a save button on parent form you would have to traverse all of the child controls to get your form values. Remember, the parent form saves everything into the database as a complete record but the child forms can update the database entries with only their respective data values they are assigned...

For instance.

 

Child 1 has a txtName, txtPassword, txtEmail fileds.

Child 2 has txtAddres1, txtAddress2, txtState etc..

If the Parent wants to get the values then it has to do :

parameters.AddWithValue("@name", child1.txtNameText,...);

parameters.AddWithValue("@name", child1.txtPassword.Text....);

parameters.AddWithValue("@name", child2.txtAddress.Text,...);

Now that really gets frustrating...Using the model I presented in the previous blog post, we can now do this for the parent:

 private void UpdateData()
        {
 

             //define the update parms
              List<ParmInfo2> UPDATE_PARMS = new List<ParmInfo2>();


                 UPDATE_PARMS.AddRange( child1.UPDATE_PARMS);
   UPDATE_PARMS.AddRange( child2.UPDATE_PARMS);

...do the rest of the where parms building and pass to the globalDal...


In the child controls then you create the following:


public class Child1

        public List<ParmInfo2> UPDATE_PARMS
        {
            get
            {
                List<ParmInfo2> _UPDATE_PARMS = new List<ParmInfo2>();

        
                    _UPDATE_PARMS.Add(PARMBUILDER.MATCH_SQL_PARM("name", txtName.Text, null));
                    _UPDATE_PARMS.Add(PARMBUILDER.MATCH_SQL_PARM("password",txtPassword.Text, null));
              ....add remaining
                    return _UPDATE_PARMS;
            }
        }

 

public class Child2


        public List<ParmInfo2> UPDATE_PARMS
        {
            get
            {
                List<ParmInfo2> _UPDATE_PARMS = new List<ParmInfo2>();

        
                    _UPDATE_PARMS.Add(PARMBUILDER.MATCH_SQL_PARM("address1", txtAddress.Text, null));
                    _UPDATE_PARMS.Add(PARMBUILDER.MATCH_SQL_PARM("address2",txtAddress.Text, null));
             ...add remaining..
                    return _UPDATE_PARMS;
            }
        }

 

Now instead of mapping the parent save parameters explicitly we just pass a <List> back from the Child form which means way less management of code.  For doing updates where you only want to sent fields that changed you can do this:

 

        public List<ParmInfo2> UPDATE_PARMS
        {
            get
            {
                List<ParmInfo2> _UPDATE_PARMS = new List<ParmInfo2>();

                if (PageHeader != Section.PageHeader || currentID == 0)
                {
                    _UPDATE_PARMS.Add(PARMBUILDER.MATCH_SQL_PARM("pageHeader", PageHeader, null));
                }
                if (PageFooter != Section.PageFooter || currentID == 0)
                {
                    _UPDATE_PARMS.Add(PARMBUILDER.MATCH_SQL_PARM("pageFooter", PageFooter, null));
                }
                if (Footer != Section.Footer || currentID == 0)
                {
                    _UPDATE_PARMS.Add(PARMBUILDER.MATCH_SQL_PARM("footer", Footer, null));
                }
                    return _UPDATE_PARMS;
            }
        }

 

No need to check anything on the parent form as the only values being passed are those that need to be updated.   Handy if you have a form with 40+ form inputs and the user only changes 4 form values.  Why pass all 40 when you can pass only the 4.  Saves on amount of traffic to the backend and again - makes extending forms even easier....

 

 

Posted: Tuesday, February 13, 2007 12:39 AM by Jody

Comments

No Comments

New Comments to this post are disabled