Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi guys
I have this function but it gives me a warning.
VB
Public Function IsConnected(ByVal strQry As String, ByVal ver As Boolean)
        Try
            If myConn.State = ConnectionState.Open Then myConn.Close()
            myConn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & dbpath & '"; Jet OLEDB:database password="
            myConn.Open()
            myCmd.CommandText = strQry
            myCmd.Connection = myConn
            If ver = False Then
                myDR = myCmd.ExecuteReader() For reading query
            Else
                myCmd.ExecuteNonQuery() For updating query
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try
    End Function

What is wrong;

Thanks
Posted
Updated 1-Nov-12 22:26pm
v2
Comments
Nelek 2-Nov-12 4:27am    
What warning? In which line? Is it actually working?
bbirajdar 2-Nov-12 4:33am    
The only thing that is wrong is that you have not posted the warning message and the line number where it occurs....Morever the code will not compile.. You have not placed ' for the comments in the code
jomachi 2-Nov-12 4:41am    
Function IsConnected doesn't return a value on all code paths.

The function working right but i don't want that warning

If you're asking me what is wrong with this function, I'd say:

1. The connection string isn't quoted correctly and looks incomplete

VB
myConn.ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & dbpath & '"; Jet OLEDB:database password="


2. There is no return statement, so the example given shouldn't even compile because it's declared as a function rather than a sub

3. The use of a MsgBox strongly couples the function to a windows forms application, you wouldn't be able to use this in ASP.net for example

4. "IsConnected" is a totally misleading name for the function as it doesn't actually tell you if you're connected or not. It looks like it runs SQL statements.

5. The following code:

VB
If ver = False Then
    myDR = myCmd.ExecuteReader() For reading query
Else
    myCmd.ExecuteNonQuery() For updating query
End If


should be simplified to:

VB
If ver Then
    myCmd.ExecuteNonQuery() For updating query
Else
    myDR = myCmd.ExecuteReader() For reading query
End If


because you're evaluating a boolean in the If
 
Share this answer
 
Your function does not return a value, yes. Why?

Because, unlike subs functions are supposed to return a result to the calling proc. Every function must have a return statement and that is why you're getting that warning.

I'm guessing you wanted to determine if you successfully connected to the database there.

Functions are called like so in VB

VB
[Modifier] Function FunctionName [Args] [as type] 
' Your code here.

Return [type] 'Same as declared in the heading.
End Function


Change it to a sub, like so...

VB
Public Sub IsConnected(ByVal strQry As String, ByVal ver As Boolean)
'Your code here.
End Sub


Learn about Subs and Functions in VB.Net or here

For other things wrong with your code... See Solution 1
 
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