I want to just bind the dropdown with DB,I have three rows in mine DB.Instead of adding the column data,In mine dropdown System.Data.DropDown is coming three times y So???

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class FrmSatelliteMain : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=(local);Initial catalog=SatelliteTVonPV;User ID=sandy;Password=sandy");
    string Query;
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }


    private void Bind()
    {
       
        Query = "SELECT * FROM Countries";
        cmd = new SqlCommand(Query, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownLanguages.DataSource = ds;
        DropDownLanguages.DataBind();
        conn.Close();
    }



}
create table Countries(UID integer primary key,country varchar(100))
insert into Countries values(1,'Brazil')
insert into Countries values(2,'China')
insert into Countries values(3,'India')
select * from Countries

You need to set the data member's name. By default when you first fill a table it is named "Table" but this is unreliable. Try using this method:

private void Bind()
    {
      Query = "SELECT * FROM madExcept";
      cmd = new SqlCommand(Query, conn);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      ds.Tables[0].TableName = "MyTable";
      this.DropDownLanguages.DataMember = "MyTable";
      this.DropDownLanguages.DataTextField = "UploadIp"; //column from query
      this.DropDownLanguages.DataValueField = "Version"; //column from query
      DropDownLanguages.DataSource = ds;
      DropDownLanguages.DataBind();
      conn.Close();
    }

u need to bind the below properties

DropDownLanguages.DataTextField =
DropDownLanguages.DataValueField =

DropDownLanguages.DataTextField = "country";

Hearist Thx!


hello sknake can u tell me what is DataValueField ?Its porpose!

sandeep,

It just lets you hold associated values in the combobox. Image if your "DataTextField" was "Customer Name" and your "DataValueField" was "Customer ID". With this you could have two customers named "John Smith" and you would be able to distinguish which John Smith they selected in the combo box.

string sText = this.DropDownLanguages.SelectedItem.Text;
      string sValue = this.DropDownLanguages.SelectedItem.Value;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.