|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Function 'GetData' doesn't return a value on all code paths...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 ------------------------------------------------- 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 "VoTiger" <christophe.marcel***@gmail.com> schreef in bericht Yes, I tried to do that at the end "Return Getdata" but then I had this :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 > 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 ------------------------------------------------- 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
Show quote
Hide quote
"VoTiger" <christophe.marcel***@gmail.com> schreef in bericht in the catch section I had already a messagebox, and I added Getdata = 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 > Nothing. I got no warnings anymore. Thanks ! 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 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 What will be returned if the DataTable constructor throws an exception?> 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 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
Reading one record from an Access DB
dataser design considerations converting short to unicode without System.Text.UnicodeEncoding ?Delegates and Interaces - similarities/differences? Mdi Form Validating VB.NET ComboBox _SelectedIndexChanged checking MP3 license Treeview problem How to find files in a directory Can I write and read a STRUCTURE to a file or to a Database??? |
|||||||||||||||||||||||