DBNull Fehler in der Überprüfung

  • VB.NET

Es gibt 18 Antworten in diesem Thema. Der letzte Beitrag () ist von a.b_om.

    DBNull Fehler in der Überprüfung

    Hallo Community

    Ich habe eine Datenbank und baue ein Programm dafür. Ich bin gerade daran die Textboxen zu füllen. Leider gibt es NULL Werte in der Datenbank.
    Ich habe auf verschiedenste Weise abzufragen, ob einer dieser Werte NULL ist.
    Ich habe es mit IsDBNull(Feld), Feld Is DBNull, Feld Is DBNull.Value, Feld = DBNullund Feld = DBNull.Value versucht.
    Ich weiss, das es mit einem = eine Exception gibt.

    Bei den anderen, komme ich in mein TryCatch und gibt einen Exception aus:

    Quellcode

    1. Der Wert für Spalte Extension in Tabelle Contacts ist DBNull.


    Es heisst auch, dass ich DBNull nicht in ein String konvertieren kann, aber das mache ich in meiner Abfrage nicht.

    Es ist für mich total unverständlich. ?( ?(

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „a.b_om“ ()

    Ich frage DBNull bei einem typisiertem DataSet so ab:

    C#-Quellcode

    1. if(!row.IsNameNull())
    2. {
    3. this.txtName.Text = row.name;
    4. }


    is__name__Null() wird aus Deiner Column generiert, wobei "name" hier eben Dein Columnname ist. Kann also auch .isSomeNiceColumnNull() sein etc.

    Alle anderen Versuche liefen immer auf eine Exception raus... :D
    @VB1963

    Das ist mein Code von der Abfrage und meinen Versuchen

    Quellcode

    1. ​Dim dtContacts As New DCFLEXNorthwind.ContactsDataTable
    2. taContacts.SelectAllByID(dtContacts, ID)
    3. Dim r As DCFLEXNorthwind.ContactsRow = dtContacts.Rows(0)
    4. If IsNothing(r.Extension) OR r.Extension Is DBNull.Value Then
    5. txtExtension.Text = ""
    6. Else
    7. txtExtension.Text = r.Extension
    8. End If
    9. If IsNothing(r.Extension) OR r.Extension Is DBNull Then
    10. txtExtension.Text = ""
    11. Else
    12. txtExtension.Text = r.Extension
    13. End If
    14. If IsNothing(r.Extension) OR r.Extension = DBNull.Value Then
    15. txtExtension.Text = ""
    16. Else
    17. txtExtension.Text = r.Extension
    18. End If
    19. If IsNothing(r.Extension) OR r.Extension = DBNull Then
    20. txtExtension.Text = ""
    21. Else
    22. txtExtension.Text = r.Extension
    23. End If
    24. If IsNothing(r.Extension) OR IsDBNull(r.Extension) Then
    25. txtExtension.Text = ""
    26. Else
    27. txtExtension.Text = r.Extension
    28. End If
    Ich habe es ausprobiert, es passte. Mein Freund ratete mir aber, dass ich nie in Datanbanken mit Null arbeiten soll. Er erklärte mir, dass man im Dataset, wenn man auf eine Spalte klickt, dass man bei den Eigenschaften das NullValue auf (Empty) stellen kann. Ich weiss, dass ich so nur das Problem umgehe, aber für die, die Null-Problematik umgehen wollen, ist diese Lösung leichter, weil das nächste Problem dieses DBNull in die Tabelle zu speichern ist. Das funktionierte bei mir nicht. Jetzt speichere ich einfach einen leeren String in die Tabelle.
    Nein, weil ich jetzt einen leeren String in die Tabelle speichere, aber es wäre schön die Lösung des Problems zu wissen, damit ich nicht um das Problem herumbauen muss. Diese Methode mit dem leeren String, ist nur eine Übergangslösung.
    Mein Klassencode(kompletter Load und Speicher Funktion):
    Sieht nach viel aus, ist es eigentlich nicht. Sind nur viele If und Else.

    So ist es mit dem Herumbauen.

    Spoiler anzeigen

    Quellcode

    1. ​Public Class frmNew_EditContact
    2. Public Action As String
    3. Public ID As Integer
    4. Private IsSpeichernErfolgreich As Boolean = False
    5. Private Sub frmNew_EditContact_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6. Try
    7. Me.Text = Action & " Contact"
    8. laTitel.Text = Action & " Contact"
    9. cmbType.Items.Add("Customer")
    10. cmbType.Items.Add("Supplier")
    11. cmbType.Items.Add("Employee")
    12. cmbType.Items.Add("Shipper")
    13. cmbType.Items.Add("")
    14. If Action = "Edit" Then
    15. laID.Visible = True
    16. txtID.Visible = True
    17. Dim dtContacts As New DCFLEXNorthwind.ContactsDataTable
    18. taContacts.SelectAllByID(dtContacts, ID)
    19. Dim r As DCFLEXNorthwind.ContactsRow = dtContacts.Rows(0)
    20. txtID.Text = Convert.ToString(ID)
    21. If r.ContactType Is DBNull.Value Then
    22. cmbType.SelectedItem = ""
    23. Else
    24. cmbType.SelectedItem = r.ContactType
    25. End If
    26. If r.CompanyName Is DBNull.Value Then
    27. txtCompany.Text = ""
    28. Else
    29. txtCompany.Text = r.CompanyName
    30. End If
    31. If r.ContactName Is DBNull.Value Then
    32. txtName.Text = ""
    33. Else
    34. txtName.Text = r.ContactName
    35. End If
    36. If r.ContactTitle Is DBNull.Value Then
    37. txtTitle.Text = ""
    38. Else
    39. txtTitle.Text = r.ContactTitle
    40. End If
    41. If r.Address Is DBNull.Value Then
    42. txtAddress.Text = ""
    43. Else
    44. txtAddress.Text = r.Address
    45. End If
    46. If r.City Is DBNull.Value Then
    47. txtCity.Text = ""
    48. Else
    49. txtCity.Text = r.City
    50. End If
    51. If r.PostalCode Is DBNull.Value Then
    52. txtPostalCode.Text = ""
    53. Else
    54. txtPostalCode.Text = r.PostalCode
    55. End If
    56. If r.Country Is DBNull.Value Then
    57. txtCountry.Text = ""
    58. Else
    59. txtCountry.Text = r.Country
    60. End If
    61. If r.Phone Is DBNull.Value Then
    62. txtPhone.Text = ""
    63. Else
    64. txtPhone.Text = r.Phone
    65. End If
    66. If r.Extension Is DBNull.Value Then
    67. txtExtension.Text = ""
    68. Else
    69. txtExtension.Text = r.Extension
    70. End If
    71. If r.Fax Is DBNull.Value Then
    72. txtFax.Text = ""
    73. Else
    74. txtFax.Text = r.Fax
    75. End If
    76. If r.HomePage Is DBNull.Value Then
    77. txtHomepage.Text = ""
    78. Else
    79. txtHomepage.Text = r.HomePage
    80. End If
    81. Else
    82. laID.Visible = False
    83. txtID.Visible = False
    84. End If
    85. Catch ex As Exception
    86. End Try
    87. End Sub
    88. Private Sub Speichern()
    89. Try
    90. Dim companyWithoutSpace As String = txtCompany.Text.Trim
    91. If txtCompany.Text = "" Or companyWithoutSpace = "" Then
    92. If MessageBox.Show("Is the company unknown?", "Unknown company?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes Then
    93. txtCompany.Text = "Unknown"
    94. Else
    95. MessageBox.Show("Please enter a company!")
    96. IsSpeichernErfolgreich = False
    97. Exit Sub
    98. End If
    99. End If
    100. Dim taContacts As New DCFLEXNorthwindTableAdapters.ContactsTableAdapter
    101. Dim dtContacts As New DCFLEXNorthwind.ContactsDataTable
    102. If Action = "Edit" Then
    103. Dim Type As Object
    104. Dim Company As Object
    105. Dim Name As Object
    106. Dim Title As Object
    107. Dim Address As Object
    108. Dim City As Object
    109. Dim PostalCode As Object
    110. Dim Country As Object
    111. Dim Phone As Object
    112. Dim Extension As Object
    113. Dim Fax As Object
    114. Dim HomePage As Object
    115. If cmbType.Text = "" Then
    116. Type = ""
    117. Else
    118. Type = cmbType.Text
    119. End If
    120. If txtCompany.Text = "" Then
    121. Company = ""
    122. Else
    123. Company = txtCompany.Text
    124. End If
    125. If txtName.Text = "" Then
    126. Name = ""
    127. Else
    128. Name = txtName.Text
    129. End If
    130. If txtTitle.Text = "" Then
    131. Title = ""
    132. Else
    133. Title = txtTitle.Text
    134. End If
    135. If txtAddress.Text = "" Then
    136. Address = ""
    137. Else
    138. Address = txtAddress.Text
    139. End If
    140. If txtCity.Text = "" Then
    141. City = ""
    142. Else
    143. City = txtCity.Text
    144. End If
    145. If txtPostalCode.Text = "" Then
    146. PostalCode = ""
    147. Else
    148. PostalCode = txtPostalCode.Text
    149. End If
    150. If txtCountry.Text = "" Then
    151. Country = ""
    152. Else
    153. Country = txtCountry.Text
    154. End If
    155. If txtPhone.Text = "" Then
    156. Phone = ""
    157. Else
    158. Phone = txtPhone.Text
    159. End If
    160. If txtExtension.Text = "" Then
    161. Extension = ""
    162. Else
    163. Extension = txtExtension.Text
    164. End If
    165. If txtFax.Text = "" Then
    166. Fax = ""
    167. Else
    168. Fax = txtFax.Text
    169. End If
    170. If txtHomepage.Text = "" Then
    171. HomePage = ""
    172. Else
    173. HomePage = txtHomepage.Text
    174. End If
    175. taContacts.UpdateContacts(Type, Company, Name, Title, Address, City, PostalCode, Country, Phone, Extension, Fax, HomePage, ID)
    176. Else
    177. Dim dtNewRw As DCFLEXNorthwind.ContactsRow = dtContacts.NewRow
    178. With dtNewRw
    179. .ContactType = cmbType.SelectedItem
    180. .CompanyName = txtCompany.Text
    181. .ContactName = txtName.Text
    182. .ContactTitle = txtTitle.Text
    183. .Address = txtAddress.Text
    184. .City = txtCity.Text
    185. .PostalCode = txtPostalCode.Text
    186. .Country = txtCountry.Text
    187. .Phone = txtPhone.Text
    188. .Extension = txtExtension.Text
    189. .Fax = txtFax.Text
    190. .HomePage = txtHomepage.Text
    191. End With
    192. dtContacts.Rows.Add(dtNewRw)
    193. taContacts.Update(dtContacts)
    194. taContacts.FillContacts(dtContacts)
    195. bsContacts.MoveLast()
    196. End If
    197. Validate()
    198. bsContacts.EndEdit()
    199. TableAdapterManager.UpdateAll(DCFLEXNorthwind)
    200. IsSpeichernErfolgreich = True
    201. Catch ex As Exception
    202. End Try
    203. End Sub
    204. Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
    205. Try
    206. 'Speichern
    207. Catch ex As Exception
    208. End Try
    209. End Sub
    210. Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
    211. Try
    212. 'Cancel
    213. Catch ex As Exception
    214. End Try
    215. End Sub
    216. Private Sub cmdClose_Click(sender As Object, e As EventArgs) Handles cmdClose.Click
    217. Try
    218. 'Close
    219. Catch ex As Exception
    220. End Try
    221. End Sub
    222. End Class

    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „a.b_om“ ()

    a.b_om schrieb:

    So ist es mit dem Herumbauen.
    Hast du dir das Dataset schon einmal angeschaut?
    Da wird vieles in Tutorials vom @ErfinderDesRades in der Rubrik Datenbanken hergezeigt, wie es besser geht...

    Du musst auch CodeTag für VB in deinem Listing anwenden, damit dein Code besser lesbar wird!