Hello John,
Even better would be a class that wraps the ConfigurationSettings, so that
you arent relying on magic constants...
class MyConfiguration
{
public static string ConnectionString { get { return ConfigurationSettings.AppSettings["ConnectionString"];
} }
}
then: SqlConnection conn = new SqlConnection(MyConfiguration.ConnectionString);
This way, errors are caught at compile time, rather than runtime. Also, if
you happen to change the name of the setting in the app.config, you can change
it in one spot, rather than having to do a global search/replace.
--
Matt Berther
http://www.mattberther.com
Hi Perin,
In web.config file, add the section below.
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=testdb;uid=sa;password=secret;"
/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
In your code, access like this..
strConnection = ConfigurationSettings.AppSettings("ConnectionString")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers", sqlConn)
sqlConn.Open()
Very simple..
Rgds,
John Paul. A