Help with DataTable.Rows Reference

G

Guest

Hi. I have a Public Function which requires 2 parameters as follows...
Public Function Test(ByVal 1 as String, ByVal 2 as String)
Prior to calling this function, I fill a dataset(ds) with an undetermined
amount of records, and then use a datatable(dt) to reference the rows of the
datatset.

Dim ds as New DataSet
'Fill DataSet
dt = ds.Tables("Table")

Test(dt.Rows(i)(0), dt.Rows(i + 1)(0))

As you can see the first and second rows from the dataset are passed to the
function. I am having a problem whenever only 1 record exists in the
dataset. When only 1 record exists I get the error message "There is no row
at position 2". Is it possible to send an empty string("") to the function in
order for the function to still run? Or, is there a better way to prevent
this error? Thanks.
 
G

Guest

Hi,
If your "Test" function would still work with an empty string then you can
just test if there is another row and if so use that value, otherwise use the
empty string. Something like:

Dim value1 As String = ""
Dim value2 As String = ""
If dt.Rows.Count > i Then
value1 = dt.Rows(i) (0)
If dt.Rows.Count > i + 1 Then
value2 = dt.Rows(i + 1) (0)
Test (value1, value2)

This way you won't run into an out-of-bounds type of error by trying to
access a row which doesn't exist.

Cheers,
Steve
 

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