Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-12
5
Medium Priority
?
754 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 750 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 750 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 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