forum

Home / DeveloperSection / Forums / Dropdownlist and gridview

Dropdownlist and gridview

Pawan kumar215031-Mar-2011
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Empfilter : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
BindData();
DdlDept.Focus();
}
public void BindData()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
cmd.Connection = con;
try
{
con.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select EmpID,Dept from TblEmpReg";
DataSet ds = new DataSet();
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
da1.Fill(ds);
DdlDept.DataSource=ds;
DdlDept.DataTextField = "Dept";
DdlDept.DataValueField = "EmpID";
DdlDept.DataBind();
DdlDept.Items.Insert(0, "---Select---");
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
}
}
public void ShowData()
{
}
protected void DdlDept_SelectedIndexChanged(object sender, EventArgs e)
{
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
cmd.Connection = con;
try
{
//if (DdlDept.SelectedValue != "---Select---")
//{
//string nameshow = DdlDept.Text.ToString().Trim();
cmd.CommandText = "select EmpID,EmpName from TblEmpReg where Dept='" +DdlDept.SelectedValue.ToString()+ "' ";
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
//}
}
catch(Exception ex)
{
Lblmsg.Text = ex.Message;
}
}
}
}

Sir i want  display record in gridview when select a item from dropdownlist,but there  r some plms its not display record  .so plz help me...


Updated on 02-Apr-2011

Can you answer this question?


Answer

1 Answers

Liked By