---
title: "Dropdownlist and gridview"  
description: "Dropdownlist and gridview"  
author: "Pawan kumar"  
published: 2011-03-31  
updated: 2011-04-02  
canonical: https://www.mindstick.com/forum/213/dropdownlist-and-gridview  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Dropdownlist and gridview

```
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](https://www.mindstick.com/forum/194/how-display-record-in-gridview-from-table-in-asp-dot-net) when [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) a [item](https://www.mindstick.com/forum/156564/remove-item-in-an-array-in-javascript) from [dropdownlist](https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework),but there r some plms its not display record .so plz help me...

## Replies

### Reply by Amit Singh

Hello pawan,\

cmd.CommandText = "select EmpID,EmpName from TblEmpReg where Dept='" +DdlDept.SelectedValue.ToString()+ "' ";

check the highlighted position where are you matching between Dept and EmpID but it should be "**Dept and** **DdlDept.SelectedText** (it contains the dept field value)

**so your query string should be**

## cmd.CommandText = "select EmpID,EmpName from TblEmpReg where Dept='" +DdlDept.SelectedText+ "' ";

replace your line and check it


---

Original Source: https://www.mindstick.com/forum/213/dropdownlist-and-gridview

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
