Question : How do I return a datatable from a stored procedure?

Is is possible for a stored procedure to return a datatable?

I attempted this with the following code:

Public Function GetBillingClassAccountsForAccountTypeAndBillingType(ByVal AccountTypeID As Long, _
                        ByVal BillingTypeID As Long) As DataTable


        Dim MyStoredProc As String = "BillingClassAccountsForAccountTypeAndBillingTypeSelectQuery"
        Dim MyConnectionString As String = My.Settings.DoctorPmtDataConnectionString
        Dim MyConnection As SqlConnection = New SqlConnection(MyConnectionString)

        'Dim NoRowsUpdated As Integer

        Dim MyDataTable As DataTable = New DataTable

        Try

            ' Open Connection
            MyConnection.Open()

            Dim MySqlCommand As New SqlCommand(MyStoredProc, MyConnection)
            MySqlCommand.CommandType = CommandType.StoredProcedure

            ' ...........Parameters

            '@AccountTypeID  int
            Dim MySqlParameter_AccountTypeID As SqlParameter = New SqlParameter("@AccountTypeID", SqlDbType.Int)
            MySqlParameter_AccountTypeID.Value = AccountTypeID
            MySqlCommand.Parameters.Add(MySqlParameter_AccountTypeID)

            '@BillingTypeID  int
            Dim MySqlParameter_BillingTypeID As SqlParameter = New SqlParameter("@BillingTypeID", SqlDbType.Int)
            MySqlParameter_BillingTypeID.Value = AccountTypeID
            MySqlCommand.Parameters.Add(MySqlParameter_BillingTypeID)

            ' Adapter to fill table
            Dim MySqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(MySqlCommand)

            ' Datatable to return
            MySqlDataAdapter.Fill(MyDataTable)
            Return MyDataTable

        Catch ex As SqlException

            Return MyDataTable
            'MsgBox(ex.ToString)

        Finally

            MyConnection.Close()
            MyDataTable = Nothing


        End Try



    End Function

Answer : How do I return a datatable from a stored procedure?

Just like u didt.
are u getting an error?
Random Solutions  
 
programming4us programming4us