Monday, September 16, 2013

Load Data into Dropdownlist using SQL -server (no sqldatasource)

void filDDPN()
{

    DropDownList1.Items.Clear();
    DataTable subjects = new DataTable();

    using (SqlConnection con = new SqlConnection(ConnectionString))
    {

        try
        {
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Period] FROM [tbl500_Periods] order by id desc", con);
            adapter.Fill(subjects);

            DropDownList1.DataSource = subjects;
            DropDownList1.DataTextField = "Period";
            DropDownList1.DataValueField = "Period";
            DropDownList1.DataBind();
        }
        catch (Exception ex)
        {
            // Handle the error
        }

    }
}

No comments: