Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

getting row item from dataset with current row and column

TCaptainTCaptain Posts: 20Member
Hi,

I'm running into a bit of a wall here and googling isn't helping so far.

I'm trying to get a value from a dataset table using the current row and the current column.

Basically I have a loop for each row, then one for each column and at some point I want to get the value:

[code]
For each objRow as datarow in .rows
...do some stuff
For each objCol as datacolumn in .colums
...do some other stuff
strArray(intIndex) = (the value in the current column of the current row)

next
next
[/code]

I've tried using something like this:

strArray(intIndex) = CType(objRow.Item(objCol.ColumnName.ToString), String)

after dealing with many exceptions..but this isn't working. It gives me an error:

"Cast type from DBNull to type String is not valid"

The problem is that MANY of the fields in the datasets can be null.
I'm putting them into a string array so that I can add them to a different table later (after doing some operations on the array..its not a straight 1 to 1 add...basically I am concatenating data from 2 tables into one row and adding a couple of fields)

Can anyone give me a hint?

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    : Hi,
    :
    : I'm running into a bit of a wall here and googling isn't helping so far.
    :
    : I'm trying to get a value from a dataset table using the current row and the current column.
    :
    : Basically I have a loop for each row, then one for each column and at some point I want to get the value:
    :
    : [code]
    : For each objRow as datarow in .rows
    : ...do some stuff
    : For each objCol as datacolumn in .colums
    : ...do some other stuff
    : strArray(intIndex) = (the value in the current column of the current row)
    :
    : next
    : next
    : [/code]
    :
    : I've tried using something like this:
    :
    : strArray(intIndex) = CType(objRow.Item(objCol.ColumnName.ToString), String)
    :
    : after dealing with many exceptions..but this isn't working. It gives me an error:
    :
    : "Cast type from DBNull to type String is not valid"
    :
    : The problem is that MANY of the fields in the datasets can be null.
    : I'm putting them into a string array so that I can add them to a different table later (after doing some operations on the array..its not a straight 1 to 1 add...basically I am concatenating data from 2 tables into one row and adding a couple of fields)
    :
    : Can anyone give me a hint?
    :

    This will work ...

    [code]
    strArray(intIndex) = objRow(objCol).ToString
    [/code]

    This code forces the DBNull objects ToString function to be called, which returns an empty string. The CType operator tries to convert the DBNull to a string, but never tries to explicitly use the DBNull objects ToString function.
  • iwilld0itiwilld0it Posts: 1,134Member
    [b][red]This message was edited by iwilld0it at 2004-12-9 7:48:4[/red][/b][hr]
    You may also do it the longer way ...

    [code]
    If Convert.IsDBNull(objRow(objCol)) Then
    strArray(intIndex) = String.Empty
    Else
    strArray(intIndex) = objRow(objCol).ToString
    End If
    [/code]

    This form may come in handy if you need to perform certain logic.



  • TCaptainTCaptain Posts: 20Member

    :
    : This will work ...
    :
    : [code]
    : strArray(intIndex) = objRow(objCol).ToString
    : [/code]
    :
    : This code forces the DBNull objects ToString function to be called, which returns an empty string. The CType operator tries to convert the DBNull to a string, but never tries to explicitly use the DBNull objects ToString function.
    :


    Woot! Thanks a million, that does exactly what I need.

    I had originally worked around with a try/catch statement, catching the invalid cast exception and inserting an empty string myself...this is much clearer code-wise.
Sign In or Register to comment.