kwangu

Control.CheckForIllegalCrossThreadCalls

Jun 11th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.95 KB | None | 0 0
  1. Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  2.         count_rows()
  3.     End Sub
  4.  
  5.     Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
  6.         MsgBox("Products imported successfully!", MsgBoxStyle.Information, "Import")
  7.         Me.Close()
  8.     End Sub
  9. Public Sub count_rows()
  10.         import_attendance_sheet(1054)
  11.     End Sub
  12. Private Sub import_attendance_sheet(ByVal id As Integer)
  13.         ProgressBar1.Minimum = 0
  14.         ProgressBar1.Maximum = id
  15.         ProgressBar1.Value = 0
  16.         Dim path As String = txtPath.Text
  17.         Dim excel_connection As OleDbConnection
  18.         Dim dt As DataTable
  19.         Dim cmd As OleDbDataAdapter
  20.         'Dim sql As String
  21.         'Dim result As Boolean
  22.         Dim emp_type_id As String = ""
  23.         Dim branch_id As String = ""
  24.         Dim bank_id As String = ""
  25.         'Dim sheet_dates As New List(Of String)
  26.         'excel_connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + path + ";Extended Properties=Excel 12.0 Xml; HDR=Yes;")
  27.         excel_connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0 Xml;HDR=No;IMEX=1;';")
  28.         cmd = New OleDbDataAdapter("SELECT * FROM [sheet$]", excel_connection)
  29.         dt = New DataTable
  30.         cmd.Fill(dt)
  31.         'initialize symbol row
  32.         Dim count As Integer = 6
  33.         'Loop through dates(column/header)
  34.         For Each column As DataColumn In dt.Columns
  35.             Dim colum_name As String = dt.Rows(0)(column.ColumnName).ToString()
  36.             'check if column cell is empty
  37.             If colum_name = "" Then
  38.                 'Empty do nothing
  39.             Else
  40.                 'increment symbol row by 1
  41.                 count = count + 1
  42.                 'MsgBox(count)
  43.                 'Loop through rows of a particular date/column/header
  44.                 For Each r As DataRow In dt.Rows
  45.                     'check row(empNo) cell is not empty & does not have a string
  46.                     If r(5).ToString() = "" Or r(5).ToString() = "COY #" Then
  47.                         'Empty do nothing
  48.                     Else
  49.                         'show date | Emp No | Name | symbol index
  50.                         'MsgBox(colum_name & " " & r(5).ToString() & " " & r(6).ToString() & " " & r(count).ToString())
  51.                         'do the calculation
  52.                         Dim employ_id As String = get_employee_id(r(5).ToString)
  53.                         Dim basic_salary As Decimal = get_employee_basic_salary(r(5).ToString)
  54.                         Dim staff_type_id As String = get_employee_type_id(r(5).ToString)
  55.                         Dim days_per_month As Integer = get_employee_days_per_month(staff_type_id)
  56.                         Dim hours_per_day As Double = get_employee_hours_per_day(staff_type_id)
  57.                         Dim hourly_rate As Double = basic_salary / days_per_month / hours_per_day
  58.                         Dim daily_rate As Double = basic_salary / days_per_month
  59.                         Dim normal_working_hrs As String = get_normal_working_hrs()
  60.                         Dim shift_duration As String = get_shift_duration()
  61.                         'get symbol id and its rate
  62.                         Dim symbol_id As String = get_attendance_symbol_id(r(count).ToString)
  63.                         Dim symbol_rate As Double = get_attendance_symbol_rate(symbol_id)
  64.                         Dim symbol_deduction_status As String = get_symbol_deduction_status(symbol_id)
  65.                         Dim td_amount As Double = 0
  66.                         If symbol_deduction_status = "DEDUCT" Then
  67.                             td_amount = hourly_rate
  68.                         Else
  69.                             td_amount = 0
  70.                         End If
  71.                         Dim overtime As Double = shift_duration - normal_working_hrs
  72.                         Dim ot_amount As Double = overtime * hourly_rate * symbol_rate
  73.                         Dim chaka As String = Date.Now.ToString("yyyy")
  74.                         Dim tsiku As String = Date.Now.ToString("dd")
  75.                         Dim tsiku_mawu As String = Date.Now.ToString("dddd")
  76.                         Dim mwezi As String = Date.Now.ToString("MMMM")
  77.                         ' ''insert values into DB
  78.                         sql = "INSERT INTO tbl_attendance (employee_id,time_in,time_out,total_hours_worked,overtime,ot_amount,td_amount,attendance_code_id,attendance_code,attendance_date,comment,year,date,day,month,hourly_rate,bsalary,ot_status) VALUES ('" & employ_id & "','" & 0 & "','" & 0 & "','" & shift_duration & "','" & overtime & "','" & ot_amount & "','" & td_amount & "','" & symbol_id & "','" & r(count).ToString & "','" & calc_attendance_date(colum_name) & "','import','" & chaka & "','" & tsiku & "','" & tsiku_mawu & "','" & mwezi & "','" & hourly_rate & "','" & basic_salary & "','" & symbol_rate & "')"
  79.                         result = save_to_db(sql)
  80.                         ProgressBar1.Value = ProgressBar1.Value + 1
  81.                         'If result Then
  82.                         '    Timer1.Start()
  83.                         'End If
  84.                     End If
  85.                 Next
  86.             End If
  87.         Next
  88.     End Sub
  89. '******* Function which shows the error ****************
  90. Public Function calc_attendance_date(ByVal value As String)
  91.         Dim at_date As String = ""
  92.         Dim current_month As String = frmMain.cmbMonth.Text
  93.         Dim current_year As String = frmMain.cmbYear.Text
  94.         Dim mwezi As String
  95.         Dim chaka As String
  96.         Dim format_day As String = ""
  97.         Dim format_month As String = ""
  98.         'Date.Now.ToString("yyyy-MM-dd")
  99.         '****  find previous month
  100.         'when its january
  101.         If current_month = "January" And value >= 22 And value <= 31 Then
  102.             mwezi = "12"
  103.             chaka = Convert.ToInt32(current_year) - 1
  104.             at_date = chaka & "-" & mwezi & "-" & value
  105.         ElseIf current_month <> "January" And value >= 22 And value <= 31 Then
  106.             mwezi = IntMonth() - 1
  107.             'day
  108.             If value < 10 Then
  109.                 format_day = "0" & value
  110.             ElseIf value >= 10 Then
  111.                 format_day = value
  112.             End If
  113.             'format mwezi
  114.             If mwezi < 10 Then
  115.                 format_month = "0" & mwezi
  116.             ElseIf mwezi >= 10 Then
  117.                 format_month = mwezi
  118.             End If
  119.             chaka = current_year
  120.             at_date = chaka & "-" & format_month & "-" & format_day
  121.         End If
  122.         '****  find current month
  123.         If current_month = "January" And value >= 1 And value <= 21 Then
  124.             mwezi = IntMonth()
  125.             chaka = current_year
  126.             'day
  127.             If value < 10 Then
  128.                 format_day = "0" & value
  129.             ElseIf value >= 10 Then
  130.                 format_day = value
  131.             End If
  132.             'format mwezi
  133.             If mwezi < 10 Then
  134.                 format_month = "0" & mwezi
  135.             ElseIf mwezi >= 10 Then
  136.                 format_month = mwezi
  137.             End If
  138.             at_date = chaka & "-" & format_month & "-" & format_day
  139.         ElseIf current_month <> "January" And value >= 1 And value <= 21 Then
  140.             mwezi = IntMonth()
  141.             chaka = current_year
  142.             'day
  143.             If value < 10 Then
  144.                 format_day = "0" & value
  145.             ElseIf value >= 10 Then
  146.                 format_day = value
  147.             End If
  148.             'format mwezi
  149.             If mwezi < 10 Then
  150.                 format_month = "0" & mwezi
  151.             ElseIf mwezi >= 10 Then
  152.                 format_month = mwezi
  153.             End If
  154.             at_date = chaka & "-" & format_month & "-" & format_day
  155.         End If
  156.         Return at_date
  157.     End Function
Add Comment
Please, Sign In to add comment