Object of type 'System.DBNull' cannot be converted to type 'System.String' from formview

I have a formview that I am using for input to my datatable.  I am using an objectdatasource that uses a BLL.  This in turn points to my XSD tableadapter.

When I test, and only fill in the required fields, I am getting the error Object of type 'System.DBNull' cannot be converted to type 'System.String'

I am not sure what field is having the issue.  In my BLL for the Add, I have checked the dates for null and placed the value of DateTime.MinValue.  For the text fields, I have place "" if they are null. 

Is this error being create prior to running the BLL add?

I am not sure where to look to correct.

 Thanks in Advance

Eddi Rae

 

 

0
EddiRae
4/29/2008 4:08:18 PM
📁 asp.net.presentation-controls
📃 72751 articles.
⭐ 3 followers.

💬 5 Replies
👁️‍🗨️ 1689 Views

Does you BLL code test or do anything with the values coming from the database?  What code it it?

If it does you can test for system.dbnull like the following.

 

If duplicateRV.Row.Item("PM_STATE") IsNot DBNull.Value Then stateTextbox.Text = duplicateRV.Row.Item("PM_STATE")

 

Past your BLL code if this doesn't solve your problem.

 

-fish


-fish

DEVELOPER MAD MAN
0
fisheerman
4/29/2008 5:27:22 PM

Where would I place the code that you gave in your example.  I was trying to do the bind or eval with a function to check the data, but If I there is a better place to do this, let me know.

-1
EddiRae
4/29/2008 5:40:40 PM

Place this code in your BLL function that is calling the data or in code behind where you are eval'ing the field that is giving you the error?

Post you BLL function that is giving you the error and I can assist

 


-fish

DEVELOPER MAD MAN
-1
fisheerman
4/29/2008 5:47:06 PM

 

 Here is the BLL function that is tied to the formview for the GET:

==================================================================================

Public Function GetMailoutsByMailoutID(ByVal MailoutID As String) Return Adapter.GetMailoutsByMailoutID(MailoutID)

End Function 

==================================================================================

 I am still getting the error, even with this.

Here is the BLL for my ADD:

