Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...