Results 1 to 8 of 8

Thread: StrongTypingException = DBNULL problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    StrongTypingException = DBNULL problem

    I have a strongly typed dataset which I assign its value to my variables. There are fields in the data which I allow null (leave blank). I keep getting a StrongTypingException because of DBNULL value. Is there away for me to code it so that to say "yes it is ok to have some value to be null?"
    I have a lot of variables which I need to set value to from the dataset so to write try and catch for each of my variables could be very messy but if there is no other way then that is what I have to do. Thank you for anyone help.

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

    Re: StrongTypingException = DBNULL problem

    Each column in a typed DataTable is a property whose type matches the data type of the column, e.g. String, Integer, etc. As such you can never get from, or assign to, one of those properties a null value because DBNull.Value can never match the property type. To work around this, database columns that allow null are mapped to properties that have two helper methods. If the property is named "Something" then you also have a SetSomethingNull method and an IsSomethingNull method, which will assign null to the corresponding field and tell you if the corresponding field is null respectively.
    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
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    Re: StrongTypingException = DBNULL problem

    Thank you for your help again. So if I have 10 variables to set value to from the dataset would I have to check that each one to see if it is not NULL as follow (Which I would have to repeat for all 10 variables)?

    If Not DataSet1.table1(BindingSource1.Position).IsNameNull Then
    x=DataSet1.table1(BindingSource1.Position).Name

    end if

    OR can I write one statement to check all 10 values for NULL something like this:

    if not isSOMETHINGNULL then

    var1=DataSet1.table1(BindingSource1.Position).Name
    var2=DataSet1.table1(BindingSource1.Position).Address
    .
    .
    .
    .
    var10=DataSet1.table1(BindingSource1.Position).City

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

    Re: StrongTypingException = DBNULL problem

    You would have to test each property that CAN be null individually. Of course, you wouldn't use DataSet1.Table1 and BindingSource1.Postition over and over. You'd get the DataRow object first and use that:
    vb.net Code:
    1. Dim row As Table1DataRow = DataSet1.Table1(BindingSource1.Position)
    2.  
    3. If Not row.IsNameNull() Then
    4.     var1 = row.Name
    5. End If
    6.  
    7. If Not row.IsAddressNull() Then
    8.     var2 = row.Address
    9. End If
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    Re: StrongTypingException = DBNULL problem

    is "table1" in "table1DataRow" the name of my table? if so it does not seem to be there.

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

    Re: StrongTypingException = DBNULL problem

    How would I know what your tables are called? It's an EXAMPLE. The whole point of typed DataSets is that all the objects are typed to YOUR data, so the names used depend on YOUR data. Use whatever names are appropriate for YOUR data.
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    Re: StrongTypingException = DBNULL problem

    I know that it is an EXAMPLE. My table name is Company (table1=Company) so what I am saying is that I don't see "CompanyDataRow".

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

    Re: StrongTypingException = DBNULL problem

    I see. What is the type of your table's Item property? That's the type of the rows.
    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

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