Item Not Found when Searching DataSet

MXever7

New member
Joined
Oct 25, 2005
Messages
4
I use code such as the following to search a Dataset datatable:

Code:
dim returnValue as string
dim rowsFound as datarow()

rowsFound = MyDataSet.Tables(0).Select("FirstName = " & searchTerm & "")
returnValue = rowsFound(0).item("LastName") rowsFound(0) just for simplicity, here.

return returnValue

The problem is that if the searchTerm is not in the dataset, an exception is thrown by the select method. I want the function to allow such searches and deal with them, so I quickly thought of catching errors, something like this:

Code:
dim returnValue as string
dim rowsFound as datarow()

try
rowsFound = MyDataSet.Tables(0).Select("FirstName = " & searchTerm & "")
returnValue = rowsFound(0).item("LastName")
catch ex as exception
returnValue = "none"
end try

return returnValue

Using try... catch in this way seems wrong to me. Is there a better way to search a dataset/datatable when Im not sure if the search term is in the data table?

Any help would be appreciated.
Thank you.
 
What is the exception it is raising? If you simply do not have any matching rows you should get an array for 0 elements returned.

Does this happen for all terms with no results or just for some?
 
Oops, your right.

I misread where the error was occuring. Based on your reply, I looked again and noticed that the error was not with the select, but with the handling of rowsFound. I was able to solve the problem by making sure there were rows before trying to return values from them.

It was a silly mistake on my part... but your reply really did help. Thank you.
 
Back
Top