presonal use Dynamically bind dropdownlist

test.aspx ---------------------------------------- test.asx.cs using MaaShardaTech.Common; public partial class test : System.Web.UI.Page { CommonFun objcf = new CommonFun(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { objcf.SetDoctrTypeToDDL(ddlDoctorsType); } } } ------------------------------------- in App_Code Folde CommonFun.cs namespace MaaShardaTech.Common { public class CommonFun { object[] args; public CommonFun() { // // TODO: Add constructor logic here // } public void SetDoctrTypeToDDL(DropDownList ddlc) { DoctorTypesDAL objCoun = new DoctorTypesDAL(); ddlc.Items.Clear(); ddlc.DataSource = objCoun.GetAllDoctorTypes().DefaultView; ddlc.DataTextField = "DoctorType"; ddlc.DataValueField = "DoctorTypeID"; ddlc.DataBind(); ddlc.Items.Insert(0, new ListItem("Select", "0")); } } } ------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- public partial class PO_Registration : BasePage { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDropDownList(); } } public void BindDropDownList() { GetDataForDropDownList(DropdownListProject, "projName", "projCode", "Project"); } public void GetDataForDropDownList(DropDownList ddl, string strTextField, string strValueField, string strQueryType) { DataSet ds = null; } } ---------------------------------------------------------------------------------------------------------------------------------------------- use for multilayer ------------------------------------ public partial class Customer_PO_Add : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindItemName(); } } public void BindItemName() { MaashardaTech.DynamicDropDownBind.obj_DynamicDropDown.FillDropDown(MaashardaTech.DynamicDropDownBind.obj_DynamicDropDown.FillItemName(), ddlItemName, "ItemName", "ItemId", "Select Item"); } } ----------------------------------------- DynamicDropDownBind.cs namespace MaashardaTech { public class DynamicDropDownBind { public static MaashardaTech.DynamicDropDownBind obj_DynamicDropDown= new DynamicDropDownBind(); public void FillDropDown(DataSet ds, DropDownList ddl, string textfield, string valuefield, string defaultfield) { if (ds != null && ds.Tables[0].Rows.Count > 0) { ddl.DataSource = ds; ddl.DataTextField = textfield; ddl.DataValueField = valuefield; // ddl.SelectedValue = selectvalue; ddl.DataBind(); } // ddl.Items.Insert(0, defaultfield); ddl.Items.Insert(0, new ListItem(defaultfield,"0")); //ddl.SelectedIndex = 0; } public DataSet FillItemName() { DataSet DataSetState = BusinessLayer.BusinessLayer_EmployeeRegistrationDetail.Instance.getItemName(); if (DataSetState != null && DataSetState.Tables != null) { return DataSetState; } else return null; } } } ------------------------------- BusinessLayer_EmployeeRegistrationDetail.cs namespace BusinessLayer { public class BusinessLayer_EmployeeRegistrationDetail { public static BusinessLayer.BusinessLayer_EmployeeRegistrationDetail Instance = new BusinessLayer_EmployeeRegistrationDetail(); public DataSet getItemName() { return DataLayer.DataLayer_EmployeeRegistrationDetail.Instance.getItemName(); } } } -------------------------------------------- namespace DataLayer { public class DataLayer_EmployeeRegistrationDetail { public static DataLayer_EmployeeRegistrationDetail Instance = new DataLayer_EmployeeRegistrationDetail(); public DataSet getItemName() { string strQuery = "select ItemId, ItemName from MeriTech_ItemMaster where IsActive=1"; return DataLayer_GeneralMethods.Instance.FillDataSet(strQuery); } } } -------------------------------- DataLayer_GeneralMethods.cs namespace DataLayer { public class DataLayer_GeneralMethods { public static DataLayer_GeneralMethods Instance = new DataLayer_GeneralMethods(); public DataSet FillDataSet(string strQuery) { string strConnectionString = DataLayer_ConnectionString.ActiveConnectionString; SqlConnection con = new SqlConnection(strConnectionString); SqlCommand cmd = new SqlCommand(); DataSet dsData = new DataSet(); cmd.Connection = con; cmd.CommandTimeout = 5000; cmd.CommandType = CommandType.Text; cmd.CommandText = strQuery; try { con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dsData); } catch (Exception ex) { dsData = null; } finally { con.Close(); con.Dispose(); cmd.Dispose(); } return dsData; } } } ----------------------------------------------------- DataLayer_ConnectionString.cs using System.Data.SqlClient; namespace DataLayer { public class DataLayer_ConnectionString { public static DataLayer_ConnectionString Instance = new DataLayer_ConnectionString(); public static SqlConnection _ConnectionString; public DataLayer_ConnectionString() { } public static string ActiveConnectionString { get { return System.Configuration.ConfigurationManager.ConnectionStrings["conResourceTracker"].ConnectionString.ToString(); } } public SqlConnection con_Open() { SqlConnection con = new SqlConnection(); try { con = new SqlConnection(ActiveConnectionString.ToString()); if (con.State == ConnectionState.Closed) { con.Open(); } } catch (Exception ex) { } return con; } public void con_Close(SqlConnection con) { try { if (con.State == ConnectionState.Open) { con.Close(); con.Dispose(); } } catch (Exception ex) { } } } } -----------------------------------------------------------------------------------------------------------------------------

         

 if (!IsPostBack)   
            {
 String SC1 = ConfigurationManager.ConnectionStrings["AspConnectionString"].ConnectionString;
            string SqlQueery1 = "select CityId, CityName, CountryName from TblCity";
            using (SqlConnection con = new SqlConnection(SC1))
            {
                SqlCommand cmd = new SqlCommand(SqlQueery1, con);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPart17B.DataSource = dr;
                ddlPart17B.DataTextField = "CityName"; 
                ddlPart17B.DataValueField = "CityId";  
                ddlPart17B.DataBind();
               
            }
}
---------------------------------------------------------------------------------------------------------------------------------------

        
        
        
        
        

#region Part 22 In Page Load
          
            if (!IsPostBack)
            {
               ddlPart22Continet.DataTextField = "ContinenetName";
               ddlPart22Continet.DataValueField = "ContinenetId";
               ddlPart22Continet.DataSource = GetData("Pro_TblContinent", null);
               ddlPart22Continet.DataBind();
               ListItem liContinent = new ListItem("-Select Continnent-", "-1");
               ddlPart22Continet.Items.Insert(0, liContinent);

               ListItem liCountry = new ListItem("-Select Country-", "-1");
               ddlPart22Country.Items.Insert(0, liCountry);
               ddlPart22Country.Enabled = false;

               ListItem liCity = new ListItem("-Select City-", "-1");
               ddlPart22City.Items.Insert(0, liCity);
               ddlPart22City.Enabled = false;
            #endregion

            }



 #region Part 22 Outside Of Page Load
        //--------------------------Part 22------------------------------------
        private DataSet GetData(string Pro_Name, SqlParameter Pro_Parameter)//since three store procedure and call again and again, which increase traffic so we write common method to call all store procedure
        {
            string Sc = ConfigurationManager.ConnectionStrings["AspConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(Sc);
            SqlDataAdapter da = new SqlDataAdapter(Pro_Name, con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            if (Pro_Parameter != null)
            {
                da.SelectCommand.Parameters.Add(Pro_Parameter);
            }
            DataSet Ds = new DataSet();
            da.Fill(Ds);
            return Ds;


            //Or   When Use Blow Line through an error   Invalid attempt to call FieldCount when reader is closed.  why?

            //SqlCommand da = new SqlCommand(pro, con);
            //da.CommandType = CommandType.StoredProcedure;
            //if (param != null)
            //{
            //    da.Parameters.Add(param);
            //}
            //con.Open();
            //return da.ExecuteReader();
        }

        protected void ddlPart22Continet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlPart22Continet.SelectedIndex == 0)
            {
                ddlPart22Country.SelectedIndex = 0;
                ddlPart22Country.Enabled = false;
                ddlPart22City.SelectedIndex = 0;
                ddlPart22City.Enabled = false;
            }
            else
            {
                ddlPart22Country.Enabled = true;
                SqlParameter SqlPara = new SqlParameter("@ContinetId", ddlPart22Continet.SelectedValue);
                DataSet ds = GetData("Pro_GetCountryByContenetId", SqlPara);
                ddlPart22Country.DataTextField = "CountryName";
                ddlPart22Country.DataValueField = "CountryId";
                ddlPart22Country.DataSource = ds;
                ddlPart22Country.DataBind();
                ListItem liCountry = new ListItem("-Select- Country-", "-1");
                ddlPart22Country.Items.Insert(0, liCountry);
                ddlPart22City.SelectedIndex = 0; 
                ddlPart22City.Enabled = false;
            }
        }

        protected void ddlPart22Country_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlPart22Country.SelectedIndex == 0)
            {
                ddlPart22City.SelectedIndex = 0;
                ddlPart22City.Enabled = false;
            }
            else
            {
                ddlPart22City.Enabled = true;
                SqlParameter SqlPara = new SqlParameter("@CountryId", ddlPart22Country.SelectedValue);
                DataSet ds = GetData("Pro_GetCityByCountryId", SqlPara);
                ddlPart22City.DataTextField = "CityName";
                ddlPart22City.DataValueField = "CityId";
                ddlPart22City.DataSource = ds;
                ddlPart22City.DataBind();
                ListItem liCity = new ListItem("-Select City-", "-1");
                ddlPart22City.Items.Insert(0, liCity);
            }
        }
        //End Of Part 22
        #endregion