Solved

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

Posted on 2012-04-12
5
739 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
Comment Utility
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
Comment Utility
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
<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
Comment Utility
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
Comment Utility
i have chanced the ID from Database to SqlDataSource1 and it appears to have resolved the issue.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now