Solved

connect to SQL database through connection string in web.config file

Posted on 2012-04-12
5
749 Views
Last Modified: 2012-06-27
Hi Experts,

I am going through and looking to tidy up someone elses work and need a little bit of help with getting a SQL Datasource to find a new connection string.

what was previously in place was

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:Standard_Apps_Data_DevConnectionString %>"
                SelectCommand="SELECT * FROM [T4032_ChangeRequest_EmailList]">
            </asp:SqlDataSource>

and this was getting one of 35 of the same SQL Connection strings in the asp.net application.

I now have a single connection string in the web.config file and have successfully connect in the aspx.vb files but the original coder has used some select statements in the page its self.

The connection string in the web.config file is ;

<connectionStrings>
      <add name="Database" connectionString="server=DUGBSSC2EDC001;database=Standard_Apps_Data_Dev;User ID=VS_Change_Req;Password=Passw0rd" providerName="system.data.sqlclient"/>
    <!--<add name="Database" connectionString="server=PUGBSSC1EDC001;database=Standard_Apps_Data_Live;User ID=VS_Change_Req;Password=Passw0rd" providerName="system.data.sqlclient"/>-->
    </connectionStrings>

and i had changed the in page asp:sqldatasource tag to

   <asp:SqlDataSource ID="Database" runat="server"
                ConnectionString="<%$ ConnectionStrings:Database %>"
                SelectCommand="SELECT * FROM [T4032_ChangeRequest_EmailList]">
            </asp:SqlDataSource>

however,  this throws an error and the page in unviewable.

Can you please tell me what the asp:sqldatasource should be in order to use the connection string in the web.config file.

Thanks

Simon
0
Comment
Question by:SimonPrice33
  • 2
  • 2
5 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 250 total points
ID: 37836467
I'm not sure, but try to rename it so that the connection is not called database

connectionStrings>
      <add name="DatabaseConnectionString" connectionString="server=DUGBSSC2EDC001;database=Standard_Apps_Data_Dev;User ID=VS_Change_Req;Password=Passw0rd" providerName="system.data.sqlclient"/>
   </connectionStrings>

<asp:SqlDataSource ID="Database" runat="server"
                ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
                SelectCommand="SELECT * FROM [T4032_ChangeRequest_EmailList]">
            </asp:SqlDataSource>
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 250 total points
ID: 37836490
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 37836493
<appSettings>
<add key="MyConnString"
value="server=localhost;database=Northwind;uid=sa; password=secret;" />
</appSettings>

Now in any subsequent classes or ASPX pages that you create simply call the
connectionstring as:
ConfigurationSettings.AppSettings("M yConnString").ToString()

or in ASP.NET 2.0

ConfigurationManager.ConnectionStrin gs("ConnectionString").ConnectionString.ToString()

refer

http://www.velocityreviews.com/forums/t379986-variable-connectionstring-in-a-asp-sqldatasource.html
0
 

Author Comment

by:SimonPrice33
ID: 37836531
Dhaest,

that hasnt worked for me unfortunately :(,

pratima_mcs

the 35 refernces that have been removed have been replaced with

SqlConn.ConnectionString = connstr

where Dim connstr As String = ConfigurationManager.ConnectionStrings("Database").ConnectionString is at the top of each aspx.vb page where required.

and got my example from one of your links; http://weblogs.asp.net/owscott/archive/2005/08/26/Using-connection-strings-from-web.config-in-ASP.NET-v2.0.aspx

which hasnt worked for me.

how would putting the refference in the appsettings tags work better rather than putting them in the connection strings tags?

Thanks

Simon
0
 

Author Comment

by:SimonPrice33
ID: 37836772
i have chanced the ID from Database to SqlDataSource1 and it appears to have resolved the issue.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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