Link to home
Start Free TrialLog in
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

Avatar of crisco96
crisco96
Flag of United States of America image

What was the original C#?
Avatar of Jorge Paulino
Try:

Dim list As New list(Of Integer)

More info: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
Humm it seams your're adding string on the list! If so use

Dim list As New list(Of String)
Avatar of jtammyg
jtammyg

ASKER

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
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jtammyg

ASKER

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