column value BOUND twice in dropdownlist..

Posted by Parthibansk under ASP.NET on 4/24/2013 | Points: 10 | Views : 542 | Status : [Member] | Replies : 1
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";




Responses

Posted by: Satyapriyanayak on: 4/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Try

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();
}
}

}

If this post helps you mark it as answer
Thanks

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response