Solved

ASP.NET ConnectionString not working

Posted on 2011-09-05
5
385 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.NET 2008 VB and C# 6 28
What Does This C# Code Block Do? 5 33
Linq asp.net mvc 13 12
I need help with Crystal Report's Parameter Prompt Form 2 14
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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