David Svedarsky
asked on
Cast from type 'DBNull' to type 'String' is not valid
How to modify code to remove error: Cast from type 'DBNull' to type 'String' is not valid valid when cell is empty?
Thanks!
Private Sub TblWorkorderDataGridView_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblWorkorderDataGridView.CellEndEdit
Dim strUpperValue As String
'Apply to certain columns
Select Case e.ColumnIndex
Case 10
strUpperValue = TblWorkorderDataGridView.CurrentCell.Value
TblWorkorderDataGridView.CurrentCell.Value = strUpperValue.ToUpper
End Select
Thanks!
Here's another way to code it ...
strUpperValue = ""
If TblWorkorderDataGridView.CurrentCell.Value IsNot Nothing AndAlso TblWorkorderDataGridView.CurrentCell.Value.Length > 0 Then
strUpperValue = TblWorkorderDataGridView.CurrentCell.Value
End If
Hi
I'm using following function:
Now you can use from anywhere:
MsgBox(GetDBString(TblWork orderDataG ridView.Cu rrentCell. Value,"Mis sing"))
'or
Dim x as String=GetDBString(TblWork orderDataG ridView.Cu rrentCell. Value)
If x="" Then
MsgBox("Null value!")
End If
I'm using following function:
Public Shared Function GetDBString(ByVal o As Object, Optional ByVal strDBNull As String = "") As String
If o Is Nothing Then Return strDBNull
If IsDBNull(o) Then Return strDBNull Else Return o.ToString
End Function
Now you can use from anywhere:
MsgBox(GetDBString(TblWork
'or
Dim x as String=GetDBString(TblWork
If x="" Then
MsgBox("Null value!")
End If
ASKER
I tried all of the code samples above and none worked.
I get the error on this line when deleting text from a databridview cell (datatype nvarchar (3):
strUpperValue = TblBasicPlumbingWorkorderD ataGridVie w.CurrentC ell.Value
Here are the error details:
System.InvalidCastExceptio n was unhandled
Message="Conversion from type 'DBNull' to type 'String' is not valid."
Source="Microsoft.VisualBa sic"
StackTrace:
at Microsoft.VisualBasic.Comp ilerServic es.Convers ions.ToStr ing(Object Value)
at Plumbing.frmTrackingUPBJob s.TblWorko rderDataGr idView_Cel lEndEdit(O bject sender, DataGridViewCellEventArgs e) in C:\Documents and Settings\Dave\My Documents\Visual Studio 2005\Projects\Plumbing\Plu mbing\frmT rackingUPB Jobs.vb:li ne 68
at System.Windows.Forms.DataG ridView.On CellEndEdi t(DataGrid ViewCellEv entArgs e)
at System.Windows.Forms.DataG ridView.En dEdit(Data GridViewDa taErrorCon texts context, DataGridViewValidateCellIn ternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean resetCurrentCell, Boolean resetAnchorCell)
at System.Windows.Forms.DataG ridView.Se tCurrentCe llAddressC ore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataG ridView.On CellMouseD own(HitTes tInfo hti, Boolean isShiftDown, Boolean isControlDown)
at System.Windows.Forms.DataG ridView.On CellMouseD own(DataGr idViewCell MouseEvent Args e)
at System.Windows.Forms.DataG ridView.On MouseDown( MouseEvent Args e)
at System.Windows.Forms.Contr ol.WmMouse Down(Messa ge& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.DataG ridView.Wn dProc(Mess age& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Applicati onContext context)
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.OnRun( )
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.DoAppl icationMod el()
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.Run(St ring[] commandLine)
at Plumbing.My.MyApplication. Main(Strin g[] Args) in 17d14f5c-a337-4978-8281-53 493378c107 1.vb:line 81
at System.AppDomain._nExecute Assembly(A ssembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
Can someone help on this?
Thanks!
I get the error on this line when deleting text from a databridview cell (datatype nvarchar (3):
strUpperValue = TblBasicPlumbingWorkorderD
Here are the error details:
System.InvalidCastExceptio
Message="Conversion from type 'DBNull' to type 'String' is not valid."
Source="Microsoft.VisualBa
StackTrace:
at Microsoft.VisualBasic.Comp
at Plumbing.frmTrackingUPBJob
at System.Windows.Forms.DataG
at System.Windows.Forms.DataG
at System.Windows.Forms.DataG
at System.Windows.Forms.DataG
at System.Windows.Forms.DataG
at System.Windows.Forms.DataG
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.DataG
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Plumbing.My.MyApplication.
at System.AppDomain._nExecute
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
Can someone help on this?
Thanks!
ASKER
All I want to do is convert the cell value to upper case and have nothing when deleted.
If I add a binding textbox to the form - the datagridview cell formatting works fine and deletes...
If I add a binding textbox to the form - the datagridview cell formatting works fine and deletes...
Did you try
strUpperValue = GetDBString(TblBasicPlumbi ngWorkorde rDataGridV iew.Curren tCell.Valu e)
strUpperValue = GetDBString(TblBasicPlumbi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You just want make textbox uppercase?
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is TextBox Then
If CType(sender, DataGridView).CurrentCell.ColumnIndex = 10 Then
CType(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
End If
End If
End Sub
ASKER
Not textbox, but datagridview column to uppercase...
the code I have is working fine...
Thanks!
the code I have is working fine...
Thanks!
ASKER
I accepted my comment as a solution because no other comments worked.
Open in new window