Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid.

 

problem is ado.net is processing the update not sure what exactly is the cause.

 

Heres the code first I have a handler in the form load:

 

AddHandler DsSurveyPricesEntered1.Tables("SurvPric").ColumnChanging, AddressOf SaleCode_ColumnChanging

 

The sub that handles it:

 

Private Sub SaleCode_ColumnChanging(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs)

' Only check for errors in the Sale Code column

If (e.Column.ColumnName.Equals("SaleCode")) Then

' Do not allow certain codes

Dim CodeValue As Object = e.ProposedValue

If e.Row.HasErrors Then

e.Row.ClearErrors()

 

End If

Select Case CType(e.ProposedValue, String)

Case "1"

e.ProposedValue = "b"

Case "2"

e.ProposedValue = "pl"

Case "3"

e.ProposedValue = "/"

Case Else

e.ProposedValue = Nothing

End Select

End If

End Sub

 

works fine until i go to save using ado.net here:

 

Private Sub SavePrices()

'***** this is where it blows up *******

Me.OleDbDataAdapter3.Update(DsSurveyPricesEntered1)

'***** end where it blows up ********

'update the sale codes

Me.UpdateCodes()

Me.OleDbDataAdapter3.Fill(DsSurveyPricesEntered1)

 

oSess = Nothing

End Sub

 

*** If I comment out the handler the table updates fine no problems but I need it. or something similar. Any ideas would certainly be appreciated. Thanks Ed

Posted

I have received this same error before. However, it was generated while attempting to read a string value from a field within a record that didn't have anything in it. As long as each record contained some string value in it there was no problem.

 

Not sure why would get the same error when trying to update a record. Is it caused by trying to update a field with a blank text box or something?

Posted
don't know i use .fill to load the table it has null values everywhere at first until its edited. There are four fields, product and editable fields regprice, salecode (the one i'm trying to handle), and sale price. everything works fine until i add the handler which i assume only meddles with the salecode column. yet the error is envoked by edits to the other fields. hmmm
Posted
What does your table structure look do you allow nulls into thiese fields.If not it won't allow the updates.
Programmers are trying to create bigger and beter idiot proof programs.The universe is trying to create bigger and beter idiots and so far the universe is winning.
Posted
It is loaded with null values to begin with. I assume, since i use the .fill method of the adaper and the fields are null initially in the sql server table.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...