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
Solved

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

Posted on 2012-04-12
5
748 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
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 …

807 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