Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.73/5 (4 votes)
See more:
I want to call a function in an 'If' statement, I have done this before but it was different. This is how I did it before:

VB
If Not validatePhone(txtContactNumber.Text) Then 
......
End If


That is straight forward for me because I know what to put in the brackets, but I want to call a function that checks that a database:

VB
If Not checkDatabaseExists() Then
......
End If

I do not know what parameters go into the brackets here. This is the code for the function 'checkDatabaseExists':

VB
Public Shared Function checkDatabaseExists(ByVal server As String, ByVal database As String) As Boolean
        Dim conString As String = ("Data Source=.\SQLExpress;Initial Catalog=Visitors;Integrated Security=True;MultipleActiveResultSets=True")
        Dim cmdText As String = ("SELECT * FROM master.dbo.sysdatabases WHERE Name=Visitors")
        Dim databaseExists As Boolean = False
        Using sqlConnection As SqlConnection = New SqlConnection(conString)
            sqlConnection.Open()
            Using sqlCmd As SqlCommand = New SqlCommand(cmdText, sqlConnection)
                Using reader As SqlDataReader = sqlCmd.ExecuteReader
                    databaseExists = reader.HasRows
                End Using
            End Using
        End Using
        Return databaseExists
    End Function


Can anyone help me? I know it's something simple and I'll be kicking myself when I find out what it is but I haven't a clue at the moment unfortunately. All help with this matter would be greatly appreciated.

Edit:

I get the following error without having parameters in the checkDatabaseExists() part of the 'If' statement:

'Argument not specified for parameter "database" of "Public Shared Function checkDatabaseExists(ByVal server As String, ByVal database As String) As Boolean" '
Posted
Updated 7-Aug-14 0:34am
v2
Comments
Leo Chapiro 7-Aug-14 6:17am    
Your checkDatabaseExists get two parameters that will be never used: ByVal server As String, ByVal database As String. Looked at that way you can call it without parameter: If Not checkDatabaseExists() Then
Member 10804519 7-Aug-14 6:35am    
Hi @_duDE_, see the edit on the question, I get an error without parameters.
[no name] 7-Aug-14 6:41am    
Since you are not using the parameters, why don't you simply remove them? Of course you will get an error if you call a function without supplying the parameters it expects.
Member 10804519 7-Aug-14 6:45am    
Thanks @Wes Aday, that worked. I should have known to do that.

1 solution

Answered only to remove from the unanswered queue.
 
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