?
Solved

How do you change the ConnectionString for Entire Project?

Posted on 2009-02-13
14
Medium Priority
?
244 Views
Last Modified: 2013-11-26
"PLMConnectionString" is the ConnectionString that I am using, but what I need is to know how to change the String after Deployment to match the customers information.

My current project has the following ConnectionString in the Project.Settings:
My current String is: "Data Source=ITCS-SQL-01; Initial Catalog=PLM; User ID=PLMAdmin; Password=*********"
I published the project and then realized that I need to change the Project.Settings to the following:
Customers Should be: "Data Source=RPL-ADS01;Initial Catalog=PLM;User ID=PLMAdmin;Password=*********"
 
So how do I change the ConnectionString so that I can continue to develope this appliaction and yet be able to deploy it at customers locations?

Where in my project do I need to place this code to make it global?
0
Comment
Question by:ITCSAdmin
  • 6
  • 5
  • 3
14 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 23634307
You can store them in the Settings:

    Project menu | Properties | Settings (tab)

You can access them through:

    'example:
    My.Settings.MyConnectionString
0
 
LVL 3

Expert Comment

by:Mahone7
ID: 23634385
u have to change the connection string manualy or you can store the connection string in the web.comfig file like this way in the appSetting Section in Web.Config file
  <appSettings>
    <add key="myString"
      value="Connection String Value" />
  </appSettings>
and you can get the connection string in your code like this way :
String strConnection = ConfigurationSettings.AppSettings("myString")
 
i hope that would be helpful ...
0
 
LVL 3

Expert Comment

by:Mahone7
ID: 23634486
there are another way to put the connection string in the <ConnectionString> Section in the web config file like this way :

 <connectionStrings>
   <add name="myString" connectionString="my Connnection Value"/>
 </connectionStrings>

then use the code to get the value of it :

