NewBie: Saving Application settings?

G

Graves

Hi
I am trying to learn Vb.net, but have reached a stand still :-(

I am trying to write an updated database connection string to the
application settings.

Try
If Not _IsChanged Then Return

ConnectionFullName =
String.Format("{0}.My.MySettings.DBConnectionString.connectionString.{1}",
System.Reflection.Assembly.GetExecutingAssembly.GetName.Name,
connectionName)

Config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Section = CType(Config.GetSection("connectionStrings"),
ConnectionStringsSection)
Setting = Section.ConnectionStrings(ConnectionFullName)

If IsNothing(Setting) Then Throw New Exception("There is no
connection with this name defined in the config file.")

Setting.ConnectionString = Me._ConnectionString
Config.Save(ConfigurationSaveMode.Full)

My.MySettings.Default.Item(connectionName) = Me._ConnectionString

Catch ex As Exception
Throw New Exception(String.Format("Failed saving connection
'{0}':{1}{2}", connectionName, vbCrLf, ex.Message), ex)
End Try

But still i am getting an error:
System.Exception was unhandled
Message="Failed saving connection 'Data Source=GK-PC02\SQLEXPRESS;Initial
Catalog=db2;Persist Security Info=True;User ID=sa;Password=': There is no
connection with this name defined in the config file."

Can anyone tell me what i do wrong?

Thank on advance :)
 
A

Alcibiade

what are you trying to do??
You are a newbie so I think you could use editor shortcut ;)
For creating/saving connectionstring uses automatic procedure.
Instead if you wanna to store some properties as window location for
example, check property window of the object and have a look about
application settings ;)
I hope this could help you
 
G

Graves

iHi
Thanks for your answer.
I have created at form where i want to be able to update my database
conections.
The data connections is stored in the Application settings named
DBConnectionString.
The form is reading and updating the code perfect, but when i want to write
it bact to the application setting partameter, i keep getting the error that
there is no connection with that name. but if i write the parameters out and
look in the app.config, it looks like the parameters should be right :-(

thanks on advance :)
/Graves
 
G

Graves

Hi Patrice

Thanks alot :)
That helpde a lot, cut and paste confused me.

Now everything looks right, and it looks like the variables have the right
values.
The runtime values is updated, but the settings is not updated in the
application setting. Can you tell me if there is a trick?

Thanks :)

/Graves
 
G

Graves

Hi Patrice

Thanks :)
Yes i was running the solution as debug, but whe i run the .exe everything
works as it should :)

I am learning, thanks :)

/Graves
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top