First thing there is nothing wrong with what you did.
But since you asked :)
First I would create a stored procedure
CREATE PROCEDURE [dbo].[Asset_Service_Master_Get_Counts]
@CaseStatus VARCHAR(50) = NULL,
@CallClosed VARCHAR(50) = NULL,
@BillingComplete VARCHAR(50) = NULL
AS
BEGIN
SELECT COUNT(*)
FROM Asset_Service_Master
WHERE CaseStatus = COALESCE(@CaseStatus, Asset_Service_Master.CaseStatus)
AND CallClosed = COALESCE(@CallClosed,Asset_Service_Master.CallClosed)
AND BillingComplete = COALESCE(@BillingComplete,
Asset_Service_Master.BillingComplete)
END
GO
Here is the method I would use.
Public Function GetCount(Optional ByVal caseStatus As String = "", Optional ByVal callClosed As String = "", Optional ByVal billingComplete As String = "") As String
Using cn As New SqlConnection(connectionString)
cn.Open()
If cn.State = ConnectionState.Open Then
Using cmd As New SqlCommand("Asset_Service_Master_Get_Counts", cn)
cmd.Parameters.Add(New SqlParameter("@CaseStatus", IIf(caseStatus = "", DBNull.Value, caseStatus)))
cmd.Parameters.Add(New SqlParameter("@CallClosed", IIf(callClosed = "", DBNull.Value, callClosed)))
cmd.Parameters.Add(New SqlParameter("@BillingComplete", IIf(billingComplete = "", DBNull.Value, billingComplete)))
cmd.CommandType = CommandType.StoredProcedure
Return cmd.ExecuteScalar().ToString
End Using
Else
Return String.Empty
End If
End Using
End Function