Results 1 to 11 of 11

Thread: Cast from type DBNull to type String is not valid

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    45

    Cast from type DBNull to type String is not valid

    I have an application where I am filling text boxes with data from a table. Every now and then, not all the fields in the table are going to contain data. For instance some records will have notes and others won't. Right now if I try and do this (below) I get Cast from type DBNull to type String is not valid
    VB Code:
    1. Try
    2.                 cnn.Open()
    3.                 dr = cmd.ExecuteReader
    4.                 While dr.Read()
    5.                     tbRoNumb.Text = dr("RoNumber")
    6.                     tbLastName.Text = dr("LastName")
    7.                     tbFirstName.Text = dr("FirstName")
    8.                     tbVehicle.Text = dr("Vehicle")
    9.                     tbInsurer.Text = dr("Insurer")
    10.                     tbColorCode.Text = dr("ColorCode")
    11.                     tbAmount.Text = dr("Amount")
    12.                     tbDateEntered.Text = dr("DateEntered")
    13.                     tbDeliverDate.Text = dr("DeliveryDate")
    14.                     tbDeliverTime.Text = dr("DeliveryTime")
    15.                     tbPartsDate.Text = dr("PartsDate")
    16.                     tbF.Text = dr("Frame")
    17.                     tbM.Text = dr("Mech")
    18.                     tbA.Text = dr("Align")
    19.                     tbB.Text = dr("Body")
    20.                     tbP.Text = dr("Paint")
    21.                     tbReleaseDay.Text = dr("ReleaseDate")
    22.                     tbReleaseTime.Text = dr("ReleaseTime")
    23.                     CB1.Checked = dr("CB1")
    24.                     CB2.Checked = dr("CB2")
    25.                     CB3.Checked = dr("CB3")
    26.                     CB4.Checked = dr("CB4")
    27.                     CB5.Checked = dr("CB5")
    28.                     tbMemo.Text = dr("Notes")
    29.                 End While
    30.                 cnn.Close()
    How do I do this and still allow for nulls in one or more fields?

  2. #2
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    Try this:
    VB Code:
    1. If ISDBNull(dr("LastName")) Then
    2. tbLastName.Text = ""
    3. Else
    4. tbLastName.Text = dr("LastName")
    5. End If

    Good luck!

    Brenda

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    45
    So I will have to do an If Then for every field? Well any field that may be null. Is there any other way to accomplish this?

  4. #4
    Addicted Member
    Join Date
    Sep 2004
    Location
    Brooklyn
    Posts
    147
    So I will have to do an If Then for every field? Well any field that may be null. Is there any other way to accomplish this?
    I've run into this same thing many times. Yes, every field that could be null. No, I'm not aware of any other way to do it, but I'd love to discover one.

  5. #5
    Addicted Member corwin_ranger's Avatar
    Join Date
    Sep 2004
    Location
    CT
    Posts
    198
    Create a function that incapsulates the IF statement that returns either an empty string or the value of the field. Then just call that function from the appropriate potentially null fields.

    If this is an issue that you might have in more than one form in your project, add a module and define the function in the module making it available throughout the project.
    Last edited by corwin_ranger; Sep 9th, 2004 at 03:29 PM.

  6. #6
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    That is the only way I know of. That is what I do in my programs. Lots of copy paste I guess. Maybe someone smarter knows a better way.

    Brenda

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    45
    Ok thanks for the help.

  8. #8
    Addicted Member
    Join Date
    Oct 2000
    Location
    Québec, Canada
    Posts
    212
    You can use :

    tbLastName.Text = dr("LastName") & ""

    If you know you want a string, this way, you will end up with "" if your value in DB is null.
    Regards,

    El-Nino

  9. #9
    Addicted Member
    Join Date
    Sep 2004
    Location
    Brooklyn
    Posts
    147
    Aha! That'll save a lot of typing, thanks

  10. #10
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353
    Hey, that works great! Love it! Thanks!

    Brenda

  11. #11
    Member EricDalnas's Avatar
    Join Date
    Sep 2004
    Location
    Rhode Island
    Posts
    51
    I do it like this:
    dr.Item("fieldname").ToString

    If the field isn't string type field than I use the function in the link listed below. It is ugly at the moment but maybe I'll make it pretty one day.

    http://www.mredkj.com/vbnet/FixNull.html

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