Solved

ASP.NET ConnectionString not working

Posted on 2011-09-05
5
388 Views
Last Modified: 2012-05-12
In my .aspx page VS2008 C# ASP.NET I have this line:
-----------
<asp:SqlDataSource ID="mySqlDataSourceID" runat="server"
                                ConnectionString="<%$ ConnectionStrings:myConnectionString1 %>"
                                onselecting="mysomething"                                
ProviderName="<%$ ConnectionStrings:myConnectionString1.ProviderName %>"
        SelectCommand="SELECT * from MyTab">
</asp:SqlDataSource>
-----------
The web.config has:
<configuration>
  <connectionStrings>
    <add name="myConnectionString1" connectionString="DATA SOURCE=mySOMETHING1;PASSWORD=myPwd;PERSIST SECURITY INFO=True;USER ID=myID"
     providerName="Oracle.DataAccess.Client" />
  </connectionStrings>
</configuration>
------
Above works.

But I need to get rid of "myConnectionString1" under   <connectionStrings> in the web.config file.
Instead I need to use the similar stuf under: <appSettings> in the web.config file.

The current contenct in web.config ther is:
  <appSettings>
<add key="myKey1" value="Data Source=mySOMETHING1;Persist Security Info=True;User ID=myID;Password=myPwd;"/>
</appSettings>

The value of "myKey1" in the   <appSettings> is same as the myConnectionString1under  <myConnectionString>.

How could I use "myKey1" of   <appSettings> above instead of "myConnectionString1" in   <connectionStrings> for the SqlDataSource ID "mySqlDataSourceID" ?
In my .aspx.page?

I tried:
<asp:SqlDataSource ID="mySqlDataSourceID" runat="server"
                                ConnectionString="<%$ AppSettings["myKey1"] %>"
                                onselecting="mysomething"                                
ProviderName="Oracle.DataAccess.Client"
        SelectCommand="SELECT * from MyTab">
</asp:SqlDataSource>

But It shows syntax error for the line:
                                ConnectionString="<%$ AppSettings["myKey1"] %>"

How to fix?
0
Comment
Question by:toooki
5 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36486792
Try
"<%$ AppSettings.Settings["myKey1"] %>"
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 238 total points
ID: 36486823
"<%$ AppSettings:myKey1 %>"
0
 
LVL 4

Assisted Solution

by:AnuTiji
AnuTiji earned 237 total points
ID: 36486851
Hi
Please try the following code
 <asp:SqlDataSource ID="mySqlDataSourceID" runat="server" ConnectionString="<%$ appSettings:myKey1 %>"  ProviderName=System.Data.OracleClient
              SelectCommand="SELECT * from MyTab">
        </asp:SqlDataSource>

Open in new window

0
 

Author Comment

by:toooki
ID: 36583992
Thank you. I am going to try.
0
 

Author Comment

by:toooki
ID: 37129393
Thank you all. It worked.

This syntax worked:

<asp:SqlDataSource ID="mySqlDataSourceID" runat="server"
                               ConnectionString="<%$ appSettings:myKey1 %>"
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question