So I have a function when fired queries against the database (twice) and returns the results in a dataset. It checks the results (to make sure there are some) then loops through and grabs each row from the returned dataset and imports (copies) it to a different dataset.
I also add a primary key into a List so I do not add the same item twice (from the 2nd query) to the dataset.
I then return the dataset.
The problem? The query works and there is a returned value.. however the dataset in which I aim to import the row(s) to does not keep the imported row.
Please make any suggestions regarding my approach and the issue at hand; I’m always looking to improve!
Public Function GetClientsWithMonitors(ByVal argHost As FOO.Interfaces.BAR) As DataSet
Try
Dim localDataSet As New DataSet()
Dim clientsWithMonitors As New DataSet()
Dim tempList As New List(Of Integer)
clientsWithMonitors.Clear()
localDataSet.Clear()
tempList.Clear()
clientsWithMonitors.Tables.Add()
'SQL getting monitors applied to clients
Dim clientSQL As String = "SELECT DISTINCT c.ClientID, c.Name FROM agents a LEFT JOIN clients c ON c.ClientID = a.ClientID WHERE a.ClientID > 0"
'SQL getting monitors applied directly to an agent
Dim agentSQL As String = "SELECT DISTINCT c.ClientID, c.Name FROM clients c LEFT JOIN computers comp ON c.ClientID = comp.ClientID LEFT JOIN agents a ON comp.ComputerID = a.ComputerID WHERE a.LocID = 0 AND a.ClientID = 0 AND a.ComputerID > 0"
localDataSet = argHost.GetDataSet(clientSQL)
If localDataSet.Tables.Count > 0 AndAlso localDataSet.Tables(0).Rows.Count > 0 Then
For Each row As DataRow In localDataSet.Tables(0).Rows
If Not tempList.Contains(CInt(row("ClientID").ToString())) Then
Dim clientID As Integer = CInt(row("ClientID").ToString())
clientsWithMonitors.Tables(0).ImportRow(row)
tempList.Add(clientID)
End If
Next
End If
If localDataSet.Tables.Count > 0 AndAlso localDataSet.Tables(0).Rows.Count > 0 Then
localDataSet.Clear()
localDataSet = argHost.GetDataSet(agentSQL)
For Each row As DataRow In localDataSet.Tables(0).Rows
If Not tempList.Contains(CInt(row("ClientID").ToString())) Then
Dim clientID As Integer = CInt(row("ClientID").ToString())
clientsWithMonitors.Tables(0).ImportRow(row)
tempList.Add(clientID)
End If
Next
End If
Return clientsWithMonitors
Catch ex As Exception
LogEventViaHost(argHost, "Error Getting dataset of clients with a specified monitor" & ex.StackTrace & " " & ex.Message)
Return Nothing
End Try
Column names must be explicitly stated; for some reason I was thinking the dataset it would implicitly inherit the column names from the imported datarow row.