==================================================================================

 Public Function AddMailout(ByVal MailoutID As String, ByVal Loc_No As Integer, _
                         ByVal LastName As String, ByVal FirstName As String, _
                         ByVal Address1 As String, ByVal Address2 As String, _
                         ByVal City As String, ByVal State As String, ByVal Zip As String, _
                         ByVal Type1 As String, ByVal Type2 As String, ByVal Type3 As String, _
                         ByVal Status As String, ByVal DeceasedName As String, _
                         ByVal DeceasedAddr1 As String, ByVal DeceasedAddr2 As String, _
                         ByVal DeceasedCity As String, ByVal DeceasedState As String, _
                         ByVal DeceasedZip As String, ByVal DateOfDeath As Date, _
                         ByVal BirthDateOfDeceased As Date, ByVal RelationToDeceased As String, _
                         ByVal Preplanned As Boolean, ByVal Letter1 As Date, ByVal Letter2 As Date, _
                         ByVal Letter3 As Date, ByVal Letter4 As Date, ByVal InputDate As Date, _
                         ByVal ModifyDate As Date) As Boolean

          Dim MailoutsAdd As New Mailouts.MailoutsDataTable
          Dim MailoutAdd As Mailouts.MailoutsRow = MailoutsAdd.NewMailoutsRow

          strMsg = Nothing

          MailoutAdd.Loc_no = HoldLoc

          If IsDBNull(FirstName) Then
               MailoutAdd.FirstName = ""
          Else
               MailoutAdd.FirstName = UCase(FirstName)
          End If

          If IsDBNull(LastName) Then
               MailoutAdd.LastName = ""
          Else
               MailoutAdd.LastName = UCase(LastName)
          End If

          If IsDBNull(Address1) Then
               MailoutAdd.Address1 = ""
          Else
               MailoutAdd.Address1 = UCase(Address1)
          End If

          If IsDBNull(Address2) Then
               MailoutAdd.Address2 = ""
          Else
               MailoutAdd.Address2 = UCase(Address2)
          End If

          If IsDBNull(City) Then
               MailoutAdd.City = ""
          Else
               MailoutAdd.City = UCase(City)
          End If

          If IsDBNull(State) Then
               MailoutAdd.State = ""
          Else
               MailoutAdd.State = UCase(State)
          End If

          If IsDBNull(Zip) Then
               MailoutAdd.Zip = ""
          Else
               MailoutAdd.Zip = UCase(Zip)
          End If

          If IsDBNull(Type1) Then
               MailoutAdd.Type1 = ""
          Else
               MailoutAdd.Type1 = UCase(Type1)
          End If

          If IsDBNull(Type2) Then
               MailoutAdd.Type2 = ""
          Else
               MailoutAdd.Type2 = UCase(Type2)
          End If

          If IsDBNull(Type3) Then
               MailoutAdd.Type3 = ""
          Else
               MailoutAdd.Type3 = UCase(Type3)
          End If

          If IsDBNull(Status) Then
               MailoutAdd.Status = "ACTIVE"
          Else
               MailoutAdd.Status = UCase(Status)
          End If

          If IsDBNull(DeceasedName) Then
               MailoutAdd.DeceasedName = ""
          Else
               MailoutAdd.DeceasedName = UCase(DeceasedName)
          End If

          If IsDBNull(DeceasedAddr1) Then
               MailoutAdd.DeceasedAddr1 = ""
          Else
               MailoutAdd.DeceasedAddr1 = UCase(DeceasedAddr1)
          End If

          If IsDBNull(DeceasedAddr2) Then
               MailoutAdd.DeceasedAddr2 = ""
          Else
               MailoutAdd.DeceasedAddr2 = UCase(DeceasedAddr2)
          End If

          If IsDBNull(DeceasedCity) Then
               MailoutAdd.DeceasedCity = ""
          Else
               MailoutAdd.DeceasedCity = UCase(DeceasedCity)
          End If

          If IsDBNull(DeceasedState) Then
               MailoutAdd.DeceasedState = ""
          Else
               MailoutAdd.DeceasedState = UCase(DeceasedState)
          End If

          If IsDBNull(DeceasedZip) Then
               MailoutAdd.DeceasedZip = ""
          Else
               MailoutAdd.DeceasedZip = UCase(DeceasedZip)
          End If

          If Not IsDate(DateOfDeath) Then
               MailoutAdd.DateOfDeath = DateTime.MinValue
          Else
               MailoutAdd.DateOfDeath = DateOfDeath
          End If

          If Not IsDate(BirthDateOfDeceased) Then
               MailoutAdd.BirthDateOfDeceased = DateTime.MinValue
          Else
               MailoutAdd.BirthDateOfDeceased = BirthDateOfDeceased
          End If

          If IsDBNull(RelationToDeceased) Then
               MailoutAdd.RelationToDeceased = ""
          Else
               MailoutAdd.RelationToDeceased = UCase(RelationToDeceased)
          End If

          If IsDBNull(Preplanned) Then
               MailoutAdd.Preplanned = False
          Else
               MailoutAdd.Preplanned = Preplanned
          End If

          If Not IsDate(Letter1) Then
               MailoutAdd.Letter1 = DateTime.MinValue
          Else
               MailoutAdd.Letter1 = Letter1
          End If

          If Not IsDate(Letter2) Then
               MailoutAdd.Letter2 = DateTime.MinValue
          Else
               MailoutAdd.Letter2 = Letter2
          End If

          If Not IsDate(Letter3) Then
               MailoutAdd.Letter3 = DateTime.MinValue
          Else
               MailoutAdd.Letter3 = Letter3
          End If

          If Not IsDate(Letter4) Then
               MailoutAdd.Letter4 = DateTime.MinValue
          Else
               MailoutAdd.Letter4 = Letter4
          End If

          If (MailoutAdd.Status = "DECEASED") Or (MailoutAdd.Status = "INACTIVE") Then
               MailoutAdd.Type1 = ""
               MailoutAdd.Type2 = ""
               MailoutAdd.Type3 = ""
          End If

          If MailoutAdd.Status = "INACTIVE" Then
               If IsDate(MailoutAdd.Letter1) Then
                    If MailoutAdd.Letter1 > Now Then
                         MailoutAdd.Letter1 = DateTime.MinValue
                    End If
               End If
               If IsDate(MailoutAdd.Letter2) Then
                    If MailoutAdd.Letter2 > Now Then
                         MailoutAdd.Letter2 = DateTime.MinValue
                    End If
               End If
               If IsDate(MailoutAdd.Letter3) Then
                    If MailoutAdd.Letter3 > Now Then
                         MailoutAdd.Letter3 = DateTime.MinValue
                    End If
               End If
               If IsDate(MailoutAdd.Letter4) Then
                    If MailoutAdd.Letter4 > Now Then
                         MailoutAdd.Letter4 = DateTime.MinValue
                    End If
               End If
          End If

          If (MailoutAdd.Type2 = MailoutAdd.Type1) Or (MailoutAdd.Type1 = "") Then
               MailoutAdd.Type1 = MailoutAdd.Type2
               MailoutAdd.Type2 = MailoutAdd.Type3
               MailoutAdd.Type3 = ""
          End If

          If (MailoutAdd.Type3 = MailoutAdd.Type2) Or (MailoutAdd.Type2 = "") Then
               MailoutAdd.Type2 = MailoutAdd.Type3
               MailoutAdd.Type3 = ""
          End If

          If (MailoutAdd.Type3 = MailoutAdd.Type1) Then
               MailoutAdd.Type3 = ""
          End If

          Dim AddrZip As Mailouts.MailoutsDataTable = Adapter.GetMailoutsByLocNameAddr(MailoutAdd.Loc_no, MailoutAdd.LastName, MailoutAdd.FirstName, MailoutAdd.Address1, MailoutAdd.Zip)
          Dim x As Integer = 1
          Dim y As Integer = 0

          If AddrZip.Count > 1 Then
               Try
                    x /= y
               Catch ex As Exception
                    strMsg = "This Location, Name, Address and Zip already exist"
                    Throw New ApplicationException("This Location, Name, Address and Zip already exist", ex)
                    Return False
               End Try
          End If
          MailoutAdd.ModifyDate = Now
          MailoutAdd.InputDate = Now

          Dim MailoutInfoTbl As Mailouts.MailoutInfoDataTable = Adapter1.GetMailoutInfo
          If MailoutInfoTbl.Count <> 1 Then
               Try
                    x /= y
               Catch ex As Exception
                    strMsg = "Error in getting MailoutInfo, Contact Website Administrator"
                    Throw New ApplicationException("Error in getting MailoutInfo, Contact Website Administrator", ex)
                    Return False
               End Try
          End If

          Dim MailoutInfoRec As Mailouts.MailoutInfoRow = MailoutInfoTbl(0)
          Dim strMailoutID As String
          Dim intMailoutID As Integer

          intMailoutID = MailoutInfoRec.LastID + 1
          strMailoutID = "O" & Format(intMailoutID, "000000")
          MailoutAdd.MailoutID = strMailoutID
          MailoutInfoRec.LastID = intMailoutID

          'update MailoutInfo record
          Adapter1.Update(MailoutInfoTbl)

          'Add new mailout record
          MailoutsAdd.AddMailoutsRow(MailoutAdd)

          Dim rowsAffected As Integer = Adapter.Update(MailoutsAdd)
          Return rowsAffected = 1
     End Function
0
EddiRae
4/29/2008 7:39:21 PM

I have it working ... I am not sure what I did to correct.  The only thing I changed was to have a field dtDefault as date to be #1/1/1900#.  I then checked to see if the cell was a date, if not, i placed this into it.

0
EddiRae
5/1/2008 6:14:09 PM