Menu
Home
About Me
Articles
Hobbies
Code Library
Flash Library
3d stuido max

 
public void LoadSelect(DropDownList DropListObject, 
                       String SQL, String SelectedValue, 
                       OleDbConnection ActiveConn)
{
  //open connection and populate dataset object
  ActiveConn.Open();
  System.Data.DataSet ds = new DataSet();
  OleDbDataAdapter adapter = new OleDbDataAdapter(SQL,ActiveConn);
  adapter.Fill(ds);
  ActiveConn.Close();

  //format droplist fields from returned data
  DropListObject.DataValueField = ds.Tables[0].Columns[0].ToString();
  DropListObject.DataTextField = ds.Tables[0].Columns[1].ToString();
  DropListObject.DataSource=ds.Tables[ds.Tables[0].TableName];
  DropListObject.DataBind();
  
  //add null/blank option
  DropListObject.Items.Insert(0,"");

  try 
  {
    if (SelectedValue=="0")
     DropListObject.Items.FindByText("").Selected = true;
    else
     DropListObject.Items.FindByValue(SelectedValue).Selected = true;
  }
  catch
  {
    DropListObject.Items.FindByText("").Selected = true;
  }
  adapter.Dispose();
  ds.Dispose();
}

LoadSelect(droplistobj,"Select ID, Text from Table",DefaultID.ToString(), ActiveConn);