i have this function and it is giving me this warning. and i have many similar functions
so by the time i'll be done i'll have a thousand warnings how can i get rid of the warnings

Function 'SelectRows_local' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

Public Function SelectRows_Web(ByVal query As String) As DataTable
        'Dim Conn_web As SqlConnection
        Dim Conn_web As SqlConnection

        Conn_web = MyFormz.connec.ConnWeb

        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        dt.Clear()
        da.SelectCommand = New SqlCommand(query, Conn_web)

        Try

            da.Fill(dt)

            Return dt

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Function

Its just a waring. It means what it says. Your not returning a value on all code paths. Your lacking the case of an exception.

If you want it to go away, Just rearange your code

This will return a data table on any result.

Try

            da.Fill(dt)
         
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try

        Return dt

    End Function

This will return nothing on an error.

Try

            da.Fill(dt)
            Return dt    
        Catch ex As Exception
            'MsgBox(ex.Message)
            Return Nothing
        End Try

        Return dt

    End Function

Either one will remove your warning.

You are being warned that your function has a path it can go through that will not return a value to whatever called it, normally this will result in a runtime error.

In your example if your da.Fill command fails for some unforeseen reason, the Try... Catch block will catch the exception and the function will then carry on and return no value.

Although you can indeed turn off this warning, or ignore it completely as it stands (It is only a warning after all, not a design error), it is exceedingly poor practise to do so and you will inevitably encounter unhandled runtime exceptions when things you didn't predict go wrong (e.g. connection to the database being terminated abruptly).

This can be remedied in a few ways, one of which (As the above poster has mentioned) is to return a value of nothing and have the calling method check for this, another is to return a blank data table and have your calling method deal with it normally.

However, if you really feel the need to simply remove the warning instead of correcting the code, you can do so by right clicking on your solution in the explorer and going to the compile tab and change 'Function Returning Reference Type Without Return Value' from warning to none.

(I know this thread is marked as solved, but I felt that it hadn't actually answered his question on how to disable the warning message or elaborated as to why the warning was cropping up in the first place and the seriousness of the warning in question)

Edited 5 Years Ago by Kinney: n/a

Thanks Kinney for the extra input.

You are being warned that your function has a path it can go through that will not return a value to whatever called it, normally this will result in a runtime error.

In your example if your da.Fill command fails for some unforeseen reason, the Try... Catch block will catch the exception and the function will then carry on and return no value.

Its just a waring. It means what it says. Your not returning a value on all code paths. Your lacking the case of an exception

Although you can indeed turn off this warning, or ignore it completely as it stands (It is only a warning after all, not a design error), it is exceedingly poor practise to do so and you will inevitably encounter unhandled runtime exceptions when things you didn't predict go wrong (e.g. connection to the database being terminated abruptly).

Never asked how to disable or ignore. Only how to get rid of the warnings.

This can be remedied in a few ways, one of which (As the above poster has mentioned) is to return a value of nothing and have the calling method check for this, another is to return a blank data table and have your calling method deal with it normally.

2 examples above. One returns nothing. One returns a blank data table.

However, if you really feel the need to simply remove the warning instead of correcting the code, you can do so by right clicking on your solution in the explorer and going to the compile tab and change 'Function Returning Reference Type Without Return Value' from warning to none.

Once again, Never asked how to disable the warning in Visual Studio.

(I know this thread is marked as solved, but I felt that it hadn't actually answered his question on how to disable the warning message or elaborated as to why the warning was cropping up in the first place and the seriousness of the warning in question)

It is solved. And answered.

This question has already been answered. Start a new discussion instead.