0

Hi,

I'm pulling my hair out with this one, I need to populate a combobox with a list of servers from the registry, I think the code I've used is fairly sound, but when I run the project, I get the following error, which I'm sure you're all familiar with Null Reference Exception was Unhandled Object Reference not set to an instance of an object

private void Login_Load(object sender, EventArgs e)
        {
            string strKey = @"HKEY_CURRENT_USER\Software\Datasym UK\Stockade SQL\Server List";

            //if (IntPtr.Size == 8)
            //{
            //    strKey = @"SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server";
            //}

            RegistryKey rkSqlServer = Registry.LocalMachine.OpenSubKey(strKey);
          string[] instances = rkSqlServer.GetValue("InstalledInstances")[/COLOR]
            as string[];
            if (instances.GetUpperBound(0) >= 0)
            {
                 foreach (string element in instances)
                     if (element == "MSSQLSERVER")
                {
                    lstServers.Items.Add(Environment.MachineName);
                }
                else
                {
                    lstServers.Items.Add(Environment.MachineName + @"\" + element);
                }

            }
        }

As anyone any ideas as to where I may be going wrong?

Thanks,

R

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by LizR
0

instead of getvalue - which returns a single value and as you specified no default option, if not found, it would return null..

Do you not mean getsubkeynames?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.