Results 1 to 3 of 3

Thread: filling values from dataset

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    filling values from dataset

    Hi All,

    I have a need to assign values from my dataset to a variable but when i use string (which its normally text) and the item in that row of the dataset is empty it tells me that conversion from DBnull to string is not valid

    how do I get around this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: filling values from dataset

    That depends how you want to handle null values. If you want a null from the database to correspond to Nothing in VB then you will have to test for null first and only get the value if it is not null. There are various ways to do that but the DataRow class has its own method for doing so so, if you're using a DataRow, you should use that, e.g.
    Code:
    Dim name = If(myDataRow.IsNull("Name"), CStr(Nothing), CStr(myDataRow("Name")))
    If you want a null from the database to correspond to an empty string then you can simply call ToString on the field because DBNull.ToString returns an empty string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: filling values from dataset

    lovely, .tostring was what i was after.

    Thanx heaps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width