Home All Groups Group Topic Archive Search About

Function 'GetData' doesn't return a value on all code paths...

Author
13 Jul 2006 8:31 AM
Screaming Eagles 101
Hi, I got this warning, but I don't have a clue on how to resolve it the
best way,
maybe one of you can help.
Application is running smoothly, it's only a warning, but I'd like to
resolve it.

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

and this is the code:
Public Function GetData(ByVal ConnectionType As String, ByVal
ConnectionString As String, ByVal strQuery As String) As DataTable
    Try

        GetData = New DataTable

        If ConnectionType = "SQL" Then

            Dim da As SqlDataAdapter

            da = New SqlDataAdapter(strQuery, ConnectionString)

            da.Fill(GetData)

        Else

            Dim da As OdbcDataAdapter

            da = New OdbcDataAdapter(strQuery, ConnectionString)

            da.Fill(GetData)

        End If

    Catch ex As Exception

        MsgBox(ex.Message)

    End Try

End Function


--
Filip
http://users.skynet.be/101airborne
Official Site of the 101st Airborne - 463rd PFA
skype: airborne463pfa-fiwi
-------------------------------------------------

Author
13 Jul 2006 8:46 AM
VoTiger
You have missed the return so that's why you have that warning. If you
have a function, always set a return something.

Hope this help,

Regards,

----

C.Marcelino
Author
13 Jul 2006 8:57 AM
Screaming Eagles 101
"VoTiger" <christophe.marcel***@gmail.com> schreef in bericht
news:1152780370.847211.242670@h48g2000cwc.googlegroups.com...
> You have missed the return so that's why you have that warning. If you
> have a function, always set a return something.
>
> Hope this help,
>
> Regards,
>
> ----
>
> C.Marcelino
>

Yes,  I tried to do that at the end "Return Getdata" but then I had this :
Variable 'GetData' is used before it has been assigned a value. A null
reference exception could result at runtime.
--
Filip
http://users.skynet.be/101airborne
Official Site of the 101st Airborne - 463rd PFA
skype: airborne463pfa-fiwi
-------------------------------------------------
Author
13 Jul 2006 9:24 AM
VoTiger
It's seems to be normal because you are making :
GetData = new datatable
But you are not using it... try to put a value in it and then return
it. Or if it's normal that you don't set it with values, just put it to
NULL.

I am waiting for more.

Regards,

----

C.Marcelino
Author
13 Jul 2006 11:19 AM
Screaming Eagles 101
Show quote Hide quote
"VoTiger" <christophe.marcel***@gmail.com> schreef in bericht
news:1152782687.385057.113000@h48g2000cwc.googlegroups.com...
> It's seems to be normal because you are making :
> GetData = new datatable
> But you are not using it... try to put a value in it and then return
> it. Or if it's normal that you don't set it with values, just put it to
> NULL.
>
> I am waiting for more.
>
> Regards,
>
> ----
>
> C.Marcelino
>

in the catch section I had already a messagebox, and I added Getdata =
Nothing.
I got no warnings anymore.
Thanks !
Author
13 Jul 2006 12:35 PM
VoTiger
No problem you are welcome. Just think like this : a SUB returns
nothing so no need to put "return" but a FUNCTION always return a thing
so ... use "return blablabla".

Regards,

----

C.Marcelino
Author
13 Jul 2006 9:57 AM
Larry Lard
Screaming Eagles 101 wrote:
Show quoteHide quote
> Hi, I got this warning, but I don't have a clue on how to resolve it the
> best way,
> maybe one of you can help.
> Application is running smoothly, it's only a warning, but I'd like to
> resolve it.
>
> This is the Warning
> Function 'GetData' doesn't return a value on all code paths. A null
> reference exception could occur at run time when the result is used.
>
> and this is the code:
> Public Function GetData(ByVal ConnectionType As String, ByVal
> ConnectionString As String, ByVal strQuery As String) As DataTable
>     Try
>
>         GetData = New DataTable
>
>         If ConnectionType = "SQL" Then
>
>             Dim da As SqlDataAdapter
>
>             da = New SqlDataAdapter(strQuery, ConnectionString)
>
>             da.Fill(GetData)
>
>         Else
>
>             Dim da As OdbcDataAdapter
>
>             da = New OdbcDataAdapter(strQuery, ConnectionString)
>
>             da.Fill(GetData)
>
>         End If
>
>     Catch ex As Exception
>
>         MsgBox(ex.Message)
>
>     End Try
>
> End Function

What will be returned if the DataTable constructor throws an exception?

Put a Return statement in the Catch block.

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using