9 Replies - 6151 Views - Last Post: 03 October 2011 - 10:18 AM Rate Topic: -----

#1 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Error reading from registry

Posted 03 October 2011 - 06:42 AM

I am trying to retrieve some data from a value in registry.

Here is my code for initialization, I replaced the path with ...
RegistryKey rk= Registry.LocalMachine;
        rk = rk.OpenSubKey("HKEY_LOCAL_MACHINE\\...");
        string dbdata=rk.GetValue("configdb");



I get the error ConsoleApplication.Program1.rk is a field but is used like a type. I am not sure what the issue is, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Error reading from registry

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Error reading from registry

Posted 03 October 2011 - 06:48 AM

In which line do you get this error?
Was This Post Helpful? 0
  • +
  • -

#3 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Error reading from registry

Posted 03 October 2011 - 06:54 AM

when i try to build i get

Invalid token '=' in class,struct, or interface member declaration

Invalid token '(' in class,struct, or interface member declration.

Both from line 2

This post has been edited by gordonheimer: 03 October 2011 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5583
  • View blog
  • Posts: 11,942
  • Joined: 02-June 10

Re: Error reading from registry

Posted 03 October 2011 - 07:29 AM

You probably have a problem earlier than the 2 lines you showed us.

My guess is a missing closing brace } or missing semi coloon ;

Either that or you are trying to do the lines you showed us outside of a method. You can't set variables to a value like in line 2, if you aren't in a method.
Was This Post Helpful? 1
  • +
  • -

#5 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Error reading from registry

Posted 03 October 2011 - 07:50 AM

that was exactly the issue, I made a console app to test the connection but now i get the error:Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. Line 15

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        
        static void Main(string[] args)
        {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\\...");
            string dbdata = (string)rk.GetValue("configdb");
            // Console.WriteLine("Connecting to Database...");
            SqlConnection myConnection = new SqlConnection(dbdata);
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}


This post has been edited by gordonheimer: 03 October 2011 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: Error reading from registry

Posted 03 October 2011 - 07:59 AM

Quote

I made a console app to test


One trick that I've really been learning how to take advantage of now: Unit Tests. If you have a version of VS that supports them, I highly suggest you use them. If not, NUnit is free, and very good as well.

The idea is that you don't need to start a console app and write out different functionality to test. You write tests for small chunks of functionality, and then Assert certain things, to prove that what you tested worked. It's very nice for being able to prove out tests like that, and it's also great for regression testing. Click one button, and you'll know if any change you made broke anything else (assuming you set up your tests properly).
Was This Post Helpful? 0
  • +
  • -

#7 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Error reading from registry

Posted 03 October 2011 - 08:06 AM

View PostCurtis Rutland, on 03 October 2011 - 08:59 AM, said:

Quote

I made a console app to test


One trick that I've really been learning how to take advantage of now: Unit Tests. If you have a version of VS that supports them, I highly suggest you use them. If not, NUnit is free, and very good as well.

The idea is that you don't need to start a console app and write out different functionality to test. You write tests for small chunks of functionality, and then Assert certain things, to prove that what you tested worked. It's very nice for being able to prove out tests like that, and it's also great for regression testing. Click one button, and you'll know if any change you made broke anything else (assuming you set up your tests properly).


awesome, ill have to try it out thanks!!

Also the error is line 16, not 15, sorry.

 string dbdata = (string)rk.GetValue("configdb");

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5583
  • View blog
  • Posts: 11,942
  • Joined: 02-June 10

Re: Error reading from registry

Posted 03 October 2011 - 08:49 AM

That makes sense. If the result of the .GetValue is null, then you try to convert that to a string... Null value exception.

Probably means 2 things:
  • There is no registry value in that node by that name
  • You aren't doing any kind of robust coding for unexpected conditions. Your code works if everything is perfect, but crashes outside of the perfect clean-room environment.


Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

Was This Post Helpful? 1
  • +
  • -

#9 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Error reading from registry

Posted 03 October 2011 - 10:13 AM

Quote

That makes sense. If the result of the .GetValue is null, then you try to convert that to a string... Null value exception.


You can actually cast null to any reference type of nullable type without a problem. This suggests the problem is with this line:

RegistryKey rk = Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\\...");


Regardless, the point is the same. The call to OpenSubKey() isn't finding the key, so it is returning null. You then call GetValue() on null, which throws the NullReferenceException.

This bit:

RegistryKey rk = Registry.LocalMachine;


returns the HKEY_LOCAL_MACHINE key, so you don't need to include that in the 'path' you pass to OpenSubKey().

For example, if I wanted the SOFTWARE key, I could do this:

RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE");


Obviously also double check that the key is in exactly the same place as your path points to.
Was This Post Helpful? 1
  • +
  • -

#10 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Error reading from registry

Posted 03 October 2011 - 10:18 AM

View PostCodingSup3rnatur@l-360, on 03 October 2011 - 11:13 AM, said:

Quote

That makes sense. If the result of the .GetValue is null, then you try to convert that to a string... Null value exception.


You can actually cast null to any reference type of nullable type without a problem. This suggests the problem is with this line:

RegistryKey rk = Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\\...");


Regardless, the point is the same. The call to OpenSubKey() isn't finding the key, so it is returning null. You then call GetValue() on null, which throws the NullReferenceException.

This bit:

RegistryKey rk = Registry.LocalMachine;


returns the HKEY_LOCAL_MACHINE key, so you don't need to include that in the 'path' you pass to OpenSubKey().

For example, if I wanted the SOFTWARE key, I could do this:

RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE");


Obviously also double check that the key is in exactly the same place as your path points to.



Thanks! After going through some testing it seems like it is a permission issue!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1