SIGN UP MEMBER LOGIN:    
ARTICLE

Filling Data objects using Reflection

Posted by Sridhar Subramanian Articles | C# Language October 13, 2008
This article describes about filling a "Data class" using a generic method .
Reader Level:

This article describes about filling a "Data class" using a generic method. Reflection is used to find property of any passed class dynamically  and assign the value. The below approach will be very userful in large applications where hunderds of classes has to be filled through out the flow.

Consider you have a data class called  "Person" which contains below listed properties
{Name, Address, City, State, Country} . If this data object has to be  filled the usual way is to get a datareader or dataset and assign the values directly.

But this new strategy iterates through each and every property of any passed object and assigns value from datareader automatically.

UI:
***

        Person person = new Person();

        DAL dal = new DAL();

        //Pass object as a parameter

        dal.ExecuteDataReader(person, "a");

        Response.Write("Name: " + person.Name + "<br>");

        Response.Write("Address: " + person.Address + "<br>");

        Response.Write("City: " + person.City + "<br>");

        Response.Write("State: " + person.State + "<br>");

        Response.Write("Country: " + person.Country + "<br>");

DAL
***

    /// <summary>

    /// ExecuteClass is used to fill all properties any passed  data object

    /// Author :Sridhar  Subramanian

    /// </summary>

    /// <param name="objectClass">Object which has to be filled</param>

    /// <param name="parameter1">SQL query parameter,you can use SP </param>

    public void ExecuteClass(object objectClass, string parameter1)

    {

        try

        {

            sqlConnection = new SqlConnection(@"Server=SridharDEV;Database=EntLibTest;Uid=Test;Pwd=Test;");

            sqlConnection.Open();

            sqlCommand = new SqlCommand("SELECT * FROM PERSON WHERE NAME='" + parameter1 + "'", sqlConnection);

            sqlDataReader = sqlCommand.ExecuteReader();

            Type theType = objectClass.GetType();

            PropertyInfo[] p = theType.GetProperties();

            object[] custAttr = null;

            while (sqlDataReader.Read())

            {

                foreach (PropertyInfo pi in p)

                {

                    try

                    {

                        custAttr = pi.GetCustomAttributes(true);

                        if ((sqlDataReader[pi.Name] != System.DBNull.Value) && (custAttr.Length == 0))

                            pi.SetValue(objectClass, sqlDataReader[pi.Name], null);

                    }

                    catch (System.IndexOutOfRangeException) { }

                }

            }

           

        }

        catch (Exception ex)

        {

            throw ex;

        }

        finally

        {

            if (sqlDataReader.IsClosed == false)

                sqlDataReader.Close();

            if (sqlConnection.State == ConnectionState.Open)

                sqlConnection.Close();

            sqlConnection.Dispose();

        } 
 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor