Eddy Wu

Eddy Wu

  • 2k
  • 115
  • 17k

How to handling "Input String was not in a correct format" FormatException Throwned

Mar 9 2012 12:05 PM
please help to examining the followong code snippet,

-----------------
----------------
----------------
Private Sub BtnRekam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRekam.Click
Dim tmstr As String = String.Format("{0}", txtReceival.Text.Trim)
REM The program ran to broke at this line "FormatException" Unhandled
Dim tmint As Long = Convert.ToUInt32(tmstr) 
Dim custhst As New Hashtable
Dim mycustomer As New CustomerMaintenance

custhst.Add("IDPlg", txtCustID.Text.Trim) '0
custhst.Add("PerusahaanPlg", txtCompany.Text.Trim) '1
custhst.Add("AlamatPlg1", txtAddress1.Text.Trim) '2
custhst.Add("AlamatPlg2", txtAddress2.Text.Trim) '3
custhst.Add("TelpPlg1", txtPhone1.Text.Trim) '4
custhst.Add("TelpPlg2", txtPhone2.Text.Trim) '5
custhst.Add("FaxPlg", txtFax.Text.Trim) '6
custhst.Add("CPPlg", txtCPName.Text.Trim) '7
custhst.Add("EmailPlg", txtEmail.Text.Trim) '8
custhst.Add("SitusPlg", txtWebSite.Text.Trim) '9
custhst.Add("PiutangPlg", IIf((txtReceival.Text = ""), 0, tmint)) '10
custhst.Add("KtrPlg", txtDescription.Text.Trim) '11

mycustomer.Append(custhst)
mycustomer.SaveCustomerData()
End Sub
----------------------------------
---------------------------------
---------------------------------

Public Class CustomerMaintenance
Private hsl As Hashtable = Nothing
Private fld As String() = {"IDPlg", "PerusahaanPlg", "AlamatPlg1", "AlamatPlg2", "TelpPlg1", "TelpPlg2", "FaxPlg", "CPPlg", _
"EmailPlg", "SitusPlg", "PiutangPlg", "KtrPlg"}

'Default Constructor
Public Sub New()
hsl = New Hashtable
End Sub

'Overloaded Constructor with Parameter
Public Sub New(ByVal CustomerTable As Hashtable)
If (hsl Is Nothing) Then hsl = New Hashtable
hsl = CustomerTable.Clone
End Sub

Public Function Append(ByVal TableCustomer As Hashtable) As Integer
hsl = TableCustomer.Clone
Return hsl.Count
End Function

Public Function SaveCustomerData() As Boolean
Dim myconn As MySqlConnection = Nothing
Dim mycomm As MySqlCommand = Nothing
Dim ssql As String = Nothing
Dim bSaved As Boolean = False

myconn = New MySqlConnection(ConnectionText)
myconn.Open()

For n As Integer = 0 To (hsl.Count / hsl.Count) - 1
ssql = "insert into tpelanggan(IDPlg,PerusahaanPlg,AlamatPlg1,AlamatPlg2,TelpPlg1,TelpPlg2,FaxPlg,CPPlg,EmailPlg" + _
",SitusPlg,PiutangPlg,KtrPlg) values(@p0,@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11)"

mycomm = New MySqlCommand(ssql, myconn)

mycomm.Parameters.AddWithValue("@p0", hsl.Item(fld(n)))
mycomm.Parameters.AddWithValue("@p1", hsl.Item(fld(n + 1)))
mycomm.Parameters.AddWithValue("@p2", hsl.Item(fld(n + 2)))
mycomm.Parameters.AddWithValue("@p3", hsl.Item(fld(n + 3)))
mycomm.Parameters.AddWithValue("@p4", hsl.Item(fld(n + 4)))
mycomm.Parameters.AddWithValue("@p5", hsl.Item(fld(n + 5)))
mycomm.Parameters.AddWithValue("@p6", hsl.Item(fld(n + 6)))
mycomm.Parameters.AddWithValue("@p7", hsl.Item(fld(n + 7)))
mycomm.Parameters.AddWithValue("@p8", hsl.Item(fld(n + 8)))
mycomm.Parameters.AddWithValue("@p9", hsl.Item(fld(n + 9)))
mycomm.Parameters.AddWithValue("@p10", hsl.Item(fld(n + 10)))
mycomm.Parameters.AddWithValue("p11", hsl.Item(fld(n + 11)))

If (mycomm.ExecuteNonQuery() > 0) Then
bSaved = True
Else
MessageBox.Show("Gagal Update SystemData .. !!", "System Data Error..!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Next
myconn.Close()
Return bSaved
End Function


End Class


I used VS2008
How better suggestion to solve that problem ?



Answers (2)