Frage

which approaches are correct about "using" (first or second)?

First:

using (DataTable dt = list.ToDataTable())
{
     dataList.DataSource = dt;
     dataList.DataBind();
}

Second:

 using (DataTable dt = list.ToDataTable())
 {
     dataList.DataSource = dt;
 }
 dataList.DataBind();
War es hilfreich?

Lösung

Well in your case it has to be the first approach otherwise you will dispose of the DataTable before it's been used (so DataBind() in the second would throw an exception).

In general though, you only need to keep code which uses the disposable object within the scope of the using.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top