0

First, let's have a look at my code:

Private Sub FormLoad(sender As Object, e As EventArgs) Handles MyBase.Load

    txtMDF.Text = My.Settings.MDF

End Sub

Assume My.Settings.MDF has a string value of Application.StartupPath + "\MyDB.mdf". I get this result:

Image 1

But I want the result to be:

Image 2

I have tried the following links and methods:

Community
  • 1
  • 1
  • 2
    Why put `Application.StartupPath` into the setting? just leave in the name of your file in the setting and then use `Path.Combine()` to get the full filename – A Friend Feb 21 '17 at 11:39
  • 1
    No, I want it just like at first open the file location to be Application.StartupPath + + "\MyDB.mdf" but later if I want to change the Database location it should be changed and save in My.Settings... –  Feb 21 '17 at 11:47
  • So present them with the default, and then if they want to change the location you'll have to save their entire filepath specifically. – A Friend Feb 21 '17 at 11:50
  • 1
    But Can't I do what I asked? –  Feb 21 '17 at 11:51
  • 1
    Nope not directly... If you REALLY need a variable prefix like that I suggest you store it as something like "!SUP\MuDB.mdf" then do a replace when you read it. – Trevor_G Feb 21 '17 at 11:54
  • As already mentioned a few times, it can't be done. You need to save their specific path separately and later retrieve it for use as @Trevor has mentioned and Pro Grammer. – Trevor Feb 21 '17 at 12:05

1 Answers1

2

As ProGamer Suggested,

First

save you My.Settings.MDF String = None

Second

Edit your code as follows:

Private Sub FormLoad(sender As Object, e As EventArgs) Handles MyBase.Load

    If My.Settings.MDF = "None" Then
        txtMDF.Text = Application.StartupPath + "\MyDB.mdf"
    Else
        txtMDF.Text = My.Settings.MDF
    End If
    txtMDF.Text = My.Settings.MDF

End Sub

Third

Add the following code to YourFormClose_Event

Private Sub FormClosing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    My.Settings.MDF = txtMDF.Text
    My.Settings.Save()
End Sub

And NOTE that you should select "User" from 'Scope Drop Down' in the Settings for MDF instead of "Application" or else My.Settings.Save() will not work and it will remain "None"

Example:

enter image description here

  • My Pleasure! And waiting for your result. :) ;) – Mohammed Julfikar Ali Mahbub Feb 21 '17 at 12:12
  • 1
    `If ... else` statement in `FormLoad` have no sense, because you overwrite `txtMDF.Text` anyway by value from settings – Fabio Feb 21 '17 at 13:45
  • @Fabio is correct. The last line of your code will overwrite the value in txtMDF.Text with the value of "None" since the value in My.Settings.MDF still contains the value "None" and has not yet been saved with the new value. – Chris Dunaway Feb 22 '17 at 15:02
  • @ChrisDunaway I added that because suppose he use that location in his ConnectionString and he open the Connection on Form_Load then he would get error so I added that and it would not affect because I think it will only do that when he first launch it later once he changed the value in My.Settings.MDF then it will be just ok.... I want to know if I am correct please let me know.... tnx! – Mohammed Julfikar Ali Mahbub Feb 22 '17 at 19:27
  • 1
    Thank you guys for all of your help but I don't have any issue till now using Mohammad Zulfikar's code... –  Feb 22 '17 at 22:16