C#C
C#2y ago
BlindWiz

Creating a SqlConnection connection via connection string

Hi All, I have the following connection string:

Server=mssql01,3266;Database=ProductsDev;Trusted_Connection=True;

so I have the code:

string con_str = "Server=mssql01,3266;Database=ProductsDev;Trusted_Connection=True;";
using(var sqlConnection = new SqlConnection(con_str)) {
this.sqlConnection = sqlConnection; // store the object for the rest of the code to use.
}

I have this in a constructor, but when I use this sqlConnection object from another method where I am using a SqlCommand object to insert a row into a table it says it is closed, and I get the exception

The ConnectionString property has not been initialized.

Is the issue that I put the SqlConnection object in a using statement so its closing it?

but if if it is closing it, won't this.sqlConnection.open() reopen the connection to the db?

Thanks, I hope this makes sense.
Was this page helpful?