MXever7 Posted March 25, 2006 Posted March 25, 2006 I use code such as the following to search a Dataset datatable: 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: 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 I'm not sure if the search term is in the data table? Any help would be appreciated. Thank you. Quote
Administrators PlausiblyDamp Posted March 25, 2006 Administrators Posted March 25, 2006 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MXever7 Posted March 25, 2006 Author Posted March 25, 2006 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.