oshirowanen
oshirowanen

Reputation: 15965

Confused about SqlDataSource

in my C# code, I use a connection string like this:

connection = new SqlConnection(ConfigurationManager.AppSettings["myconnectionstring"])

which works fine.

I'm now trying to use the <asp:SqlDataSource control, but I can't seem to use the above connection string in the control like this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"   
ConnectionString="<%$ ConfigurationManager.AppSettings["myconnectionstring"] %>"   
SelectCommand="SELECT * from table1"></asp:SqlDataSource> 

I get an error saying:

Parser Error Message: The server tag is not well formed.

What am I doing wrong?

Upvotes: 1

Views: 314

Answers (1)

rene
rene

Reputation: 42494

it is not code you run there but a placeholder: that basically tells the DataSource 'look in the config file for a configuration block called ConnectionStrings and if you find it take the value from the element with the key myconnectionstring'

<asp:SqlDataSource ID="SqlDataSource1" runat="server"    
ConnectionString="<%$ ConnectionStrings:myconnectionstring%>"    
SelectCommand="SELECT * from table1"></asp:SqlDataSource>  

remember to add your connectionstring the app.config section ConnectionStrings,

<connectionStrings>
  <add name="myconnectionstring" connectionString="server=localhost;database=Northwind;user=north;password=north;" providerName="System.Data.SqlClient" />
</connectionStrings>

specially because you want to keep them safe

Upvotes: 1

Related Questions