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

  • Thread starter Screaming Eagles 101
  • Start date
S

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
-------------------------------------------------
 
V

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,
 
S

Screaming Eagles 101

VoTiger said:
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,

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
-------------------------------------------------
 
V

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,
 
L

Larry Lard

Screaming said:
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.
 
S

Screaming Eagles 101

VoTiger said:
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,

in the catch section I had already a messagebox, and I added Getdata =
Nothing.
I got no warnings anymore.
Thanks !
 
V

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top