Using ‘Using’ to Populate a DropDownList

Found a new way to populate a DropDownList using the ‘using’ statement.

DataTable dtTour = new DataTable();
string strConnection = ConfigurationManager.ConnectionStrings["ChinatowndbConnString"].ConnectionString;

using (SqlConnection con = new SqlConnection(strConnection))

SqlDataAdapter adapter = new SqlDataAdapter("SELECT TourId, TName FROM Chinatowndb.dbo.Tour", con);

ddlTour.DataSource = dtTour;
ddlTour.DataValueField = "TourId";
ddlTour.DataTextField = "TName";
catch (Exception ex)
// Handle the error


// Add the initial item
ddlTour.Items.Insert(0, new ListItem("", "0"));

You can find out more about ‘Using’ here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: