Avatar of jtammyg
jtammyg asked on

converted code from c# to Vb and getting error

Hi!

I have the following code which intends to get me a listbox of all the databases in a specific sql server. But I am getting an error at the

dim list as list = new list(32)   <---- i have no idea what this does...this was converted online through a website.

The error I am getting is:

too few type arguments to 'system.collections.generic.list(of T)'


any help you can give me on this will be greatly appreciated.

thanks!

Regards,

Tammy
Public Shared Function FindDatabases(ByVal server As String) As String()
        Dim connectionString As String = (TrustedConnection(server, "master") + ";Connection Timeout=3")
        Dim connection As SqlConnection = Nothing
        Dim command As SqlCommand = Nothing
        Dim reader As SqlDataReader = Nothing
        Dim list As List = New List(32)
        Try
            connection = New SqlConnection(connectionString)
            command = New SqlCommand("DECLARE @t TABLE(db sysname NOT NULL PRIMARY KEY) INSERT @t(db) SELECT name FROM master.dbo.sysdataba" & _
                "ses WITH(NOLOCK) WHERE name NOT IN(N'SpecialData',N'master',N'tempdb',N'model',N'msdb') AND" & _
                " HAS_DBACCESS(name)=1 SELECT db FROM @t WHERE OBJECT_ID(N'[' + db + N'].dbo.table_version','R') I" & _
                "S NOT NULL ORDER BY db", connection)
            connection.Open()
            reader = command.ExecuteReader((CommandBehavior.CloseConnection Or CommandBehavior.SingleResult))
 
            While reader.Read
                list.Add(reader.GetString(0))
 
            End While
        Finally
            If ((Not (reader) Is Nothing) _
                        AndAlso Not reader.IsClosed) Then
                reader.Close()
            End If
            If ((Not (connection) Is Nothing) _
                        AndAlso (connection.State <> ConnectionState.Closed)) Then
                connection.Close()
            End If
        End Try
        Return list.ToArray
    End Function

Open in new window

.NET ProgrammingMicrosoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
jtammyg

8/22/2022 - Mon
crisco96

What was the original C#?
Jorge Paulino

Try:

Dim list As New list(Of Integer)

More info: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Jorge Paulino

Humm it seams your're adding string on the list! If so use

Dim list As New list(Of String)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
jtammyg

This was the original C# code:

  public static string[] FindDatabases(string server)
    {
        string connectionString = TrustedConnection(server, "master") + ";Connection Timeout=3";
        SqlConnection connection = null;
        SqlCommand command = null;
        SqlDataReader reader = null;
        List<string> list = new List<string>(0x20);
        try
        {
            connection = new SqlConnection(connectionString);
            command = new SqlCommand("DECLARE @t TABLE(db sysname NOT NULL PRIMARY KEY) INSERT @t(db) SELECT name FROM master.dbo.sysdatabases WITH(NOLOCK) WHERE name NOT IN(N'N'SpecialData'',N'master',N'tempdb',N'model',N'msdb') AND HAS_DBACCESS(name)=1 SELECT db FROM @t WHERE OBJECT_ID(N'[' + db + N'].dbo.table_version','R') IS NOT NULL ORDER BY db", connection);
            connection.Open();
            reader = command.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult);
            while (reader.Read())
            {
                list.Add(reader.GetString(0));
            }
        }
        finally
        {
            if ((reader != null) && !reader.IsClosed)
            {
                reader.Close();
            }
            if ((connection != null) && (connection.State != ConnectionState.Closed))
            {
                connection.Close();
            }
        }
        return list.ToArray();
    }

ASKER CERTIFIED SOLUTION
Jorge Paulino

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
jtammyg

Okay that works...except that the 2 listboxes don't get populated with the servers in the 1 listbox and the databases in the second listbox...but i will post another question for that one.

Thanks a lot jpaulino!!!!!

Tammy