[C#]
string connStr = ConfigurationManager.ConnectionStrings["myString"].ConnectionString;

[VB]
Dim connStr As String = ConfigurationManager.ConnectionStrings("myString").ConnectionString;

cya
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ITCSAdmin
ID: 23636023
VBRocks: Your code allowed me to get the current Connectionstring, but I needed to change the current connectionstring after deploying the applaiction at the customers site.
0
 

Author Comment

by:ITCSAdmin
ID: 23636138
Mahone7:
I am very NEW to programming Just FYI. I understand that you have suggested a couple of ways to modify the Web.config. What I am not sure about it this:
  • I have developed a VB9 Appliaction, I have it working at my site
    • 2003 ADS Server,
    • SQL 2005 Server [ITCS-SQL-01],
    • WinXP Pro Workstation
  • I published the Appliaction, but then I realized that I needed to change the ConnectionString to point to the customers Server
    • 2003 SBS Server [RPL-ADS01] with 2005 SQL Express,
    • Windows XP Pro Workstations
  • How do I and Where do I place this code to allow this change for the Connectionstring?
  • How and Where do I change this when installing the applaiction?
0
 
LVL 3

Expert Comment

by:Mahone7
ID: 23637029
First You can't change your connectionString in Quick way ... u have to change them manualy ie: you have to check your pages and change the connectionString to the new one ...

but my advise is to make the connection string globle ... by adding it to the web.config file which is located in the directory of your web application ...

open it and find the <ConnectionString\> tage

replace it with :

 <connectionStrings>
   <add name="myString" connectionString="my Connnection Value"/>
 </connectionStrings>

while myString is what ever you wanna name it ...
and connectionString is the actual connectionString value which it is in your case is :

Data Source=RPL-ADS01;Initial Catalog=PLM;User ID=PLMAdmin;Password=*********";

and then after it when you will try get the connectionString just get it in these way :

[C#]
string connStr = ConfigurationManager.ConnectionStrings["myString"].ConnectionString;

[VB]
Dim connStr As String = ConfigurationManager.ConnectionStrings("myString").ConnectionString;

these way make your connectionString Globle for the website .... and every time you wanna change the connectionString just change the value in web.config and its changes in the entier website

P.S : each folder in your web application has a web.config file ... and you have to edit the one in the root folder ...

i hope that will help you
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 23637062
ITCSAdmin:  If you set it as an Application Setting, then when you deploy your project, find the configuration file, open it up with Notepad or something and change it manually.  The configuration file will have the same name as your exe, but end with ".exe.config", so for example:

    myprogram.exe.config


Look for and change the connection string:

    <connectionStrings>
        <add name="Expert_In_VB_2008.My.MySettings.CnString" connectionString="test" />
    </connectionStrings>

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 23637117
Another option is just put both connection strings in the Application Settings, and change it programmatically based off the build:

        Dim connectionString As String = String.Empty
#If Debug = True Then
        connectionString = My.Settings.DevConnectionString       'Development connection string
#Else
        connectionString = My.Settings.DeployConnectionString   'Deployment connection string
#End If

        MsgBox(connectionString)

0
 

Author Comment

by:ITCSAdmin
ID: 23637618
VBRocks:

Where do you place the following Code?

Dim connectionString As String = String.Empty
#If Debug = True Then
        connectionString = My.Settings.DevConnectionString       'Development connection string
#Else
        connectionString = My.Settings.DeployConnectionString   'Deployment connection string
#End If

        MsgBox(connectionString)


0
 
LVL 27

Expert Comment

by:VBRocks
ID: 23637741
Place it in your project module/class code before you create your connection.

Here's an example:

Dim connectionString As String = String.Empty
#If Debug = True Then
       connectionString = My.Settings.DevConnectionString       'Development connection string
#Else
       connectionString = My.Settings.DeployConnectionString   'Deployment connection string
#End If


    Private Function GetData() As DataTable

        Dim SQL As String = "SELECT * FROM myTable"

        Dim table As New DataTable()

        Dim adapter As New SqlClient.SqlDataAdapter(SQL, connectionString)
        adapter.Fill(table)

        adapter.Dispose()

        Return table

    End Function

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 23637751
I'm sorry...  I messed that example up, let's try that again:

    Private Function GetData() As DataTable

Dim connectionString As String = String.Empty
#If Debug = True Then
       connectionString = My.Settings.DevConnectionString       'Development connection string
#Else
       connectionString = My.Settings.DeployConnectionString   'Deployment connection string
#End If

        Dim SQL As String = "SELECT * FROM myTable"

        Dim table As New DataTable()

        Dim adapter As New SqlClient.SqlDataAdapter(SQL, connectionString)
        adapter.Fill(table)

        adapter.Dispose()

        Return table

    End Function

0
 

Author Comment

by:ITCSAdmin
ID: 23637824
I added to my VB Project "Lighthouse" - Module (module_Lighthouse), see code below:

Now I am getting an error with the "Debug" in my Visual Studio 2008 stating the following:

 "Debug is a type and connot be used as an expression."
Module module_Lighthouse
    Private Function GetData() As DataTable
 
        Dim connectionString As String = String.Empty
 
        If Debug = True Then
            connectionString = My.Settings.PLMConnectionStringDev     'Development connection string
        Else
            connectionString = My.Settings.PLMConnectionStringDeploy   'Deployment connection string
        End If
 
        Dim SQL As String = "SELECT * FROM myTable"
 
        Dim table As New DataTable()
 
        Dim adapter As New SqlClient.SqlDataAdapter(SQL, connectionString)
        adapter.Fill(table)
 
        adapter.Dispose()
 
        Return table
 
    End Function
 
End Module

Open in new window

0
 
LVL 27

Accepted Solution

by:
VBRocks earned 2000 total points
ID: 23637833
You're missing the pound signs ( # )

        #If Debug = True Then
            connectionString = My.Settings.PLMConnectionStringDev     'Development connection string
        #Else
            connectionString = My.Settings.PLMConnectionStringDeploy   'Deployment connection string
        #End If


0
 

Author Comment

by:ITCSAdmin
ID: 23638022
That took care of that issue, thanks.

I will notbe able to test it until next week, will then let you know, but will give you the points
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

809 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