binded a dropdowlist[continents] with gridview.. but continents appear twice in dropdownlist..
ex: asia asia
DropDownList1.AppendDataBoundItems = true;
// String strConnString = ConfigurationManager
// .ConnectionStrings["conString"].ConnectionString;
String strQuery = "select ID, ContinentName from Continents";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "ContinentName";
//DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from NEWS where CONTINENT = '" + DropDownList1.SelectedValue + "'", con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Label1.Text = "record found";
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Apr 24, 2013, 9:45 AM
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.AppendDataBoundItems = true;
// String strConnString = ConfigurationManager
// .ConnectionStrings["conString"].ConnectionString;
String strQuery = "select ID, ContinentName from Continents";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "ContinentName";
//DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}