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

problem save and reading from the registry

P: n/a
Hi,

I want to store some data in the registry, however I have not been able to
do this, and think my logic maybe flawed.

Firstly I try to open the registry and read in any existing values.
Dim aKey As RegistryKey
aKey = Registry.CurrentUser.OpenSubKey("software\myApplic ation")

I then attempt to read in any existing values
strServerName = aKey.GetValue("ServerName")
strLoginName = aKey.GetValue("LoginName")

When the first getValue is executed, i get an error 'object reference not
set to an instance of an object', which I guess is fine as this registry
entry doesn't exist yet.

If the registry entry doesn't exist (say first time the application is run),
then I capture the error on the above code and create the registry subkey=
Registry.CurrentConfig.CreateSubKey("software\myAp plication") ' No error on
this line

After the user has logged in, i want to save the user id and server name to
the registry, so that next time they use the program, step 1 will populate
the login form with the last login details used.

aKey.SetValue("ServerName", frmSPlash.txtServer.Text)
aKey.SetValue("LoginName", frmSPlash.txtUserID.Text)

When the first setvalue is execute i get an error 'object reference not set
to an instance of an object'. which i would not expect as it should be just
creating the entry.

What have I done wrong here......

THanks

Aug 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Have you looked at the app.config file? This replaces storing settings
in the registry for .NET apps. If you using VS 2005 it's very simple:

'Get Value
Value = My.Settings.[SettingsName]

'Set Value
My.Settings.[SettingsName] = Value
Aussie Rules wrote:
Hi,

I want to store some data in the registry, however I have not been able to
do this, and think my logic maybe flawed.

Firstly I try to open the registry and read in any existing values.
Dim aKey As RegistryKey
aKey = Registry.CurrentUser.OpenSubKey("software\myApplic ation")

I then attempt to read in any existing values
strServerName = aKey.GetValue("ServerName")
strLoginName = aKey.GetValue("LoginName")

When the first getValue is executed, i get an error 'object reference not
set to an instance of an object', which I guess is fine as this registry
entry doesn't exist yet.

If the registry entry doesn't exist (say first time the application is run),
then I capture the error on the above code and create the registry subkey=
Registry.CurrentConfig.CreateSubKey("software\myAp plication") ' No error on
this line

After the user has logged in, i want to save the user id and server name to
the registry, so that next time they use the program, step 1 will populate
the login form with the last login details used.

aKey.SetValue("ServerName", frmSPlash.txtServer.Text)
aKey.SetValue("LoginName", frmSPlash.txtUserID.Text)

When the first setvalue is execute i get an error 'object reference not set
to an instance of an object'. which i would not expect as it should be just
creating the entry.

What have I done wrong here......

THanks
Aug 24 '06 #2

P: n/a
I think the problem is that you read from a different location then you
write to:

Registry.CurrentUser.OpenSubKey("software\myApplic ation")
Registry.CurrentConfig.CreateSubKey("software\myAp plication")

Below is a working sample, hope this helps:

Greetz Peter

Dim myKey As RegistryKey
myKey = Registry.CurrentUser.OpenSubKey("Software\testNG")
If myKey Is Nothing Then
myKey = Registry.CurrentUser.OpenSubKey("Software", True)
myKey.CreateSubKey("testNG")
myKey = Registry.CurrentUser.OpenSubKey("Software\testNG", True)
myKey.SetValue("testNG", "Peter Proost")
Else
MsgBox(myKey.GetValue("testNG"))
End If

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Aussie Rules" <Au*********@nospam.nospamschreef in bericht
news:#t**************@TK2MSFTNGP02.phx.gbl...
Hi,

I want to store some data in the registry, however I have not been able to
do this, and think my logic maybe flawed.

Firstly I try to open the registry and read in any existing values.
Dim aKey As RegistryKey
aKey = Registry.CurrentUser.OpenSubKey("software\myApplic ation")

I then attempt to read in any existing values
strServerName = aKey.GetValue("ServerName")
strLoginName = aKey.GetValue("LoginName")

When the first getValue is executed, i get an error 'object reference not
set to an instance of an object', which I guess is fine as this registry
entry doesn't exist yet.

If the registry entry doesn't exist (say first time the application is
run),
then I capture the error on the above code and create the registry subkey=
Registry.CurrentConfig.CreateSubKey("software\myAp plication") ' No error
on
this line

After the user has logged in, i want to save the user id and server name
to
the registry, so that next time they use the program, step 1 will populate
the login form with the last login details used.

aKey.SetValue("ServerName", frmSPlash.txtServer.Text)
aKey.SetValue("LoginName", frmSPlash.txtUserID.Text)

When the first setvalue is execute i get an error 'object reference not
set
to an instance of an object'. which i would not expect as it should be
just
creating the entry.

What have I done wrong here......

THanks

Aug 25 '06 #3

P: n/a
Hi Aussie,

Additional to Peter's reply:

What you got is the NullReferenceException, which normally means that aKey
is a null reference. The simplest way is set a breakpoint in aKey.SetValue
line, and view aKey variable in Watch Window in VS.net IDE debugger.

If once you find the null reference, you should check why aKey is not
assigned a real object instance.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.