object reference not set to an instance of an object for registry key code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohammed saleh
    New Member
    • Apr 2013
    • 42

    object reference not set to an instance of an object for registry key code

    Code:
    Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\CATS\Conn")
    
        Dim DS As Object = key.GetValue("DataSource")->(Error) "object referenc not set to inestance of an object"(/Error) 
        Dim DB As Object = key.GetValue("DataBase")
        Dim Uid As Object = key.GetValue("UserName")
        Dim Pass As Object = EncDecCls.EncryptClass.DecryptTripleDES(key.GetValue("Password"))
     Dim con As New SqlConnection( _
          "Data Source =" & DS & ";" & _
        "Database =" & DB & ";" & _
    " User id =" & Uid & "; Password =" & Pass & ";")




    please help me
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use code tags when posting code.

    2.) So the registry key you looked up was not found. Check that it really does exist or it's accessible or you are using the correct name format to access it.

    Comment

    • VanessaMeacham
      New Member
      • Sep 2012
      • 31

      #3
      HI ! I agree with r035198x. Please Check it.

      Comment

      • mohammed saleh
        New Member
        • Apr 2013
        • 42

        #4
        I checked the name
        but show this error

        my data source(server name) is (local)
        database moh

        are registrykey accept the server name (local)

        Comment

        • vijay6
          New Member
          • Mar 2010
          • 158

          #5
          Hey mohammed saleh, you're getting this error because inside the 'HKEY_LOCAL_MAC HINE -> Software -> CATS -> Conn' directory 'DataSource' key doesn't exists (r035198x answer is right).

          Before reading the key's value in Registry first you've to check whether that key is already exists or not.

          You can check whether the key is already present or not in registry using the following code,

          Code:
          bool found = false;
          
          RegistryKey rs1 = Registry.LocalMachine;
          RegistryKey rs2 = rs1.OpenSubKey("SOFTWARE\\CATS\\Conn");
          
          string[] allkeynames = rs2.GetValueNames();
                      
          foreach(var temp in allkeynames)
          {
          if (temp.Equals("DataSource"))
          {
          found = true;
          break;
          }
          }
          if(found)
          Console.WriteLine("Key exists");
          else
          Console.WriteLine("Key doesn't exists");

          Comment

          • mohammed saleh
            New Member
            • Apr 2013
            • 42

            #6
            This problem is solved

            I used Text File connection string


            The answer:
            Code:
             Public Function Readconnectionstring() As String
                    Try
            
                        Dim pass As String = ""
                        Dim path As String = "C:\connection\Textfile.txt"
                        Dim sw As StreamWriter = New StreamWriter(path)
                        sw.WriteLine()
                        EncDecCls.EncryptClass.EncryptTripleDES(pass)
            
            
                        Dim sr As StreamReader = New StreamReader(path)
                        Dim line As String = sr.ReadLine()
            
                       
            
                        Dim ds As String = sr.ReadLine()
                        Dim db As String = sr.ReadLine()
                        Dim uid As String = sr.ReadLine()
                        pass = EncDecCls.EncryptClass.EncryptTripleDES(sr.ReadLine())
            
            
                        cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
                    Catch ex As FileNotFoundException
                        MsgBox(ex.Message)
                    End Try
                    Return cons
                End Function

            Comment

            Working...