I have a connection string for my SQL database connection called my.settings.GCRConnectionString which was created when I created my dataset for the application. I want to change the value of the connection string but it give me an error saying it is ready only. Is there a way to update my connection string during the run-time?

Recommended Answers

All 4 Replies

You could try something like this:

My.Settings.GCRConnectionString = "new string info here"
My.Settings.Save()

Im gonna write this short.

begginer, your code works on most application setting but there are exceptions such as settings with scope set at application level(readyonly settings)

After long day of jerkin Google, I put together a solution by piecing together what other people had. (Big thanks to TheCodeMonk)

'Creating connection string
Imports System.Data.SqlClient

Public Class ClassConnectionBuilder
    Public Shared Function ConnectionStringBuilder(ByRef Server As String, ByRef database As String, _
                                                   ByRef userid As String, ByRef password As String) As String

        Dim sqlConnString As New System.Data.SqlClient.SqlConnectionStringBuilder() With {
            .DataSource = Server,
            .InitialCatalog = database,
            .UserID = userid,
            .Password = password
        }
        Return sqlConnString.ConnectionString
    End Function
End Class
'Updating connection string
My.Settings("your_connectionstring") = ConnectionStringBuilder(Server, Database, user, password *******)
'saving settings
My.Settings.Save()

I dont know how I came up with these codes but it work. So i say that was a big step forward for this .NET dummy.I would appreciate if someone would explain to me the difference between begginer's post and my post.

The code above creates a structure, in the form of a class, then passes the string information into the class. It then builds the string and assigns the value to your settings with what ever string name you want it to be.

Then it saves.

To use this, build the class by using the Project > Add Class name it ClassConnectionBuilder. Save the code you have above into that class.

'Creating connection string
Imports System.Data.SqlClient

Public Class ClassConnectionBuilder
    Public Shared Function ConnectionStringBuilder(ByRef Server As String, ByRef database As String, _
                                                   ByRef userid As String, ByRef password As String) As String

        Dim sqlConnString As New System.Data.SqlClient.SqlConnectionStringBuilder() With {
            .DataSource = Server,
            .InitialCatalog = database,
            .UserID = userid,
            .Password = password
        }
        Return sqlConnString.ConnectionString
    End Function
End Class

Then you can use the build by either importing it then using it or providing a full "path".

IE

'Imported
'Importing would be: Imports yourprojectname.ClassConnectionBuilder
SqlConnectionStringBuilder(string,string,string,string)

'Not imported
ClassConnectionBuilder.SqlConnectionStringBuilder

Hope this helps

Anything that will broaden my knowledge helps.
Thanks bigginner

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.