By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

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

P: 42
Expand|Select|Wrap|Line Numbers
  1. Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\CATS\Conn")
  2.  
  3.     Dim DS As Object = key.GetValue("DataSource")->(Error) "object referenc not set to inestance of an object"(/Error) 
  4.     Dim DB As Object = key.GetValue("DataBase")
  5.     Dim Uid As Object = key.GetValue("UserName")
  6.     Dim Pass As Object = EncDecCls.EncryptClass.DecryptTripleDES(key.GetValue("Password"))
  7.  Dim con As New SqlConnection( _
  8.       "Data Source =" & DS & ";" & _
  9.     "Database =" & DB & ";" & _
  10. " User id =" & Uid & "; Password =" & Pass & ";")
  11.  
  12.  




please help me
Apr 3 '13 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
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.
Apr 3 '13 #2

P: 31
HI ! I agree with r035198x. Please Check it.
Apr 3 '13 #3

P: 42
I checked the name
but show this error

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

are registrykey accept the server name (local)
Apr 3 '13 #4

100+
P: 158
Hey mohammed saleh, you're getting this error because inside the 'HKEY_LOCAL_MACHINE -> 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,

Expand|Select|Wrap|Line Numbers
  1. bool found = false;
  2.  
  3. RegistryKey rs1 = Registry.LocalMachine;
  4. RegistryKey rs2 = rs1.OpenSubKey("SOFTWARE\\CATS\\Conn");
  5.  
  6. string[] allkeynames = rs2.GetValueNames();
  7.  
  8. foreach(var temp in allkeynames)
  9. {
  10. if (temp.Equals("DataSource"))
  11. {
  12. found = true;
  13. break;
  14. }
  15. }
  16. if(found)
  17. Console.WriteLine("Key exists");
  18. else
  19. Console.WriteLine("Key doesn't exists");
Apr 4 '13 #5

P: 42
This problem is solved

I used Text File connection string


The answer:
Expand|Select|Wrap|Line Numbers
  1.  Public Function Readconnectionstring() As String
  2.         Try
  3.  
  4.             Dim pass As String = ""
  5.             Dim path As String = "C:\connection\Textfile.txt"
  6.             Dim sw As StreamWriter = New StreamWriter(path)
  7.             sw.WriteLine()
  8.             EncDecCls.EncryptClass.EncryptTripleDES(pass)
  9.  
  10.  
  11.             Dim sr As StreamReader = New StreamReader(path)
  12.             Dim line As String = sr.ReadLine()
  13.  
  14.  
  15.  
  16.             Dim ds As String = sr.ReadLine()
  17.             Dim db As String = sr.ReadLine()
  18.             Dim uid As String = sr.ReadLine()
  19.             pass = EncDecCls.EncryptClass.EncryptTripleDES(sr.ReadLine())
  20.  
  21.  
  22.             cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  23.         Catch ex As FileNotFoundException
  24.             MsgBox(ex.Message)
  25.         End Try
  26.         Return cons
  27.     End Function
Apr 18 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.