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();