Simple Way To Use A DataTable

When I normally create a datatable, I use quite a few lines of code just to add columns and input data into my DataTable.  But recently I have been using the following method use a DataTable in my code. The following function validates RadionButtonLists that I have in my page by passing the RadioButtonList size (rblSize) and RadioButtonList name (rblName).

//Function: Validate RadioButtonList for each question group
bool GroupValidateRBL(int rblSize, string rblName)
{
    bool validateOutput = true;
    int counter = 1;
   
    //Create DataTable object
    DataTable dt = new DataTable();

    //Add columns to DataTable
    dt.Columns.Add("No");
    dt.Columns.Add("RadioButtonName");
    dt.Columns.Add("Checked");

    while (counter <= rblSize)
    {
        ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
        RadioButtonList rbl = (RadioButtonList)cph.FindControl(rblName + counter);

       //Add Rows to Datatable
       dt.Rows.Add(new string[] { counter.ToString(), rblName, rbl.SelectedIndex.ToString() });
       counter++;
    }

    //Iterate through all rows in the DataTable to find all RadioButtonLists that have not been selected
    foreach (DataRow dr in dt.Rows)
    {
        if (Convert.ToInt32(dr["Checked"]) == -1)
        {
            validateOutput = false;
            break;
        }
    }
   return validateOutput;
} 

The DataTable in the function above stores a list of RadioButtonList entries and then iterates through each row in the DataTable to see if they have been selected. The data is entered into the DataTable by the following line:

dt.Rows.Add(new string[] { counter.ToString(), rblName, rbl.SelectedIndex.ToString() });

Looping Through RadioButtonLists

At the moment I am working on a Survey that contains tonnes of RadioButtonList controls that users will use to respond to the numerous questions I have on the website. All was going well until I thought about how I would find a quick and efficient way to enter all the RadioButtonList entries into my SQL database without having to do the following:

_rblBLL.InsertResults(1, rblist1.SelectedValue);
_rblBLL.InsertResults(2, rblist2.SelectedValue);
_rblBLL.InsertResults(3, rblist3.SelectedValue);
...
...
...
...

(InserResults() is my BLL function)

As you can see above, this is indeed a long winded way of inserting all my RadioButtonList entries to the database.

But I found a better way by using the FindControl() method:

for (int i=1; i < questionNo; i++)
{      
    RadioButtonList rbl = (RadioButtonList)this.FindControl("rblist" + i);

    if(rbl != null)
    {
         _rblBLL.InsertResults(i, rbl.SelectedValue);
    }
} 

As you can see, I created a for loop that will iterate through all the RadioButtonLists by incrementing the end value of RadioButtonList ID. So you will have to adopt a naming convention that has a number at the end as shown I have used above.

Last Post Of The Year

logo_bullguard Well this is my last post of 2007. Even though I have not made many posts since I started this blog, hopefully next year will give me more time to pad this blog out.

Since I have created this blog entry, I might as well talk about something. Well a day or two ago my Bullguard Internet Security had expired and even though I have been meaning to purchase a new yearly license, it was not on my priority list. Normally, when your Internet Securty expires the only thing that you cannot do is get up-to-date virus definitions, which is not that much of a deal since your computer is still protected.

However, what I found with Bullguard was that the Virus Scanner,  Firewall, and Spam Filter was locked until I purchased my new yearly license. I found this to be quite annoying since I had to find an alternative Internet Security suite until my new version of Bullguard was posted.

This is only a minor grudge from a great piece of Intenet Security though!

Oh yeah! Happy New Year!!!