Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having a slight problem here..I have two drop down lists called as division and name...So when i click on a particular division from the division drop down list all the employee names belonging to that division gets accumulated in the name drop down list.Now what I want to do is once I select a particular name from the dropdown list I want the employees designation to be displayed in a label..Below is the code I tried...I dont know If i have to enable autopostback for both the dropdown lists....I have enabled Auto post back for Division drop down list...Currently The label is only showing the first employees designation....Hoping you could help me out a bit here :)...Thanx in advance

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            Getdata();

        }

    }

    protected void ddlgroup_SelectedIndexChanged(object sender, EventArgs e)
    {

        int DivisionID = Convert.ToInt32(ddlname.SelectedValue.ToString());

        Fillnames(DivisionID);

        ddlname.SelectedIndex = 0;

    }

    protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
    {

        int designationID = Convert.ToInt32(ddlState.SelectedValue.ToString());

        Filldesignation(designationID);

    }



    private void Getdata()

    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "SELECT DivisionID,Division_name FROM emp_personal";

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

        dAdapter.Fill(objDs);

        con.Close();

        if (objDs.Tables[0].Rows.Count > 0)

        {

            ddlgroup.DataSource = objDs.Tables[0];

           ddlgroup.DataTextField = "Division_name";

            ddlgroup.DataValueField = "DivisionID";

            ddlgroup.DataBind();

           ddlgroup.Items.Insert(0, "--Select--");

       }
    }
    

    private void Fillnames(int divisionID)

    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();
	   cmd.Connection = con;

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "SELECT DivisionID, Empname FROM emp_personal WHERE DivisionID =@CountryID";

        cmd.Parameters.AddWithValue("@DivisionID", divisionID);

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

        dAdapter.Fill(objDs);

        con.Close();

        if (objDs.Tables[0].Rows.Count > 0)

        {

            ddlname.DataSource = objDs.Tables[0];

            ddlname.DataTextField = "EmpName";

            ddlname.DataValueField = "DivisionID";

           ddlname.DataBind();

            ddlname.Items.Insert(0, "--Select--");

        }

       

    }



    private void  Filldesignation(int designationid)

    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(strConn);

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "SELECT DesignationID,designation_name,EmpName FROM emp_personal WHERE DesignationID =@DesignationID";

        cmd.Parameters.AddWithValue("@DesignationID", designationid);

        DataSet objDs = new DataSet();

        SqlDataAdapter dAdapter = new SqlDataAdapter();

        dAdapter.SelectCommand = cmd;

        con.Open();

       lbldes.Text=cmd.ExecuteScalar().Tostring();

        con.Close();

       

        }
}
Posted
Updated 29-Jul-13 4:40am
v3
Comments
Member 10066916 29-Jul-13 10:35am    
thank you for the update Orcun Iyigun
ZurdoDev 29-Jul-13 10:37am    
Is your code executing? No, then set autopostback.
Member 10066916 29-Jul-13 10:39am    
yes...I now i have enabled autopostback for both the dropdownlists....but still its not giving me the right value in the label....
ZurdoDev 29-Jul-13 10:43am    
Step through the code and see what is happening.
Member 10066916 29-Jul-13 10:45am    
ok ill try that again....thanx :)

This outline of your code works for me: both DropDownLists are AutoPostBack=true but not for the label.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        string[] div = { "a", "b", "c", "d" };
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                GetData();
            }
        }

        private void GetData()
        {
            foreach (var d in div)
            {
                this.ddlgroup.Items.Add(d);
            }
        }

        protected void ddlgroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.ddlgroup.SelectedIndex;
            FillNames(index);
            this.ddlname.SelectedIndex = 0;
        }

        private void FillNames(int index)
        {
            if (index < 0 || index >= div.Length) return;
            this.ddlname.Items.Clear();
            for (int i = 0; i < 5; i++)
            {
                this.ddlname.Items.Add(div[index] + i.ToString());
            }

        }

        protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = this.ddlname.SelectedItem.Text;
            this.lblOut.Text = "Selected Employee: " + name;
        }
    }
}
 
Share this answer
 
Comments
Member 10066916 30-Jul-13 2:55am    
thank you ...I will try this out
Member 10066916 31-Jul-13 13:05pm    
I enabled the autopostback true for the group dropdownlist...but when i enable the autopostback for name dropdownlist...it doesnt let me select any other values in the name dropdownlist...it directly selects the first value and displays the first employee's designation...whereas when I disable the autopostback for the name dropdownlist it doesnt trigger any value for the label...where am i goin wrong?can anyone help me out.

Below is my C# code that i changed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.Configuration;


public partial class Forms__form : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Server=SOFTDEV1; Database=2903_SOFTDEV; Integrated security = true");
protected void Page_Load(object sender, EventArgs e)
{
DropDownList7.Visible = false;

if (!IsPostBack)
{

Getdata();//adding the division to the dropdown box


}
}



/* Adding The contents to the Division DropDown List */

private void Getdata()
{
SqlCommand cmd = new SqlCommand("SELECT DivisionID, DivShort FROM M_Division ORDER BY DivShort", conn);
DataSet objDs = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
conn.Open();
sd.Fill(objDs);
conn.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
ddlgroup.DataSource = objDs.Tables[0];
ddlgroup.DataTextField = "DivShort";
ddlgroup.DataValueField = "DivisionID";
ddlgroup.DataBind();
ddlgroup.Items.Insert(0, "--Select--");
}
}

/*Adding the names to the Name DropDownList based on the selected division from Division Drop Down List*/

