ASP.Net: Storing & Using Connection String In Web.Config

Web.Config
<configuration>
    <connectionStrings>
        <add name="ConnString" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
    </connectionStrings>
</configuration>



Aspx Page
<asp:SqlDataSource
    id="srcMovies"
    SelectCommand="SELECT * FROM Movies"
    ConnectionString="<%$ ConnectionStrings:ConnString %>"
Runat="server" />


You can also fatch Connection String from Web.Config as
ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString

No comments:

Post a Comment