-
Feb 27th, 2014, 09:39 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Format Exception Occurred
When I run the program, I get this error
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
The code that shows the above error is this
Code:
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Payroll_BackEnd.accdb"
Please help!!
-
Feb 27th, 2014, 10:56 AM
#2
Thread Starter
Addicted Member
Re: Format Exception Occurred
the whole coding for the form
Code:
Imports System.Data.OleDb
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class Staff_Editor
Dim inc As Integer
Dim MaxRows As Integer
Dim conn As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim dj As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String = "SELECT * FROM Department"
Dim rdr As OleDbDataReader = Nothing
Dim dtable As New DataTable
Dim con As OleDbConnection = Nothing
Dim adp As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand = Nothing
Dim dt As New DataTable
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Payroll_BackEnd.accdb"
Private Sub Staff_Editor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source=|DataDirectory|\Payroll_BackEnd.accdb"
conn.ConnectionString = dbProvider & dbSource
conn.Open()
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(dj, "Payroll_BackEnd")
conn.Close()
MaxRows = dj.Tables("Payroll_BackEnd").Rows.Count
inc = -1
End Sub
Private Sub SaveButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton1.Click
Dim promptQuestion As DialogResult = MessageBox.Show("Are you sure you want to save this record?", "Save Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If promptQuestion = Windows.Forms.DialogResult.No Then
Exit Sub
End If
con = New OleDbConnection(cs)
con.Open()
Dim cb As String = "Insert INTO Staff_Details(DepartmentCode,EmployeeCode,EmployeeName,LastName,Gender,BirthDate,ContactNumber,Address,Email,Designation,DateEmployed,BasicSalary,Status,ProfilePicture) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14)"
Dim MemStream As New MemoryStream
Dim DataPic_Update As Byte()
Me.PictureBox1.Image.Save(MemStream, Imaging.ImageFormat.Png)
DataPic_Update = MemStream.GetBuffer()
MemStream.Read(DataPic_Update, 0, MemStream.Length)
cmd = New OleDbCommand(cb)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@d1", OleDbType.VarChar, 4, "DepartmentCode"))
cmd.Parameters.Add(New OleDbParameter("@d2", OleDbType.VarChar, 5, "EmployeeCode"))
cmd.Parameters.Add(New OleDbParameter("@d3", OleDbType.VarChar, 20, "EmployeeName"))
cmd.Parameters.Add(New OleDbParameter("@d4", OleDbType.VarChar, 20, "LastName"))
cmd.Parameters.Add(New OleDbParameter("@d5", OleDbType.VarChar, 6, "Gender"))
cmd.Parameters.Add(New OleDbParameter("@d6", OleDbType.Date, 30, "BirthDate"))
cmd.Parameters.Add(New OleDbParameter("@d7", OleDbType.VarChar, 11, "ContactNumber"))
cmd.Parameters.Add(New OleDbParameter("@d8", OleDbType.VarChar, 100, "Address"))
cmd.Parameters.Add(New OleDbParameter("@d9", OleDbType.VarChar, 50, "Email"))
cmd.Parameters.Add(New OleDbParameter("@d10", OleDbType.VarChar, 15, "Designation"))
cmd.Parameters.Add(New OleDbParameter("@d11", OleDbType.Date, 30, "DateEmployed"))
cmd.Parameters.Add(New OleDbParameter("@d12", OleDbType.VarChar, 10, "BasicSalary"))
cmd.Parameters.Add(New OleDbParameter("@d13", OleDbType.VarChar, 15, "Status"))
cmd.Parameters("@d1").Value = DepartmentCodeTextBox2.Text
cmd.Parameters("@d2").Value = EmployeeCodeTextBox.Text
cmd.Parameters("@d3").Value = Employee_NameTextBox.Text
cmd.Parameters("@d4").Value = Last_NameTextBox.Text
cmd.Parameters("@d5").Value = GenderComboBox.Text
cmd.Parameters("@d6").Value = BirthDatePicker.Value
cmd.Parameters("@d7").Value = Contact_NumberTextBox.Text
cmd.Parameters("@d8").Value = AddressTextBox.Text
cmd.Parameters("@d9").Value = EmailTextBox.Text
cmd.Parameters("@d10").Value = DesignationComboBox.Text
cmd.Parameters("@d11").Value = DateEmployedPicker.Value
cmd.Parameters("@d12").Value = Basic_PayTextBox.Text
cmd.Parameters("@d13").Value = StatusComboBox.Text
Dim photo As OleDbParameter = New OleDbParameter("@d14", SqlDbType.Image)
photo.Value = DataPic_Update
cmd.Parameters.Add(photo)
cmd.ExecuteReader()
MessageBox.Show("Record successfully saved.")
SaveButton1.Enabled = False
EmployeeCodeTextBox.Focus()
con.Close()
End Sub
Private Sub Basic_PayTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Basic_PayTextBox.KeyPress
Dim keyChar = e.KeyChar
If Char.IsControl(keyChar) Then
'Allow all control characters.
ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
Dim text = Me.Basic_PayTextBox.Text
Dim selectionStart = Me.Basic_PayTextBox.SelectionStart
Dim selectionLength = Me.Basic_PayTextBox.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
'Reject an integer that is longer than 16 digits.
e.Handled = True
ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
'Reject a real number with two many decimal places.
e.Handled = False
End If
Else
'Reject all other characters.
e.Handled = True
End If
End Sub
Private Sub Employee_NameTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Employee_NameTextBox.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
'space accepted
If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
e.Handled = True
End If
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub
Private Sub EmailTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EmailTextBox.Validating
Dim rEMail As New System.Text.RegularExpressions.Regex("^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")
If EmailTextBox.Text.Length > 0 Then
If Not rEMail.IsMatch(EmailTextBox.Text) Then
MessageBox.Show("Invalid Email Address", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
EmailTextBox.SelectAll()
e.Cancel = True
End If
End If
End Sub
Private Sub DeleteButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton1.Click
Dim promptQuestion As DialogResult = MessageBox.Show("Are you sure you want delete this record?", "Delete Record?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If promptQuestion = Windows.Forms.DialogResult.No Then
Delete_Records()
Exit Sub
End If
MessageBox.Show("Record successfully deleted.")
End Sub
Private Sub Delete_Records()
con = New OleDbConnection(cs)
con.Open()
Dim ct As String = "Select EmployeeCode FROM OT_Amount WHERE EmployeeCode=@find"
cmd = New OleDbCommand(ct)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@find", OleDbType.VarChar, 5, "EmployeeCode"))
cmd.Parameters("@find").Value = EmployeeCodeTextBox.Text
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Unable to delete..Already in use", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Reset()
If Not rdr Is Nothing Then
rdr.Close()
End If
Exit Sub
End If
con = New OleDbConnection(cs)
con.Open()
Dim ct2 As String = "Select EmployeeCode FROM Loan WHERE EmployeeCode=@find"
cmd = New OleDbCommand(ct2)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@find", OleDbType.VarChar, 6, "EmployeeCode"))
cmd.Parameters("@find").Value = EmployeeCodeTextBox.Text
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Unable to delete..Already in use", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Reset()
If Not rdr Is Nothing Then
rdr.Close()
End If
Exit Sub
End If
con = New OleDbConnection(cs)
con.Open()
Dim ct1 As String = "Select EmployeeCode FROM Payroll_Calculator WHERE EmployeeCode=@find"
cmd = New OleDbCommand(ct1)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@find", OleDbType.VarChar, 6, "EmployeeCode"))
cmd.Parameters("@find").Value = EmployeeCodeTextBox.Text
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Unable to delete..Already in use", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Reset()
If Not rdr Is Nothing Then
rdr.Close()
End If
Exit Sub
End If
Dim RowsAffected As Integer = 0
con = New OleDbConnection(cs)
con.Open()
Dim cq As String = "Delete from Staff_Details where EmployeeCode=@DELETE1;"
cmd = New OleDbCommand(cq)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@DELETE1", OleDbType.VarChar, 6, "EmployeeCode"))
cmd.Parameters("@DELETE1").Value = Trim(EmployeeCodeTextBox.Text)
RowsAffected = cmd.ExecuteNonQuery()
If RowsAffected > 0 Then
MessageBox.Show("Successfully Deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
Reset()
Employee_NameTextBox.Focus()
UpdateButton1.Enabled = False
DeleteButton1.Enabled = False
Else
MessageBox.Show("No Record Found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Reset()
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Close()
End If
End Sub
Private Sub ButtonSpecAny1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSpecAny1.Click
GenderComboBox.Text = ""
End Sub
Private Sub ButtonSpecAny3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSpecAny3.Click
DesignationComboBox.Text = ""
End Sub
Private Sub ButtonSpecAny2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSpecAny2.Click
StatusComboBox.Text = ""
End Sub
Private Sub ExitButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton1.Click
If MsgBox("Are You Sure You Want To Quit The Program?", vbYesNo, Title:="Close Confirmation") = vbYes Then
Me.Close()
End If
End Sub
Private Sub UpdateButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton1.Click
If Len(Trim(EmployeeCodeTextBox.Text)) = 0 Then
MessageBox.Show("Please Select Employee Code", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
EmployeeCodeTextBox.Focus()
Exit Sub
End If
con = New OleDbConnection(cs)
con.Open()
Dim cb As String = "Update Staff_Details set EmployeeName='" & Employee_NameTextBox.Text & "',LastName='" & Last_NameTextBox.Text & "',Gender='" & GenderComboBox.Text & "',BirthDate='" & BirthDatePicker.Value & "',ContactNumber='" & Contact_NumberTextBox.Text & "',Address='" & AddressTextBox.Text & "',Email='" & EmailTextBox.Text & "',Designation='" & DesignationComboBox.Text & "',DateEmployed='" & DateEmployedPicker.Value & "',BasicSalary='" & Basic_PayTextBox.Text & "',Status='" & StatusComboBox.Text & "' WHERE EmployeeCode='" & EmployeeCodeTextBox.Text & "' DepartmentCode='" & DepartmentCodeTextBox2.Text & "'"
cmd = New OleDbCommand(cb)
cmd.Connection = con
If MessageBox.Show("Are you sure want to update the record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
cmd.ExecuteReader()
MessageBox.Show("Successfully Updated", "Staff Details", MessageBoxButtons.OK, MessageBoxIcon.Information)
Employee_NameTextBox.Focus()
UpdateButton1.Enabled = False
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Close()
End Sub
'Browse
Private Sub BrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseButton.Click
With OpenFileDialog1
.InitialDirectory = "C:\Users\user\Documents\Visual Studio 2010\Projects\Bakery Payroll System - Nayomi's\Profile_Pictures"
.Filter = "Bitmaps (*.bmp)|*.bmp|GIFs (*.gif)|*.gif|JPEGs (*.jpg)|*.jpg|All Image File (*.jpg,*.bmp,*.gif)|*.jpg;*.bmp;*.gif"
.FilterIndex = 2
End With
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
With PictureBox1
.Image = Image.FromFile(Me.OpenFileDialog1.FileName)
.SizeMode = PictureBoxSizeMode.AutoSize
End With
End If
End Sub
End Class
End Class
-
Feb 27th, 2014, 11:24 AM
#3
Re: Format Exception Occurred
It doesn't seem likely that the error could really be coming from that line. The line is nothing more than assigning a string to a string variable. There are rare cases where the debugger doesn't take you to quite the right line, but this doesn't seem like it could be one of them. You can test that pretty easily, though. Simply comment out the assignment so the code just looks like this:
Dim cs As String '= <whatever else is here>
The program will crash, eventually, because cs isn't set to anything, but I think what will happen is that you will get the exact same error you are currently getting, and at the exact same place, which would confirm that the error is not really on that line at all. The other way to confirm that would be to move the whole declaration of cs earlier. Cut that line from where it is and paste it up above something like dbProvider. If that line is really the problem, the error will move up to the new location. If the error is not really on that line, the exception will appear on a different line.
By the way, cs clearly stands for connection string, but in the load event you build a connection string from the two variables dbProvider and dbSource, even though these two would have the exact same contents as cs. So, you are duplicating things, there.
My usual boring signature: Nothing
-
Feb 27th, 2014, 11:53 AM
#4
Thread Starter
Addicted Member
Re: Format Exception Occurred
Originally Posted by Shaggy Hiker
It doesn't seem likely that the error could really be coming from that line. The line is nothing more than assigning a string to a string variable. There are rare cases where the debugger doesn't take you to quite the right line, but this doesn't seem like it could be one of them. You can test that pretty easily, though. Simply comment out the assignment so the code just looks like this:
Dim cs As String '= <whatever else is here>
The program will crash, eventually, because cs isn't set to anything, but I think what will happen is that you will get the exact same error you are currently getting, and at the exact same place, which would confirm that the error is not really on that line at all. The other way to confirm that would be to move the whole declaration of cs earlier. Cut that line from where it is and paste it up above something like dbProvider. If that line is really the problem, the error will move up to the new location. If the error is not really on that line, the exception will appear on a different line.
By the way, cs clearly stands for connection string, but in the load event you build a connection string from the two variables dbProvider and dbSource, even though these two would have the exact same contents as cs. So, you are duplicating things, there.
I tried all your methods, then when i run the program I got the same error but this time I got it from the declarations I used in the top.
Code:
Dim rdr As OleDbDataReader = Nothing
Dim dtable As New DataTable
Dim con As OleDbConnection = Nothing
Dim adp As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand = Nothing
Dim dt As New DataTable
when I change or delete a variable above variables I get the same error.
And i also deleted this
Code:
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Payroll_BackEnd.accdb"
and used dbprovider and dbsource like you suggested. but still it gives the same error.
can you please help me>
-
Feb 27th, 2014, 11:59 AM
#5
Re: Format Exception Occurred
When do you get the error? When it first loads? When you click a button?
-
Feb 27th, 2014, 12:07 PM
#6
Thread Starter
Addicted Member
Re: Format Exception Occurred
when I debug/run
It does not show any form when I run the program.
Code:
Imports System.Data.OleDb
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class Staff_Editor
Dim inc As Integer
Dim MaxRows As Integer
Dim conn As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim dj As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String = "SELECT * FROM Department"
Dim rdr As OleDbDataReader = Nothing
Dim dtable As New DataTable
Dim con As OleDbConnection = Nothing
Dim adp As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand = Nothing
Private Sub Staff_Editor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source=|DataDirectory|\Payroll_BackEnd.accdb"
conn.ConnectionString = dbProvider & dbSource
conn.Open()
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(dj, "Payroll_BackEnd")
conn.Close()
MaxRows = dj.Tables("Payroll_BackEnd").Rows.Count
inc = -1
End Sub
Private Sub SaveButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton1.Click
Dim promptQuestion As DialogResult = MessageBox.Show("Are you sure you want to save this record?", "Save Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If promptQuestion = Windows.Forms.DialogResult.No Then
Exit Sub
End If
If Len(Trim(Employee_NameTextBox.Text)) = 0 Then
MessageBox.Show("Please Enter Employee Name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Employee_NameTextBox.Focus()
End If
Try
con = New OleDbConnection(dbProvider & dbSource)
con.Open()
Dim cb As String = "Insert INTO Staff_Details(DepartmentCode,EmployeeCode,EmployeeName,LastName,Gender,BirthDate,ContactNumber,Address,Email,Designation,DateEmployed,BasicSalary,Status,ProfilePicture) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14)"
Dim MemStream As New MemoryStream
Dim DataPic_Update As Byte()
Me.PictureBox1.Image.Save(MemStream, Imaging.ImageFormat.Png)
DataPic_Update = MemStream.GetBuffer()
MemStream.Read(DataPic_Update, 0, MemStream.Length)
cmd = New OleDbCommand(cb)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@d1", OleDbType.VarChar, 4, "DepartmentCode"))
cmd.Parameters.Add(New OleDbParameter("@d2", OleDbType.VarChar, 5, "EmployeeCode"))
cmd.Parameters.Add(New OleDbParameter("@d3", OleDbType.VarChar, 20, "EmployeeName"))
cmd.Parameters.Add(New OleDbParameter("@d4", OleDbType.VarChar, 20, "LastName"))
cmd.Parameters.Add(New OleDbParameter("@d5", OleDbType.VarChar, 6, "Gender"))
cmd.Parameters.Add(New OleDbParameter("@d6", OleDbType.Date, 30, "BirthDate"))
cmd.Parameters.Add(New OleDbParameter("@d7", OleDbType.VarChar, 11, "ContactNumber"))
cmd.Parameters.Add(New OleDbParameter("@d8", OleDbType.VarChar, 100, "Address"))
cmd.Parameters.Add(New OleDbParameter("@d9", OleDbType.VarChar, 50, "Email"))
cmd.Parameters.Add(New OleDbParameter("@d10", OleDbType.VarChar, 15, "Designation"))
cmd.Parameters.Add(New OleDbParameter("@d11", OleDbType.Date, 30, "DateEmployed"))
cmd.Parameters.Add(New OleDbParameter("@d12", OleDbType.VarChar, 10, "BasicSalary"))
cmd.Parameters.Add(New OleDbParameter("@d13", OleDbType.VarChar, 15, "Status"))
cmd.Parameters("@d1").Value = DepartmentCodeTextBox2.Text
cmd.Parameters("@d2").Value = EmployeeCodeTextBox.Text
cmd.Parameters("@d3").Value = Employee_NameTextBox.Text
cmd.Parameters("@d4").Value = Last_NameTextBox.Text
cmd.Parameters("@d5").Value = GenderComboBox.Text
cmd.Parameters("@d6").Value = BirthDatePicker.Value
cmd.Parameters("@d7").Value = Contact_NumberTextBox.Text
cmd.Parameters("@d8").Value = AddressTextBox.Text
cmd.Parameters("@d9").Value = EmailTextBox.Text
cmd.Parameters("@d10").Value = DesignationComboBox.Text
cmd.Parameters("@d11").Value = DateEmployedPicker.Value
cmd.Parameters("@d12").Value = Basic_PayTextBox.Text
cmd.Parameters("@d13").Value = StatusComboBox.Text
Dim photo As OleDbParameter = New OleDbParameter("@d14", SqlDbType.Image)
photo.Value = DataPic_Update
cmd.Parameters.Add(photo)
cmd.ExecuteReader()
MessageBox.Show("Record successfully saved.")
SaveButton1.Enabled = False
EmployeeCodeTextBox.Focus()
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Delete_Records()
con = New OleDbConnection(dbProvider & dbSource)
con.Open()
Dim ct As String = "Select EmployeeCode FROM OT_Amount WHERE EmployeeCode=@find"
cmd = New OleDbCommand(ct)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@find", OleDbType.VarChar, 5, "EmployeeCode"))
cmd.Parameters("@find").Value = EmployeeCodeTextBox.Text
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Unable to delete..Already in use", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Reset()
If Not rdr Is Nothing Then
rdr.Close()
End If
Exit Sub
End If
con = New OleDbConnection(dbProvider & dbSource)
con.Open()
Dim ct2 As String = "Select EmployeeCode FROM Loan WHERE EmployeeCode=@find"
cmd = New OleDbCommand(ct2)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@find", OleDbType.VarChar, 6, "EmployeeCode"))
cmd.Parameters("@find").Value = EmployeeCodeTextBox.Text
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Unable to delete..Already in use", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Reset()
If Not rdr Is Nothing Then
rdr.Close()
End If
Exit Sub
End If
con = New OleDbConnection(dbProvider & dbSource)
con.Open()
Dim ct1 As String = "Select EmployeeCode FROM Payroll_Calculator WHERE EmployeeCode=@find"
cmd = New OleDbCommand(ct1)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@find", OleDbType.VarChar, 6, "EmployeeCode"))
cmd.Parameters("@find").Value = EmployeeCodeTextBox.Text
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Unable to delete..Already in use", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Reset()
If Not rdr Is Nothing Then
rdr.Close()
End If
Exit Sub
End If
Dim RowsAffected As Integer = 0
con = New OleDbConnection(dbProvider & dbSource)
con.Open()
Dim cq As String = "Delete from Staff_Details where EmployeeCode=@DELETE1;"
cmd = New OleDbCommand(cq)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@DELETE1", OleDbType.VarChar, 6, "EmployeeCode"))
cmd.Parameters("@DELETE1").Value = Trim(EmployeeCodeTextBox.Text)
RowsAffected = cmd.ExecuteNonQuery()
If RowsAffected > 0 Then
MessageBox.Show("Successfully Deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
Reset()
Employee_NameTextBox.Focus()
UpdateButton1.Enabled = False
DeleteButton1.Enabled = False
Else
MessageBox.Show("No Record Found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Reset()
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Close()
End If
End Sub
-
Feb 27th, 2014, 12:20 PM
#7
Re: Format Exception Occurred
I created a new project and put in this much of your code:
Code:
Imports System.Data.OleDb
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class Staff_Editor
Dim inc As Integer
Dim MaxRows As Integer
Dim conn As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim dj As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String = "SELECT * FROM Department"
Dim rdr As OleDbDataReader = Nothing
Dim dtable As New DataTable
Dim con As OleDbConnection = Nothing
Dim adp As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand = Nothing
Dim dt As New DataTable
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Payroll_BackEnd.accdb" ...
No error. Can you try that and see what happens for you?
(obviously include the "end class")
-
Feb 27th, 2014, 12:32 PM
#8
Thread Starter
Addicted Member
Re: Format Exception Occurred
I tired by putting your code and nothing happened, I still get the same error.
why is this happening???
please help me!
-
Feb 27th, 2014, 12:51 PM
#9
Re: Format Exception Occurred
Not sure what else to suggest since I can't duplicate your issue in that simple test.
-
Feb 27th, 2014, 01:05 PM
#10
Re: Format Exception Occurred
This one is not going to be easy to figure out, so you'll have to be a bit patient. At this point, I have no clear answer. I wasn't quite clear as to what you meant in #4. The error is still happening, but it is not happening on the same line, right? That is what I expected, because the line that was being displayed shouldn't produce any error. Unfortunately, that makes it harder to find the problem, because you are being taken to the wrong place by the editor.
My usual boring signature: Nothing
-
Feb 27th, 2014, 01:08 PM
#11
Thread Starter
Addicted Member
Re: Format Exception Occurred
Originally Posted by vbfbryce
Not sure what else to suggest since I can't duplicate your issue in that simple test.
I tried deleting some of the coding and then it gives the error same.
when i delete a coding then the error goes to the next coding then it gives an error this goes on and on.... until the page was empty.
-
Feb 27th, 2014, 03:43 PM
#12
Thread Starter
Addicted Member
Re: Format Exception Occurred
-
Feb 27th, 2014, 03:57 PM
#13
Re: Format Exception Occurred
Yeah, the error isn't even in that code.
The exception is occuring as the form is loading, that can cause the debugger to take you to the wrong line, which makes tracking down the problem far more difficult than most issues. In fact, it ends up being nearly a guess.
The issue is on that form, and is occuring very early on. What controls are on the form? Also, is this form the startup form for the application?
My usual boring signature: Nothing
-
Feb 28th, 2014, 12:24 PM
#14
Thread Starter
Addicted Member
Re: Format Exception Occurred
it is not the startup form.
the startup form order is this
Splash Screen ------ Login Form ------ Main Menu ------ Staff Details
i'm using OpenFileDialog, Binding Navigator and ToolTip controls.
this is my form
-
Feb 28th, 2014, 12:26 PM
#15
Thread Starter
Addicted Member
Re: Format Exception Occurred
it is not the startup form.
the startup form order is this
Splash Screen ------ Login Form ------ Main Menu ------ Staff Details
i'm using OpenFileDialog, Binding Navigator and ToolTip controls.
this is my form
-
Feb 28th, 2014, 12:55 PM
#16
Frenzied Member
Re: Format Exception Occurred
So set the app to open each form separately and see if that narrows down what is causing the error.
-
Feb 28th, 2014, 01:14 PM
#17
Re: Format Exception Occurred
I like that approach as a start.
What has me puzzled about this is the fact that the debugger is taking you to a line that is clearly not the source of the exception. Normally, when this happens, it takes you to the previous form, but in your case it seems to be almost jumping around. A bit of searching shows that this particular exception tends to happen when you are supplying lists of parameters, often to Format, Writeline, and others, but also possibly to queries.
I suspect that the problem may be in the code that creates and shows this form, but I'm not certain about that. One point that might matter is that you need to wrap the Load event code in a Try...Catch block. Exceptions thrown in the Load event, and not caught in the load event, can result in some very odd behavior, but typically not THIS behavior. In any case, the code in that load event, since it accesses a database, is code that should be wrapped in exception handling, anyways.
My usual boring signature: Nothing
-
Mar 1st, 2014, 01:33 AM
#18
Thread Starter
Addicted Member
Re: Format Exception Occurred
Originally Posted by CoachBarker
So set the app to open each form separately and see if that narrows down what is causing the error.
tried it and the last line of the below code shows the error. when I delete that line then the error comes from another declaration.
Code:
Dim rdr As OleDbDataReader = Nothing
Dim dtable As New DataTable
Dim con As OleDbConnection = Nothing
Dim adp As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand = Nothing
-
Mar 1st, 2014, 01:36 AM
#19
Thread Starter
Addicted Member
Re: Format Exception Occurred
System.FormatException occurred
Message=Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Source=mscorlib
StackTrace:
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.Windows.Forms.BindingNavigator.RefreshItemsCore()
at System.Windows.Forms.BindingNavigator.OnRefreshItems()
at System.Windows.Forms.BindingNavigator.EndInit()
at Bakery_Payroll_System.Staff_Editor.InitializeComponent() in C:\Users\user\Documents\Visual Studio 2010\Projects\Bakery Payroll System - Nayomi's\Bakery Payroll System - Nayomi's\Staff Editor.Designer.vb:line 697
at Bakery_Payroll_System.Staff_Editor..ctor() in C:\Users\user\Documents\Visual Studio 2010\Projects\Bakery Payroll System - Nayomi's\Bakery Payroll System - Nayomi's\Staff Editor.vb:line 21
InnerException:
-
Mar 1st, 2014, 03:36 AM
#20
Thread Starter
Addicted Member
Re: Format Exception Occurred
I got it fixed, I had a back-up copy and i used that, now there are no problems.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|