Link to home
Start Free TrialLog in
Avatar of ITCSAdmin
ITCSAdminFlag for United States of America

asked on

How do you change the ConnectionString for Entire Project?

"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?
Avatar of VBRocks
VBRocks
Flag of United States of America image

You can store them in the Settings:

    Project menu | Properties | Settings (tab)

You can access them through:

    'example:
    My.Settings.MyConnectionString
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 ...
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
Avatar of ITCSAdmin

ASKER

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.
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?
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
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>

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)

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)


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

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

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

ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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