Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok. I have the follow code. This puts the Instance in the combobox but i need it to put the computer name and instance so i can connect to the database properly.

This is what i need all the instances in the combo box to look like.


Computername\Instance
Computername\Instance

This is what it does now

Instance
Instance

This is probably a very easy question to answer i just cannot figure it out for the life of me.


VB
compname = System.Net.Dns.GetHostName()

Dim regKey As Microsoft.Win32.RegistryKey = _
           Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")
        Dim instances() As String = DirectCast(regKey.GetValueNames(), String())

        ComboServers.DataSource = instances
Posted
Comments
ZurdoDev 31-Oct-13 13:18pm    
Does compname not give you what you need?

You are assigning the list of instance names to the DataSource - but you have not combined it with the name of the computer.

You need to iterate the set of names in instances() and, for each one, add (prefix) the computer name and the '\' character.

I'm sorry I don't use VB.Net,so can't give you VB code; in C#, using Linq, it would look something like this:

C#
ComboServers.DataSource = instances.Select(n => compname + "\\" + n).ToList()


Hope that helps,

Chris
 
Share this answer
 
Comments
Member 10272248 5-Nov-13 16:20pm    
Yes perfect! Thanks!

I converted it to Vb.NET below for anyone else that may need it.
For anyone that needs this is VB.NET this is the converted answer from above.

VB
Dim regKey As Microsoft.Win32.RegistryKey = _
               Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")
            Dim instances() As String = DirectCast(regKey.GetValueNames(), String())

            ComboServers.DataSource = instances.[Select](Function(n) compname & "\" & Convert.ToString(n)).ToList()

        Catch ex As Exception
            MsgBox("Could not get SQL instances. You will have to type in the instance manually.", MsgBoxStyle.Exclamation)
        End Try
 
Share this answer
 
I'm not sure what kind of issue do you have...

Please, see this: How to enum SQL Server instances in network[^]
 
Share this answer
 

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