Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Scenario:
I have 4 different dropdownlists for different purposes.
1st to get the Employee names and email addresses.
"DATATEXTFIELD = Detail(emp_code, Empnames) and DATAVALUEFIELD = email"

2nd to get the Employee authority names.
"DATATEXTFIELD = Empnames and DATAVALUEFIELD = emp_code"

3rd to get the Employee names only.
"DATATEXTFIELD = Detail(emp_code, Empnames) and DATAVALUEFIELD = emp_code"

4th to get the Priority Levels(LOW,NORMAL,HIGH,CRITICAL) only.
"DATATEXTFIELD = priorflag_desc and DATAVALUEFIELD = priorflag_id"

All I need to create a single method in another class(EXEMPCLASS.CS) and to call this method in my main class(ACCOUNT.CS) to use for every dropdownlist...
Currently I am using this thing without overloading method.
HOW TO CREATE A PERFECT SINGLE METHOD FOR ALL THESE DROPDOWNS..

here is my code..


C#
 protected void Page_Load(object sender, EventArgs e)
    {        
        if (!Page.IsPostBack)
        {
            if (HttpContext.Current.Session["empcode"] != null)
            {
                getauthoptions(Session["empcode"].ToString()); // here getting the current LOGGED IN PERSON from the session to get the authority member above him from the database.
            }
            prioritydatabinding();
            empemailbind();
            popdata();
        }
    }

protected void popdata()
    {
        DataTable adp = new DataTable();
        OracleConnection dbConn = Conn.getConnection();
        string query = "select emp_code, (empname '-' emp_code)detail from TABLE1";
        OracleCommand cmd = new OracleCommand(query, dbConn);
        dbConn.Open();
        OracleDataAdapter dr = new OracleDataAdapter(cmd);
        dr.Fill(adp);
        DropDownList4.DataSource = adp;
        DropDownList4.DataTextField = "detail";
        DropDownList4.DataValueField = "emp_code";
        DropDownList4.DataBind();
        dbConn.Close();
    }
    protected void empemailbind()
    {
            DataTable adp3 = new DataTable();
            OracleConnection dbConn = Conn.getConnection();        
            string query3 = "select email, (empname '-' emp_code)detail from TABLE1";
            OracleCommand cmd3 = new OracleCommand(query3, dbConn);
            dbConn.Open();
            OracleDataAdapter dr3 = new OracleDataAdapter(cmd3);
            dr3.Fill(adp3);
            DropDownList2.DataSource = adp3;
            DropDownList2.DataTextField = "detail";
            DropDownList2.DataValueField = "email";
            DropDownList2.DataBind();
            dbConn.Close();
       
    }
    protected void prioritydatabinding()
    {
        DataTable adp2 = new DataTable();
        OracleConnection dbConn = Conn.getConnection();
        string query2 = "select priorflag_id, priorflag_desc from TABLE2";
        OracleCommand cmd2 = new OracleCommand(query2, dbConn);
        dbConn.Open();
        OracleDataAdapter dr2 = new OracleDataAdapter(cmd2);
        dr2.Fill(adp2);
        priority0.DataSource = adp2; // priority0(DROPDOWNLIST)
        priority0.DataTextField = "priorflag_desc";
        priority0.DataValueField = "priorflag_id";
        priority0.DataBind();
        dbConn.Close();
    }
    protected void getauthoptions(string empcode)
    {
        DataTable adp4 = new DataTable();
        OracleConnection dbConn = Conn.getConnection();
        string query4 = "select EMP_CODE, olphrm.get_name(emp_code) empname from TABLE1, TABLE3 where EMP_CODE = AUTHPARENT_ID and USER_ID=" + empcode + " order by 2";
        OracleCommand cmd4 = new OracleCommand(query4, dbConn);
        dbConn.Open();
        OracleDataAdapter dr4 = new OracleDataAdapter(cmd4);
        dr4.Fill(adp4);
        DropDownList3.DataSource = adp4;
        DropDownList3.DataTextField = "empname";
        DropDownList3.DataValueField = "emp_code";
        DropDownList3.DataBind();
        dbConn.Close();
    }


PLEASE HELP.....Code is working fine as dropdowns are getting filled perfectly but want to use this in a single Overload method.

Thanks.
Posted
Updated 12-May-14 1:15am
v3
Comments
ArunRajendra 18-Sep-13 1:42am    
When you are talking about overloading there should be commonality between your methods. In this case the required information is all different. I feel you current approach is more reasonable than overloading method.
AR547 18-Sep-13 2:26am    
and what if I have to use these methods from another class file. Actually I dont want to show the SQL Queries in main class...it should contains only methods.

1 solution

You can create one method for dropdownlist binding and one method which gets data for dropdownlist as mentioned below :

C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               this.BindData();
           }
       }

       private void BindData()
       {
           DropDownList4.DataSource = this.GetData(1);
           DropDownList4.DataTextField = "detail";
           DropDownList4.DataValueField = "emp_code";
           DropDownList4.DataBind();

           DropDownList2.DataSource = this.GetData(2);
           DropDownList2.DataTextField = "detail";
           DropDownList2.DataValueField = "email";
           DropDownList2.DataBind();

           priority0.DataSource = this.GetData(3); // priority0(DROPDOWNLIST)
           priority0.DataTextField = "priorflag_desc";
           priority0.DataValueField = "priorflag_id";
           priority0.DataBind();

           DropDownList3.DataSource = this.GetData(4);
           DropDownList3.DataTextField = "empname";
           DropDownList3.DataValueField = "emp_code";
           DropDownList3.DataBind();
       }

       private DataTable GetData(int type)
       {
           DataTable adp = new DataTable();

           var query = string.Empty;

           switch (type)
           {
               case 1:
                   query = "select emp_code, (empname '-' emp_code)detail from TABLE1";
                   break;
               case 2:
                   query = "select email, (empname '-' emp_code)detail from TABLE1";
                   break;
               case 3:
                   query = "select priorflag_id, priorflag_desc from TABLE2";
                   break;
               case 4:
                   if (HttpContext.Current.Session["empcode"] != null)
                   {
                       query = "select EMP_CODE, olphrm.get_name(emp_code) empname from TABLE1, TABLE3 where EMP_CODE = AUTHPARENT_ID and USER_ID=" +
                   Session["empcode"].ToString() + " order by 2";
                   }

                   break;
           }

           if (query != string.Empty)
           {
               OracleConnection dbConn = Conn.getConnection();
               OracleCommand cmd = new OracleCommand(query, dbConn);
               dbConn.Open();
               OracleDataAdapter dr = new OracleDataAdapter(cmd);
               dr.Fill(adp);
               dbConn.Close();
           }

           return adp;
       }
 
Share this answer
 
Comments
AR547 18-Sep-13 3:44am    
Thanks Alot dude...its working fine...but minor issue faced in case#4....so i created another method for it.... Thanks again ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900