Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have coded a windows forms app for a friend which has been running great for ages. All of a sudden when they started the app the following appeared and I'm not experienced enough to understand where to look, any pointers greatly appreciated.

************** Exception Text **************
System.InvalidCastException: Argument 'Number' cannot be converted to a numeric value.
   at Microsoft.VisualBasic.Conversion.Str(Object Number)
   at GtracsX4.gtzModSub.CalculateRegNo(String& r1)
   at GtracsX4.gtzLogin.btn0_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.PerformClick()
   at GtracsX4.gtzLogin.rb_CheckedChanged(Object sender, EventArgs e)
   at System.Windows.Forms.RadioButton.OnCheckedChanged(EventArgs e)
   at System.Windows.Forms.RadioButton.set_Checked(Boolean value)
   at System.Windows.Forms.RadioButton.OnClick(EventArgs e)
   at System.Windows.Forms.RadioButton.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18444 built by: FX451RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
GtracsX4
    Assembly Version: 1.0.1.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Program%20Files%20(x86)/GtracsX4/GtracsX4.exe
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34238 built by: FX452RTMGDR
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34251 built by: FX452RTMGDR
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 11.0.50938.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Data
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Data.DataSetExtensions
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.DataSetExtensions/v4.0_4.0.0.0__b77a5c561934e089/System.Data.DataSetExtensions.dll
----------------------------------------
System.Numerics
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------
System.Transactions
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
Accessibility
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
    CodeBase: file:///C:/windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
Posted

Look at your code, and find the relevant line: it's in the CalculateRegNo method, probably when you convert the parameter value to a number. The string it's trying to convert is not a valid number. It expects "123", or "-456.78", and is getting "hello" or similar.

So use the debugger: put a breakpoint on the first line of the method, and look at what the data is. Then use the debugger stack trace to go back through your code to find out why it is whatever it is!

We can't do that for you: we don't have your code, and we don;t have your data... :laugh:
 
Share this answer
 
Just read..

Somewhere in GtracsX4.gtzModSub.CalculateRegNo it calls VisualBasic.Conversion.Str
witch throws the Exception, because --> it can't convert the given argument 'Number' Type object to a Numeric Type..

For example Conversion.Str can't work with a String like "12a4" because it's not a Number!
Conversion.Str is designed to return a String representing a numeric value given to it. (int, short, long, single, double, etc.. even a string containing a valid numeric representation)


but.. why not just use
VB
value.ToString
?
 
Share this answer
 
Comments
Member 11890082 6-Aug-15 6:18am    
Thanks I'll look there. The other question is of course. Why has this just started happening? It was all working OK until just recently & it works perfectly well on my pc, I can't get it to go wrong.
F. Xaver 6-Aug-15 6:54am    
Depends on the values used in CalculateRegNo Method, maybe a HardwareID's witch are HEX values (like "123" on your system and "3B5" on clients) or some Input-fields not correctly validated before use
Member 11890082 6-Aug-15 7:46am    
Here's the code I'm using - can't see where it could go wrong, but thanks for your help.
Dim myDict As New Dictionary(Of Integer, Integer) From {{2013, 8888}, {2014, 9231}, {2015, 3715}, {2016, 1650}, {2017, 1923}, _
{2018, 3591}, {2019, 7835}, {2020, 9083}, {2021, 1234}, {2022, 9681}, {2023, 5619}, {2024, 7304}, {2025, 3693}, _
{2026, 3892}, {2027, 8547}, {2028, 2641}, {2029, 5807}, {2030, 7312}, {2031, 1671}, {2032, 5398}, {2033, 2761}, _
{2034, 3945}, {2035, 1793}, {2036, 9882}, {2037, 4963}, {2038, 9671}, {2039, 2001}, {2040, 4489}, {2099, 7714}}
Dim checkno1 As String, noblanks As String, r5 As String, r3 As String, r2 As String, r4 As String, r6 As String, z As Integer, checkno As String
r1 = Trim(r1)
r6 = ""
noblanks = ""
For z = 1 To Len(r1)
r3 = Mid(r1, z, 1)
If r3 <> " " Then
noblanks = noblanks + r3
End If
Next z
Dim yy As Integer = 0
For Each entry In myDict
If mRegNo1 = entry.Value Then
yy = entry.Key
End If
Next
If yy <> 0 Then
If yy > Year(Now) Then
r6 = myDict(yy)
' ElseIf yy <= Year(Now) And Month(Now) <= 6 Then
ElseIf yy = Year(Now) And Month(Now) <= 6 Then
r6 = myDict(yy)
Else
r6 = 9999
End If
Else
r6 = myDict(2099)
End If

checkno1 = Trim(Str(r6))
If Len(noblanks) < 15 Then
noblanks = noblanks + "AEHNPRUYZCDJKQS"
End If
r1 = AscW(Left(noblanks, 1))
r2 = AscW(Mid(noblanks, 3, 1))
r3 = AscW(Mid(noblanks, 6, 1))
r4 = AscW(Mid(noblanks, 10, 1))
r5 = AscW(Mid(noblanks, 15, 1))
r1 = Int((Val(r1) * 21) / 18)
r2 = Int((Val(r2) * 21) / 18)
r3 = Int((Val(r3) * 21) / 18)
r4 = Int((Val(r4) * 21) / 18)
r5 = Int((Val(r5) * 21) / 18)
checkno = Right(Trim(Str(r1)), 2) & Right(Trim(Str(r2)), 2) & Right(Trim(Str(r3)), 2) & Right(Trim(Str(r4)), 2) & Right(Trim(Str(r5)), 2)
If mRegNo = checkno And mRegNo1 = checkno1 Then
Return True
Else
Return False
End If
F. Xaver 6-Aug-15 9:06am    
puh, just some ideas and suggestions from me

why declaring all the rx variables as String?
you do a lot of math like actions on it, using VB6 compatibility commands (val/str/mid/len/right/left/ascw) -> get rid of them, better use the new VB.Net functions

and give that vars better Names. why 'yy' call it something like '_Target_Year'
Using something like
Dim _target_year As Integer = mydict.Where(Function(o) o.Value = mRegNo1).SingleOrDefault.Key
instead of
Dim yy As Integer = 0
For Each entry In myDict
If mRegNo1 = entry.Value Then
yy = entry.Key
End If
Next

makes debugging way easier


or
checkno1 = Trim(Str(r6))
r6 is already a String.
just use
checkno1 = r6.Trim()


Right(Trim(Str(r1)), 2)
r1.Trim.Substring(r1.Length - 2)

are all that Trims really necessary?

when you would use a integer var for all you calculations instead of a string
CStr(r1 Mod 100)
would also to the job


hm, use
Option Strict On
Option Explicit On
Option Infer Off

and get rid of the Microsoft.VisualBasic Namespace and its functions ;)
Sergey Alexandrovich Kryukov 6-Aug-15 9:14am    
Right. This is a very bad and "popular" these days abuse: trying to use strings representing data instead of data itself. (5 for the answer.)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900