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

Amr_Ali 136 Reputation points
2020-12-08T15:13:54.667+00:00

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

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amr_Ali 136 Reputation points
    2020-12-14T09:43:42.897+00:00

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


1 additional answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2020-12-08T16:29:30.123+00:00

    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 “\”.