highwingers
highwingers

Reputation: 1669

Return DataTable from a Function

I have a Function which returns DataTable Like this (if there are Rows)...

Protected Function getAideData() As DataTable

    Dim dt As DataTable = DAL.ReturnData("select * from pg_PersonalInfo P Left Join pg_employeeInterview E on E.sesID = P.sesID ")
    If dt.Rows.Count > 0 Then
        Return dt
    End If

End Function

Then later in my page I accress it like this:

Dim d as datatable = getAideData

Here is my problem: If I have Data in Datatable then I have no problem, however if I dont have any data returned from my method then there is a issue.

I guess my question is, if I have a Function and that cannot return a datatable (no rows), then what should I return from my function? So i can handle the data properly later in my application.

Upvotes: 0

Views: 5732

Answers (2)

albin.varghese
albin.varghese

Reputation: 59

Function GetDataTable(ByVal qry As String) As DataTable
        Dim DbCon As New OleDb.OleDbConnection
        Try
            Dim ConStr As String
            ConStr = System.Configuration.ConfigurationManager.AppSettings("ConnCMSTrend").ToString()
            DbCon.ConnectionString = ConStr
            DbCon.Open()
            Dim queryString As String = qry
            Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(queryString, ConStr)
            Dim res As DataSet = New DataSet
            adapter.Fill(res)
            DbCon.Close()
            GetCMSTrend = res.Tables(0)
        Catch ex As Exception
            DbCon.Close()
        End Try
    End Function

'Note That you need to add the connection string in the appsettings file. In the above case ConnCMSTrend is the database connection string.

Upvotes: 0

Habib
Habib

Reputation: 223392

I guess my question is, if I have a Function and that cannot return a datatable (no rows), then what should I return from my function?

Return Nothing

Protected Function getAideData() As DataTable

    Dim dt As DataTable = DAL.ReturnData("select * from pg_PersonalInfo P Left Join pg_employeeInterview E on E.sesID = P.sesID ")
    If dt.Rows.Count > 0 Then
        Return dt
    End If
    Return Nothing

End Function

Upvotes: 2

Related Questions