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. Follow

5 Replies
2895 Views

Similar Articles

[PageSpeed] 31
Get it on Google Play
Get it on Apple App Store

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
Reply:

Similar Artilces:

System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'
Hello , I am a data table with null able Columns.I am not able to catch the value for DBNull. I did set a value to be returned as empty in dataset. Here is the code where i am trying to catch.1 public List GetStores() 2 { 3 List tmpStore = new List(); 4 StoresService.shopDBDataSet.StoresDataTable stores = 5 storesService.GetStores(); 6 for (int i = 0; i < stores.Count ; i++) 7 { 8 Store store = new Store(); 9 store.StoreID = stores[i].StoreID...

Object of type 'System.DBNull' cannot be converted to type 'System.DateTime'.
Hi, How can I pass null values to a system.datetime? Thanks in advance for the help J   Below is my code:   Public Class Courses     Public Sub Update_Class(ByVal ClassId As Integer, _    ByVal StatusId As Integer, _    ByVal ClassNumber As Integer, _    ByVal TeacherId As Integer, _    ByVal AmountofParticipants As Integer, _    ByVal CourseDate1 As DateTime, _    ByVal CourseDate2 As DateTime, _    ByVal CourseDate3 As DateTime)     Dim myConnection As New SqlConnection(Conn...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi i get the following error, Unable to cast object of type 'System.DBNull' to type 'System.String'.  A generic image is meant to appear when no image is found in the database for a particular product, this is how the code is laid out;protected string FormatImageUrl(string url) {if (url != null && url.Length > 0) { //if (File.Exists(url))return (url); } else {return "~/Images/CompanyProducts/No_image.jpg"; } }   And the html is like this; <asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Ev...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi All, I am using Sql server 2005 and VS 2005 C#. Can anybody explain howz solve this prob? Unable to cast object of type 'System.DBNull' to type 'System.String'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.Source Error: Line 3496: get { Line 3497: try { Line 3498: ...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi i have the following which is meant to check if an image is available and if it isnt it should show a default image however, it gives me this error whenever the image is null in the database. This is the function which returns the error;protected string FormatImageUrl(object url) {if ((url != DBNull.Value) && (url != null) && (url.ToString().Trim().Length > 0)) { //if (File.Exists(url))return (url.ToString().Trim()); } else {return "~/images/default.gif"; } } Hey,Doesn't seem to pass the dbnull check.  Try to do:  if ((!DBNull.Valu...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi there, If I add a new event and leave the description field empty the StarterKit seems to throw a big wobbly over the null values in my DB (SQL2000). Have I made a mistake in the installation or something? The problem lies in this line: SharedRoutines.truncate((string)Eval("description")) because description is Null. I want to sort this in this line and I don't want to reply on validators? This is a known issue reported in the Identified Bugs and Fixes for the ClubSite Starter Kit post. I don't think there is one definitive answer. Search on your error message and you'll fin...

Unable to cast object of type 'System.DBNull' to type 'System.String'
I have received the following error when I am logged in as administrator.  I wanted to test the load event feature.  Allows me to load and save new event.  I can view event on location view page but when I try to link to "list or calender" view I receive this error "Unable to cast object of type 'System.DBNull' to type 'System.String'.  Debug highlights the <asp:HyperlinkID as errored line. Any assistance would be greatly appreciated Brian   <DayEventTemplate> <div style="padding: 3px;"> <asp:Hyper...

Eventcalender: Unable to cast object of type 'System.DBNull' to type 'System.String'.
 I added an event in the eventcalender. When I try to view the events_calender.aspx I get this error message, what can be wrong??   Server Error in '/bfc_csk' Application. Unable to cast object of type 'System.DBNull' to type 'System.String'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.Source Err...

Unable to cast object of type 'System.DBNull' to type 'System.String'. #2
 Hi:I am totally new to asp.net and downloaded the club website project. I have not done anything to the events page yet but as soon as i tried to add an event I got the following error. "Unable to cast object of type 'System.DBNull' to type 'System.String'." Can someone help me out here?ThanksBrian  This bug has been covered in the Identified Bugs and fixes section.  http://forums.asp.net/t/970081.aspx There are a few solutions and discussions on fixes, you will need to read through it to find the fix. Support@aspsksolutions.com Without kno...

Unable to cast object of type 'System.DBNull' to type 'System.String'. #2
I am getting this error when I try to run the code below. The error is happening on the last for each loop || foreach (string s in dr.ItemArray) || Please see below:        private void button1_Click(object sender, EventArgs e)        {            string myOutput = "";            DataTable dt = new DataTable();            using (TextReader tr = File.O...

Convert 'System.DBNull' to type 'System.String' ?
I have a textbox on my site that I want to populate with some data, however when the value is null, which is quite often.The problem is that when I call a static method which returns a string; when that string is DBNull, I get the following error which is absolutely correct.Unable to cast object of type 'System.DBNull' to type 'System.String'.So, what should I do to be able to ignore that the string is null and just display a "" string in the texbox?The code is as follows:    private void GetCartSmall(string sSessionID)    {      &n...

System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'.
Hi All, I am trying to retrieve a Picture from an SQL 2005 database. Here is my code:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ListingID As String = (Request.QueryString("GUID")) 'Connect to the database and bring back the image contents & MIME type for the specified pictureUsing myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionStringListings").ConnectionString) Const SQL As String = "SELECT [Pic1Ext], [Picture1] FROM [Listings] WHERE [ListingID] = @ListingID"C...

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

Object of type 'System.Boolean' cannot be converted to type 'System.Int16'.
I have a Checkbox on a detailsview bound to a small int field in my database table.It binds fine when showing the item template on the detailsview, but when I try do an insert/update it blows up with that error.Surely I can bind a checkbox to an integer field, so that unchecked translates to 0, and checked translates to 1?The reason I use a smallint, is its future-proof.  If we decided to switch to a status instead off/on in the future, there are less changes required.  As long as 0 is False and any other value is considered True you should be able to bind the value like this: C...

Web resources about - Object of type 'System.DBNull' cannot be converted to type 'System.String' from formview - asp.net.presentation-controls

Resources last updated: 12/16/2015 12:02:49 AM