Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using ASP.Net and using code behind as C#. Now I want to Retrieve data from SQL server to DropDownList control. The selected value should be displayed in the dropdown list (Form Load).

Help please.

Regards,
Praveen
Posted
Updated 1-Sep-11 17:32pm
v2

protected void load_cmbFirstLanguage(int intCourseId,DropDownList cmbFLanguage)
{
ds = new DataSet();
ad = new SqlDataAdapter("select * from Table","cn");
ad.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
cmbFLanguage.Items.Clear();
cmbFLanguage.Items.Add("--Select First Language--");
cmbFLanguage.Items[0].Value = "0";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cmbFLanguage.Items.Add(ds.Tables[0].Rows[i]["strSubjectName"].ToString());


}
}
else
{
lblMsg.ForeColor = Color.Red;
lblMsg.Text = "No Records found";
}
}
 
Share this answer
 
Hi Praveen,
protected void Page_Load(object sender, EventArgs e)
   {
       string cs = ConfigurationManager.ConnectionStrings["samplecnstring"].ConnectionString;
       SqlConnection cn = new SqlConnection(cs);
       SqlDataAdapter da = new SqlDataAdapter("select countryname from country", cn);
       DataSet ds = new DataSet();
       da.Fill(ds, "Country");

       DropDownList1.DataSource = ds;
       DropDownList1.DataTextField = "countryname";
       DropDownList1.DataValueField = "countryname";
       DropDownList1.DataBind();
       DropDownList1.Items.Insert(0, "-Select-");
   }


Have a good day!!!
 
Share this answer
 
try
{
if (!IsPostBack)
{
if (drpdwnlist.SelectedValue == null)
{
Response.Redirect("login.aspx");
}

else
{

ListItem lt;
DataSet ds = new DataSet();
ds = balObj.getCustomerValues();

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
lt = new ListItem();
lt.Text = ds.Tables[0].Rows[i][1].ToString();
lt.Value = ds.Tables[0].Rows[i][0].ToString();
this.drpdwnlist.Items.Add(lt);

}

this.drpdwnlist.SelectedIndex = 0;
objComp.CompIntlId = int.Parse(this.drpdwnlist.SelectedValue);
lbldispaly.Text = drpdwnlist.SelectedItem.Value.ToString();
}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900