private void FillName(int divisionID)
{
SqlCommand cmd = new SqlCommand("SELECT DivisionID, EmpName FROM M_Emp_Personal WHERE DivisionID =@DivisionID AND IsActive=1 ORDER BY EmpName", conn);
cmd.Parameters.AddWithValue("@DivisionID", divisionID);
DataSet objDs = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
conn.Open();
sd.Fill(objDs);
conn.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
ddlname.DataSource = objDs.Tables[0];
ddlname.DataTextField = "EmpName";
ddlname.DataValueField = "DivisionID";
ddlname.DataBind();
ddlname.Items.Insert(0, "--Select--");
}

}

/*Selecting the division present in the dropdownlist*/

protected void ddlgroup_SelectedIndexChanged1(object sender, EventArgs e)
{
int DivisionID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
FillName(DivisionID);
ddlname.SelectedIndex = 0;
}

/*Selecting the name present in the dropdownlist*/
protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
{
int DesingID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
FillDesig(DesingID);

}

private void FillDesig(int desingID)
{
SqlCommand cmd = new SqlCommand("Select M_Designation.DesigLong,M_Emp_Personal.EmpName From M_Designation inner join M_Emp_Personal on M_Designation.DesigID=M_Emp_Personal.DesigID", conn);
cmd.Parameters.AddWithValue("@DivisionID", divisionID);
DataSet objDs = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
conn.Open();
sd.Fill(objDs);
conn.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
lbldes.Text=objDs.Tables[0].Rows[0]["designation_name"].tostring();

}

}
Replace Filldesignation with this method.Think it helps you
C#
 private void  Filldesignation(int designationid)
 
    {
 
        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 
        SqlConnection con = new SqlConnection(strConn);
 
        SqlCommand cmd = new SqlCommand();
 
        cmd.Connection = con;
 
        cmd.CommandType = CommandType.Text;
 
        cmd.CommandText = "SELECT DesignationID,designation_name,EmpName FROM emp_personal WHERE DesignationID =@DesignationID";
 
        cmd.Parameters.AddWithValue("@DesignationID", designationid);
 
        DataSet objDs = new DataSet();
 
        SqlDataAdapter dAdapter = new SqlDataAdapter();
 
        dAdapter.SelectCommand = cmd;

 dAdapter.Fill(objDs); 

if(objDs.Tables[0].Rows.count>0)
{
 
       lbldes.Text=objDs.Tables[0].Rows[0]["designation_name"].tostring();
 

}
       
 
        }
}



------------------->If you still not able to sort it out please check query once
 
Share this answer
 
v2
Comments
Member 10066916 30-Jul-13 2:56am    
Thank you...i will try this out :)
Member 10066916 31-Jul-13 13:03pm    
I enabled the autopostback true for the group dropdownlist...but when i enable the autopostback for name dropdownlist...it doesnt let me select any other values in the name dropdownlist...it directly selects the first value and displays the first employee's designation...whereas when I disable the autopostback for the name dropdownlist it doesnt trigger any value for the label...where am i goin wrong?can anyone help me out.

Below is my C# code that i changed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.Configuration;


public partial class Forms__form : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Server=SOFTDEV1; Database=2903_SOFTDEV; Integrated security = true");
protected void Page_Load(object sender, EventArgs e)
{
DropDownList7.Visible = false;

if (!IsPostBack)
{

Getdata();//adding the division to the dropdown box


}
}



/* Adding The contents to the Division DropDown List */

private void Getdata()
{
SqlCommand cmd = new SqlCommand("SELECT DivisionID, DivShort FROM M_Division ORDER BY DivShort", conn);
DataSet objDs = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
conn.Open();
sd.Fill(objDs);
conn.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
ddlgroup.DataSource = objDs.Tables[0];
ddlgroup.DataTextField = "DivShort";
ddlgroup.DataValueField = "DivisionID";
ddlgroup.DataBind();
ddlgroup.Items.Insert(0, "--Select--");
}
}

/*Adding the names to the Name DropDownList based on the selected division from Division Drop Down List*/

private void FillName(int divisionID)
{
SqlCommand cmd = new SqlCommand("SELECT DivisionID, EmpName FROM M_Emp_Personal WHERE DivisionID =@DivisionID AND IsActive=1 ORDER BY EmpName", conn);
cmd.Parameters.AddWithValue("@DivisionID", divisionID);
DataSet objDs = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
conn.Open();
sd.Fill(objDs);
conn.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
ddlname.DataSource = objDs.Tables[0];
ddlname.DataTextField = "EmpName";
ddlname.DataValueField = "DivisionID";
ddlname.DataBind();
ddlname.Items.Insert(0, "--Select--");
}

}

/*Selecting the division present in the dropdownlist*/

protected void ddlgroup_SelectedIndexChanged1(object sender, EventArgs e)
{
int DivisionID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
FillName(DivisionID);
ddlname.SelectedIndex = 0;
}

/*Selecting the name present in the dropdownlist*/
protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
{
int DesingID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
FillDesig(DesingID);

}

private void FillDesig(int desingID)
{
SqlCommand cmd = new SqlCommand("Select M_Designation.DesigLong,M_Emp_Personal.EmpName From M_Designation inner join M_Emp_Personal on M_Designation.DesigID=M_Emp_Personal.DesigID", conn);
cmd.Parameters.AddWithValue("@DivisionID", divisionID);
DataSet objDs = new DataSet();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
conn.Open();
sd.Fill(objDs);
conn.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
lbldes.Text=objDs.Tables[0].Rows[0]["designation_name"].tostring();

}

}
venkateshCST 2-Aug-13 1:59am    
Set AppendDataBoundItems=true for the dropdown name and add item with text as "Select" & value as "0" to dropdown from code behind.It may help you..

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