Solved

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

Posted on 2012-04-12
5
750 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

728 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