Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining ConnectionString in asp SqlDataSource

Tags:

c#

asp.net

I have my database connectivity which I define under <appSettings> in web.comfig:

<appSettings>
    <add key="ConnStr" 
         value="Data Source=dsk-159\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"/>
</appSettings>

But the problem is that I am unable to access it from my aspx page as I am trying like this

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:goldsConnectionString %>"
     SelectCommand="SELECT distinct TechnologyId , [TechnologyName], [ChildId] FROM [TreeTable] where childid is null AND technologyid in(@hid1)">
     <SelectParameters>
         <asp:ControlParameter ControlID="hid1" Name="hid1" DefaultValue="23" />
     </SelectParameters>

In place of <connectionStrings> I want to define it in <appSettings>

Plesae tell the correct syntax.

like image 694
golds Avatar asked Apr 24 '13 08:04

golds


1 Answers

You know you can set the connection string in the code behind rather than inline, it's much cleaner.

SqlDataSource2.ConnectionString = 
System.Configuration.ConfigurationManager.AppSettings["ConnStr"];

Consider reading up on ConfigurationManger.AppSettings

like image 67
DGibbs Avatar answered Sep 30 '22 08:09

DGibbs