Thursday, December 8, 2011

LINQ / Refletion Typed List / Pass values across Form's without Session,Application,Querstring,POST

 

LINQ to SQL

 

using (BaseDataContext context = new BaseDataContext())

            {

                var result = context.GetPublicOfficialMasterData(

                    DatabaseConstants.GovernmentOwnershipEntityValue,

                    DatabaseConstants.SanctionEntityValue,

                    DatabaseConstants.CompanyStatusEntityValue,

                    DatabaseConstants.EmployeeStatusEntityValue,

                    DatabaseConstants.CountryEntityValue);

                List<BaseMasterData> all = result.ToList();

               

                var sanction = from res in result

                               where res.EntityName == DatabaseConstants.SanctionEntityValue

                               select res;

                //masterEntities = (BaseCollection<BaseMasterDataEntity>)result.ToList<BaseMasterDataEntity>();

            }

 

public class BaseDataContext : DataContext

    {

        public BaseDataContext()

            : base(ConfigurationManager.ConnectionStrings["CDD"].ConnectionString) { }

 

        [Function(Name = "dbo.uspGetPublicOfficialMasterData")]

        public ISingleResult<BaseMasterData> GetPublicOfficialMasterData(

            [Parameter(Name = "GovernmentOwnershipEntityName", DbType = "NChar(25)")] string governmentOwnership,

            [Parameter(Name = "SanctionEntityName", DbType = "NChar(25)")] string sanction,

            [Parameter(Name = "CompanyStatusEntityName", DbType = "NChar(25)")] string companyStatus,

            [Parameter(Name = "EmployeeStatusEntityName", DbType = "NChar(25)")] string employeeStatus,

            [Parameter(Name = "CountryEntityName", DbType = "NChar(25)")] string country)

        {

            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())),

                governmentOwnership, sanction, companyStatus, employeeStatus, country);

            return ((ISingleResult<BaseMasterData>)(result.ReturnValue));

        }

    }

 

 

 

Create Typed list via reflection

 

AddMaterEntityByEntityType(ref masterEntities,

                                drMasterData.Select(string.Format(query, DatabaseConstants.CountryEntityValue)),

                                typeof(Country));

 

private void AddMaterEntityByEntityType(ref BaseCollection<BaseMasterDataEntity> masterEntities,

            DataRow[] dataRow, Type type)

        {

            BaseMasterDataEntity entity = default(BaseMasterDataEntity);

            Type typeList = typeof(List<>);

            Type actualType = typeList.MakeGenericType(type);

            var obj = (List<BaseMasterDataEntity>)Activator.CreateInstance(actualType);

 

            foreach (DataRow item in dataRow)

            {

                entity = CreateMasterEntityByType(item["EntityName"].ToString());

                entity.ID = Convert.ToInt32(item["ID"]);

                entity.Description = Convert.ToString(item["Name"]);

                obj.Add(entity);

            }

            masterEntities.AddRange(obj);

        }

 

 

Avoid querstring, session, application, to pass variables across pages

 

Add hidden field on the page

<asp:HiddenField ID="hdnActionOnCode" runat="server" />

 

Set control client ID into session and replace _ with $

public string CodeKey

        {

            get { return ((string)Session[UIConstants.POCodeKey]).Replace('_', '$'); }

            set { Session[UIConstants.POCodeKey] = value; }

        }

 

Modify hidden field value to be passed onto other page before page request

function SetCode(code) {

        var hdnField = document.getElementById('<%= hdnActionOnCode.ClientID %>');

        hdnField.value = code;

    }

 

Access field and value as part of Request.Form.AllKeys

public string POCode {

            get {

                return Request.Form.AllKeys.Contains(CodeKey) ? Request.Form[CodeKey].ToString() : string.Empty;

            }

        }

No comments: