Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every body I have a asp.net with sql server 2008 data base . I want to select text of dropdownlist and Fill details in GridView and labels based on that but when I select an item of selected text it does not Fill details in GridView and labels notice that my drop downlist is list of months.please help me
my code:


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 tbbt : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["emp_dbConnectionString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            BindEmpGrid();
        }
    }
    
    protected void ddlEmpRecord_SelectedIndexChanged(object sender, EventArgs e)
    {

        try
        {
           
            BindEmpGrid();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
        }
    }

    private void BindEmpGrid()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter();
        try
        {
            SqlCommand cmd = new SqlCommand("select * from shakhes where month=" + ddlEmpRecord.Text  + " ", con);
            adp.SelectCommand = cmd;
            adp.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                grdEmp.DataSource = dt;
                lblsalary.Text = "salary: " + dt.Rows[0]["salary"].ToString();
                lblyear.Text = "year :" + dt.Rows[0]["year"].ToString();
                lblmonth.Text = "month: " + dt.Rows[0]["month"].ToString();
               
                grdEmp.DataBind();
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
        }
        finally
        {
            dt.Clear();
            dt.Dispose();
            adp.Dispose();
           
        }
    }

    protected void ddlEmpRecord_TextChanged(object sender, EventArgs e)
    {
        BindEmpGrid();
    }
}
Posted
Updated 5-Oct-15 7:28am
v3
Comments
Mathi Mani 5-Oct-15 12:59pm    
Try calling BindEmpGrid() method from ddlEmpRecord_SelectedIndexChanged
rezaeti 5-Oct-15 13:05pm    
Hi thanks for reply
I try this from ddlEmpRecord_SelectedIndexChanged and page_load but does not work
Mathi Mani 5-Oct-15 13:16pm    
Did you set AutoPostBack="True" in the aspx file?
rezaeti 5-Oct-15 13:23pm    
yse of cource
rezaeti 5-Oct-15 13:25pm    
I edit my code
I want when select text of dropdownlist ( a month) then the year and month and salary text display in labels and gridview

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