Hi...
I am working in asp.net and i want to display record in database when we select the item in dropdownlist and gridview ie. show gridview record (like particular column value)and dropdownlist(select value) in database?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 24, 2014, 8:07 AM EDT
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;
namespace Dropdownlist_gridview
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList1.Items.Add("Choose Id");
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["empid"].ToString());
}
reader.Close();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee where empid='" + DropDownList1.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(str,con);
ds = new DataSet();
sqlda.Fill(ds, "employee");
GridView1.DataMember = "employee";
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
}
Raghav Mehra51 reputationPosted Aug 31, 2014, 8:36 PM EDT
Gowtham RajamanickamRank #57 · 29K reputationPosted Apr 13, 2015, 3:56 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Apr 29, 2015, 6:29 AM EDT
Note: Make sure to set AutoPostback to TRUE in your dropdownlist to trigger the selectedindexchanged event.