Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello sir i want to make registry , here is my code..
C#
RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\NERP");
if (key == null)
{
    locServername  = txt_locServer .Text; //.
    locDatabaseName  = txt_locDb .Text;//employee
    locUsername  = txt_locName .Text;//sa
    locPwd  = txt_locPwd .Text;//.
    MainServername = txt_ServerName.Text;
    MainUsername = txt_ServerLoginName.Text ;
    MainPwd = txt_Severpwd.Text;
    
    
    key.SetValue("LocServer", locServername);
    key.SetValue("LocDB", locDatabaseName);
    key.SetValue("LocUsername", locUsername);
    key.SetValue("LocPassword", locPwd);
    key.SetValue("Mainserver", MainServername);
    key.SetValue("MainUsername", MainUsername);
    key.SetValue("MainPwd", MainPwd);
    
    MessageBox.Show("Registry created");
    this.Close();


when i run this program there is error like
object reference not set to an instance of an object" at this line
key.SetValue("LocServer", locServername);

in the "locServername" I pass value ".";
sir tell me how to do this.
i want to make connection string look like this
datasource=.;database=employee;user id=sa;password='';
Posted
Updated 3-Oct-14 22:00pm
v3

The statement
C#
if (key == null)

in your code, means that you will only execute the following code if your call to OpenSubKey fails. So you are guaranteed to get that error. It should be
C#
if (key != null)

And you should also investigate why your call to OpenSubKey is failing.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Oct-14 7:38am    
Well-spotted, a 5.
—SA
Your error is by design: the following condition
Quote:
if (key == null)
guarantees that key is null when the
Quote:
key.SetValue("LocServer", locServername);
is reached.



You missed a step: create the key if it does not already exist.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Oct-14 7:38am    
Well-spotted, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900