Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Notice I didn't have to open and close the connection.</p> </blockquote> <p>No, but the <code>DataAdapter</code> does it implicitely.</p> <p><a href="http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx" rel="nofollow">MSDN</a>:</p> <blockquote> <p>If the IDbConnection is closed before Fill is called, it is opened to retrieve data and then closed. If the connection is open before Fill is called, it remains open.</p> </blockquote> <p>But also note that opening and closing is not inefficient when you're using <a href="http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx" rel="nofollow"><code>Connection-Pooling</code></a>(default). Because then <code>con.Open</code> just means "wait, i need this connection now" and <code>con.Close</code> means "ok, you can reuse it somewhere else now, i'm finished".</p> <p>So you should always close a connection as soon as you're finished with it. Otherwise the connection-pool needs to open new physical connections everytime which can cause exceptions(maximum default connection count is 100) but always will decrease the performance. </p> <p>The best way to dispose/close connections(any <code>IDisposable</code>) is by using the <a href="http://msdn.microsoft.com/en-us/library/yh598w02.aspx" rel="nofollow"><code>using</code> statement</a>:</p> <pre><code>using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["PhoQL"].ConnectionString)) using(var cmd = new SqlCommand("SELECT [Abrv], [State] FROM [States]", con)) using(var da = new SqlDataAdapter(cmd)) { DataTable tbl = new DataTable(); da.Fill(tbl); ddlShipState.DataSource = tbl; ddlShipState.DataTextField = "State"; ddlShipState.DataValueField = "Abrv"; ddlShipState.DataBind(); } </code></pre> <p>(sidenote: i've replaced your <code>DataSet</code> with a "lighter" <code>DataTable</code>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload