473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
-------------------------------------------------
Jul 13 '06 #1
6 4092
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

Jul 13 '06 #2

"VoTiger" <ch******************@gmail.comschreef in bericht
news:11**********************@h48g2000cwc.googlegr oups.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
-------------------------------------------------
Jul 13 '06 #3
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

Jul 13 '06 #4

Screaming Eagles 101 wrote:
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

Jul 13 '06 #5

"VoTiger" <ch******************@gmail.comschreef in bericht
news:11**********************@h48g2000cwc.googlegr oups.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 !
Jul 13 '06 #6
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

Jul 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: a | last post by:
I have been cleaning up somebody else's code in Vs2005 .net 2.0 There wer lots of these code pat errors because of unitialized variables in functions with Conditionals - that may or may not be...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.