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

Am getting output only which is its Item default firstone, am not geting employee details other department for example...in dropdown first item is Testing(Department)...when i change other department also the output will be the same Testing (Department).

pls someone help me to resolve this issue...

pls find the code below...

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd = new SqlCommand();
    
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("Data Source=.;Initial Catalog=Employee;Integrated Security=True");
        cmd.CommandText = "SELECT Department_Name FROM Department_Master";
        cmd.Connection = conn;
        conn.Open();
        DropDownList1.DataSource = cmd.ExecuteReader();
        DropDownList1.DataTextField = "Department_Name";
        DropDownList1.DataBind();
        conn.Close();

        string sqlquery = "SELECT Employee_Master.Employee_Name, Employee_Master.Date_of_join, DATEDIFF(day, Employee_Master.Date_of_join, GETDATE()) AS TotalWorkedDays FROM Department_Master INNER JOIN Employee_Master ON Department_Master.Department_ID = Employee_Master.Department_ID";
        conn = new SqlConnection("cn");
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sqlquery, conn);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        conn.Close();
        
    }
    
    
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        
        
        string sqlquery = "SELECT Employee_Master.Employee_Name, Employee_Master.Date_of_join, DATEDIFF(day, Employee_Master.Date_of_join, GETDATE()) AS TotalWorkedDays FROM Department_Master INNER JOIN Employee_Master ON Department_Master.Department_ID = Employee_Master.Department_ID where Department_Name = '" + DropDownList1.SelectedItem + "'";
        conn = new SqlConnection("cn");
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sqlquery, conn);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        conn.Close();
    

        

    }
}



Regards,

Sara
Posted
Updated 7-Jul-11 0:13am
v2
Comments
Prerak Patel 7-Jul-11 6:13am    
Use pre tags for code.
Prerak Patel 7-Jul-11 6:22am    
pls dnt use txtspc whil psting q.
Don't use text speech here.

Try using DropDown Selected Value changed event


and on page load do this

<pre lang="midl">conn = new SqlConnection("Data Source=.;Initial Catalog=Employee;Integrated Security=True");
        cmd.CommandText = "SELECT * FROM Department_Master";
        cmd.Connection = conn;
        conn.Open();
        DropDownList1.DataSource = cmd.ExecuteReader();
        DropDownList1.DataTextField = "Department_Name";
DropDownList1.DataValueField="deptID";
        DropDownList1.DataBind();
        conn.Close();



In Selectd Value changed try this ==>
string sqlquery = "SELECT Employee_Master.Employee_Name, Employee_Master.Date_of_join, DATEDIFF(day, Employee_Master.Date_of_join, GETDATE()) AS TotalWorkedDays FROM Department_Master INNER JOIN Employee_Master ON Department_Master.Department_ID = Employee_Master.Department_ID where DeptID="+Dropdownlist.selectedValue;
        conn = new SqlConnection("cn");
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sqlquery, conn);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        conn.Close();
 
Share this answer
 
Hi,

Put PageLoad code into
SQL
if (!IsPostBack)
       {}
 
Share this answer
 
Sara,

In the code you defined the connection "conn", and you opened the connection in
page_load
event, so that no need to open it again in
DropDownList1_SelectedIndexChanged1
. So you can remove this following code from DropDownList1_SelectedIndexChanged1 event

MIDL
conn = new SqlConnection("cn");
       conn.Open()


and, the above code is not a best practice to bind grid in the event "DropDownList1_SelectedIndexChanged1" use "selectedvaluechanged" or "selectedindexchanged" event

If you want to assign values to dropdownlist box

DropDownList1.DataSource = cmd.ExecuteReader();
                uiCboIntType.DisplayMember = "Employee_Name";
                uiCboIntType.ValueMember = "Employee_Id";


Now you can write code in
DropDownList1_selectedvaluechanged

I think it is helpful to you

Sridhar Patnayak
 
Share this answer
 
v3
Comments
saroblr1 8-Jul-11 5:08am    
I got it....Thanks to everyone...

Keey helping....


Regards,
Sara...

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