DROPDOWNLIST AND GRIDVIEW

Pawan kumar

Total Post:19

Points:133
Posted by  Pawan kumar
 1457  View(s)
Ratings:
Rate this:

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...

  1. Amit Singh

    Post:103

    Points:721
    Re: Dropdownlist and gridview

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!