Hello everyone!
I want to write my connection strings and functions in a class and always call the class when i need to make a connection to my database to avoid code repetition and increase code reuse but i have an error. Here is my code below

Public Class ConnectionString

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Public Shared Function ConnSQL() As SqlConnection
        Dim connectionString As String
        Dim cnn As SqlConnection
        connectionString = "localhost;Initial Catalog=MSS;User ID=;Password="
        cnn = New SqlConnection(connectionString)
        Try
            cnn.Open()
            Return cnn
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        Finally
            cnn.Close()
        End Try

    End Function

The end function is underlined and states this: Function Sqlconn does not return a null value on all code paths

Recommended Answers

All 2 Replies

Try to move your "Return cnn" code to right before your End Function. Hopefully it'll fix. I'm a .net newbie too. :)

already did that and it worked. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.