Re: Creating connection string (i think)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Aug 1, 11:46 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@xxxxxxxxx> wrote:
On Aug 1, 10:50 am, "K Viltersten" <t...@xxxxxxxxxxxxxx> wrote:

I'm getting "connection not initialized" for
error when running the following code.

You are missing a single line :)

cmn.Connection = con;


BTW, you should code your code in this way:

using (SqlConnection con = new SqlConnection(conStr) )
{
con.Open();
using (SqlCommand cmd = new SqlCommand("select * from
the_table", con) )
{
cmd.ExecuteNonQuery()
}
}

in this way you make sure that the objects are disposed correctly,
even in the presence of an exception.
.



Relevant Pages