Wednesday, March 12, 2014

Dynamically Column name Add in DropdownList for searching....



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Redhawk.Signal.Newsletter.Data;

namespace Redhawk.Signal.Newsletter.Web.Pages.Modal
{
    public partial class Search : System.Web.UI.Page
    {
        string tableName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {            
                tableName = Request.QueryString["tableName"];
                if(!string.IsNullOrEmpty(tableName))
                {
                    this.LoadSearchDropdownList(tableName);
                }
            }
        }

        private void LoadSearchDropdownList(string tableName)
        {
            List<Column> lst = new List<Column>();
            var _dbCon = new RedhawkSignalNewsletterEntities();
            var ds = from t1 in _dbCon.Database.SqlQuery<Column>("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME ='" + tableName.ToString() + "'" )

// var ds = from t1 in _dbCon.Database.SqlQuery<Column>("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'Templates'")

                     select t1;
            if (tableName == "Templates")
            foreach (var m in ds)
            {
                if (m.COLUMN_NAME == "TemplateName" || m.COLUMN_NAME == "TemplateBody" || m.COLUMN_NAME == "TemplateDescription")
                    lst.Add(m);
            }
            else if (tableName == "fund")
            {
                foreach (var m in ds)
                {
                    if (m.COLUMN_NAME == "FundName" || m.COLUMN_NAME == "FundDescription")
                        lst.Add(m);
                }
            }
            ddlSearch.DataSource = lst.ToList();
            ddlSearch.DataBind();
        }
    }

    public class Column
    {
        public string COLUMN_NAME { get; set; }
    }
}

No comments:

Post a Comment