question

AmrAli4235 avatar image
0 Votes"
AmrAli4235 asked ·

Creation of a registry key for the first time gives an error

Hi all ,
Really i miss you a lot ,
Okay I have this code

  Private Sub LoginForm19_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         Panel2.Location = New Point(Me.ClientSize.Width / 2 - Panel2.Size.Width / 2, Me.ClientSize.Height / 2 - Panel2.Size.Height / 2)
         Panel2.Anchor = AnchorStyles.None
         InputLanguage.CurrentInputLanguage = InputLanguage.InstalledInputLanguages(0)
           
         CreateReg()
         GetReg1()
         SetReg1()
         SetReg3()
     End Sub
    
     Sub CreateReg()
         NewSubKey1 = Registry.CurrentUser.OpenSubKey(
             "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\NamedPipes", True)
         NewSubKey2 = Registry.CurrentUser.OpenSubKey(
            "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\TCPIP", True)
         NewSubKey3 = Registry.CurrentUser.OpenSubKey(
            "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\SharedMemory", True)
    
         If NewSubKey1 Is Nothing Then
             ' Key doesn't exist; create it.
             NewSubKey1 = Registry.CurrentUser.CreateSubKey(
                 "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\NamedPipes")
         End If
         If NewSubKey2 Is Nothing Then
             ' Key doesn't exist; create it.
             NewSubKey2 = Registry.CurrentUser.CreateSubKey(
                 "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\TCPIP")
         End If
         If NewSubKey3 Is Nothing Then
             ' Key doesn't exist; create it.
             NewSubKey3 = Registry.CurrentUser.CreateSubKey(
                 "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\SharedMemory")
         End If
    
     End Sub
    
     Sub SetReg1()
         NewSubKey1 = Registry.CurrentUser.OpenSubKey(
             "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\NamedPipes", True)
    
         If Label8.Text = "0" Then
             If NewSubKey1 IsNot Nothing Then
                 NewSubKey1.SetValue("Version", "2020-03-01")
                 NewSubKey1.Close()
             End If
         Else
             Label8.ForeColor = Color.Red
         End If
     End Sub
    
     Sub GetReg1()
         NewSubKey1 = Registry.CurrentUser.OpenSubKey(
             "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\NamedPipes", True)
    
         If NewSubKey1 IsNot Nothing Then
                
             Label8.Text = NewSubKey1.GetValue("Version", NewSubKey1)
             NewSubKey1.Close()
         End If
     End Sub
    
     Sub SetReg3()
         NewSubKey3 = Registry.CurrentUser.OpenSubKey(
            "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\SharedMemory", True)
    
         If NewSubKey3 IsNot Nothing Then
                
             Label7.Text = NewSubKey3.GetValue("Version", 0)
             Label7.Text += 1
             NewSubKey3.SetValue("Version", Val(Label7.Text))
             NewSubKey3.Close()
         End If
     End Sub

But it gives me an error("Conversion from type 'RegistryKey' to type 'String' is not valid") when it creates for the first time ,
How can i get rid of this error
Any suggestions ......................

dotnet-visual-basic
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AmrAli4235 avatar image
0 Votes"
AmrAli4235 answered ·

I solved my issue , So i want to share the solution here with you . May it helps someone ...
The rearrange of (CreateReg(), SetReg1(), SetReg3() then GetReg1() ) really it didn't fix the problem,
Because in SetReg1() i set the reg value to ("2020-03-01")for the first time only (after my app. open, i go to my admin panel to set the reg date to "2021-03-01") and when i close my app. and open it again, It returns to the same date ("2020-03-01"). Here is the problem and fixed it by made some another modification to my code like this

  Private Sub LoginForm19_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
         CreateReg()
         SetReg3()
           
         If Label7.Text > 0 Then
             GetReg1()
             
         Else
             SetReg1()
               
         End If
    
     End Sub

 Sub SetReg3()
         NewSubKey3 = Registry.CurrentUser.OpenSubKey(     "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\SharedMemory", True)
    
         If NewSubKey3 IsNot Nothing Then
             Label7.Text = NewSubKey3.GetValue("Version", 0)
             Label7.Text += 1
             NewSubKey3.SetValue("Version", Val(Label7.Text))
             NewSubKey3.Close()
         End If
     End Sub

And i made SetReg3() also in the Form1_Load to insure that the number that will appear to the label7.text is larger than 1 .
Finally this strategy solved my issue ........................

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you also fix the NewSubKey1.GetValue("Version", NewSubKey1) expression, which probably generated the original error?

0 Votes 0 ·

Thanks @Viorel-1 for paying attention to details.

My last modifications - as i mentioned in my answer - fixed the error , Because i need it like this



 Sub GetReg1()
          NewSubKey1 = Registry.CurrentUser.OpenSubKey(        "SOFTWARE\\Microsoft\\SQTClient\\SQLV2017\\Microsoft\\SQLServerAgent\\ClientsProtocol\\NamedPipes", True)
        
          If NewSubKey1 IsNot Nothing Then      
              Label8.Text = NewSubKey1.GetValue("Version", NewSubKey1)
              NewSubKey1.Close()
          End If
      End Sub

In order to populate a new date(from the reg key) that i set it before from my admin panel to change the date from ("2020-03-01") to whatever i want like ("2021-03-01")

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

Which line throws the error?

Try replacing

 Label8.Text = NewSubKey1.GetValue("Version", NewSubKey1)

with something like

 Label8.Text = NewSubKey1.GetValue("Version", "2020-03-01") 

By the way, I think that you can write “\” instead of “\\”.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @Viorel-1
My question waited a permission so i have the time to make some modifications,
My problem has solved by rearrange the sequence of these code from

CreateReg()
GetReg1()
SetReg1()
SetReg3()

TO
CreateReg()
SetReg1()
SetReg3()
GetReg1()

It's working well now .... I had to set a reg key before i get it again, when i realized that, I fixed it...
Thanks for paying attention

0 Votes 0 ·