The Page Load method gets the DropDownList filled with data from Database. When don't have to manually add the List options in the DropDownList. Here is the code below for populating DropDownList Menu. It works like a charm.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand("SELECT Name, Id FROM RetPDF");
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
ddlCountries.DataSource = ds;
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "id";
ddlCountries.DataBind();
}
}
con.Close();
}
}
}