Return DataRow

S

samoore33

I use the code below to search through a DataSet:

Dim t As DataTable
t = result.Tables("State")
Dim strExpr As String
strExpr = "id = '" & theState.ToString() & "'"
Dim foundRows() As DataRow
foundRows = t.Select(strExpr)

This of course returns foundRows. My problem is that I need to return
the foundRows so that I can use that DataRow to display the information
that was found. I am terrible at explaining these things.

I originally looped through this and displayed the information in a
textbox. I have not been informed that I just need to return the
information and that it will be displayed outside of the function.

I am not sure how to return the foundRows information.

Any help would be appreciated.

Scott Moore
 
S

samoore33

Duh! What I am trying to say is that I need to return the foundRows as
an object.
 
P

Phill W.

samoore33 said:
I use the code below to search through a DataSet:

Dim t As DataTable
t = result.Tables("State")
Dim strExpr As String
strExpr = "id = '" & theState.ToString() & "'"
Dim foundRows() As DataRow
foundRows = t.Select(strExpr)

My problem is that I need to return the foundRows so that I can
use that DataRow to display the information that was found.

I am not sure how to return the foundRows information.

foundRows is an array of DataRows, so return that from your function, as
in:

Function XYZ(ByVal result As DataSet) As DataRow()
Dim t As DataTable
= result.Tables("State")
Dim strExpr As String _
= "id = '" & theState.ToString() & "'"
Dim foundRows() As DataRow _
= t.Select(strExpr)

Return foundRows
End Function

HTH,
Phill W.
 
M

Mike McIntyre

Here is a code snippet from MSDN. It is a function that takes an array of
DataRows and processes each row to print it. I think it will explain how
you could process your foundRows.

.....
foundRows = t.Select(strExpr)
PrintRows(foundRows)
.....

Private Sub PrintRows(ByVal rows() As DataRow)
If rows.Length <= 0 Then
Console.WriteLine("no rows found")
Exit Sub
End If

Dim row As DataRow
Dim column As DataColumn
For Each row In rows
For Each column In row.Table.Columns
Console.Write("\table {0}", row(column))
Next column
Console.WriteLine()
Next row
End Sub
 
S

samoore33

Thanks Mike, the printing out part I have. I want to pass it as an
object.

I have created a function that searches through the dataset, I want to
pass the information in the foundRows datarow as an object. I am going
to put my code in here:

Public Function SearchXML(ByVal theState As String) As DataSet

result.ReadXml("test.xml")

Dim returnTable As DataTable

Dim t As DataTable
t = result.Tables("State")
Dim strExpr As String
strExpr = "id = '" & theState.ToString() & "'"
Dim foundRows() As DataRow
foundRows = t.Select(strExpr) 'Returns items found

'Checks to make sure items were returned
If foundRows.Length = 0 Then
MessageBox.Show("That state is not in the list", "State
Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

Return result

End Function

I want the result variable to be populated with the datarow foundRows.

Scott Moore
 
M

Mike McIntyre

See two changed lines in code below (maked: --- Was ...). Now the function
returns an ojtect that is an array of DataRows.

But perhaps you want to put the returned rows into a DataTable and return
that?
Public Function SearchXML(ByVal theState As String) As DataRow() --- WAS
DataSet

result.ReadXml("test.xml")

Dim returnTable As DataTable

Dim t As DataTable
t = result.Tables("State")
Dim strExpr As String
strExpr = "id = '" & theState.ToString() & "'"
Dim foundRows() As DataRow
foundRows = t.Select(strExpr) 'Returns items found

'Checks to make sure items were returned
If foundRows.Length = 0 Then
MessageBox.Show("That state is not in the list", "State
Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

Return foundRows --- WAS result

End Function


--
Mike

Mike McIntyre [MVP]
http://www.getdotnetcode.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top