下面的代码让我有些头疼。它可能是两年前写的,从那以后一直在慢慢消失,但我们注意到有时插入查询会运行两次,有时根本不运行。该代码基本上从网页(aspx)标签中获取整数和十进制值,并将它们插入到主表中。
最近的问题似乎受到系统上用户数量的影响。当只有一个人使用它时,似乎没有问题,但有多个用户时,问题开始出现。我使用的是会话变量而不是标签,但作为消除过程,我停止使用这些变量。
如果有人可以帮助或给我一些建议,那就太好了,非常感谢。
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
'local
Dim subidString As String = "SELECT [Submission_ID], [Week Ending] FROM tbl_Weekly_Expenses WHERE [Submission_ID] = @SubID AND [Week Ending] = @WeekEnding AND [SentToPayroll] IS NULL"
'local
Dim mtdString As String = "SELECT SUM([Mileage Covered]) AS SumOfMileageCovered FROM tbl_Weekly_Expenses WHERE [Gabem Employee Reference] = @NovaRef"
'remote
Dim vehicleString As String = "SELECT [Vehicle] FROM tblExpensesMileage WHERE [SubID] = @SubID"
'local
Dim insertString As String = "INSERT INTO tbl_Weekly_Expenses ([Week Ending], [Date Expenses Processed], [Expenses_From], [Expenses_To], [Date Expenses received], [Gabem Employee Reference], [Identifying Number], [Submission_ID], [Car], [Motorcycle], [Pushbike], [Mileage Covered], [Mileage Amount Claimed], [Accommodation], [Meal Allowance], [Daily Allowance], [Tools/Clothing], [Details_Of_Tools], [Travel Amount], [Telephone], [Professional Fees], FuelVAT, MAVAT, TAVAT, AVAT, DAVAT, TCVAT, TelVAT, PfVAT, processedby) " & _
"VALUES (@WeekEnding, @DateProcessed, @ExpensesFrom, @ExpensesTo, @DateReceived, @NovaRef, @IdentifyingNumber, @SubID, @Car, @Motorcycle, @Pushbike, @MileageCovered, @MileageAmountClaimed, @Accommodation, @Meals, @DailyAllowance, @ToolsClothing, @ToolsDetails, @Travel, @TelephoneCalls, @ProfessionalFees, @FuelVAT, @MAVAT, @TAVAT, @AVAT, @DAVAT, @TCVAT, @TelVAT, @PfVAT, @processedby)"
'remote
Dim updateMileage As String = "UPDATE tblExpensesMileage SET [Status] = 'Processed' WHERE [MileageID] = @id"
Dim updateAccommodation As String = "UPDATE tblExpensesAccommodation SET [Status] = 'Processed' WHERE [AccommodationID] = @id"
Dim updateMeals As String = "UPDATE tblExpensesMeals SET [Status] = 'Processed' WHERE [MealsID] = @id"
Dim updateLaundry As String = "UPDATE tblExpensesLaundry SET [Status] = 'Processed' WHERE [LaundryID] = @id"
Dim updateDailyAllowance As String = "UPDATE tblExpensesDailyAllowance SET [Status] = 'Processed' WHERE [DailyAllowanceID] = @id"
Dim updateTravel As String = "UPDATE tblExpensesTravel SET [Status] = 'Processed' WHERE [TravelID] = @id"
Dim updateTools As String = "UPDATE tblExpensesTools SET [Status] = 'Processed' WHERE [ToolsID] = @id"
Dim updateTelephoneCalls As String = "UPDATE tblExpensesTelephoneCalls SET [Status] = 'Processed' WHERE [TelephoneCallsID] = @id"
Dim updateProfessionalFees As String = "UPDATE tblExpensesProfessionalFees SET [Status] = 'Processed' WHERE [ProfessionalFeesID] = @id"
Dim dbconnLocal As String = ConfigurationManager.ConnectionStrings("connection_nbsas2").ConnectionString
Dim dbconnRemote As String = ConfigurationManager.ConnectionStrings("connection_nbsa_web").ConnectionString
If txtWEDate.Text = String.Empty Then
Exit Sub
End If
Using myConnectionLocal As New SqlConnection(dbconnLocal)
myConnectionLocal.Open()
'VEHICLE TYPE AND PPM STARTS
Dim Car As Integer = 0
Dim Motorcycle As Integer = 0
Dim Pushbike As Integer = 0
Dim ppm As Decimal
Using myConnectionRemote As New SqlConnection(dbconnRemote)
myConnectionRemote.Open()
'Vehicle type
Dim cmdSelectVehicle As New SqlCommand(vehicleString, myConnectionRemote)
cmdSelectVehicle.Parameters.AddWithValue("@SubID", Session("subid"))
Dim readerVehicle As SqlDataReader = cmdSelectVehicle.ExecuteReader()
readerVehicle.Read()
If readerVehicle.HasRows = True Then
'MTD value for pence per mile (ppm)
Dim cmdSelectMTD As New SqlCommand(mtdString, myConnectionLocal)
cmdSelectMTD.Parameters.AddWithValue("@NovaRef", Session("novaref"))
Dim readerMTD As SqlDataReader = cmdSelectMTD.ExecuteReader()
readerMTD.Read()
If IsDBNull(readerMTD.Item(0)) = True Then
If readerVehicle.Item(0) = "Car" Then
ppm = 0.45
Car = "1"
ElseIf readerVehicle.Item(0) = "Motorcycle" Then
ppm = 0.24
Motorcycle = "1"
Else
ppm = 0.2
Pushbike = "1"
End If
Else
If readerVehicle.Item(0) = "Motorcycle" Then
ppm = 0.24
Motorcycle = "1"
ElseIf readerVehicle.Item(0) = "Car" And readerMTD.Item(0) < 10000 Then
ppm = 0.45
Car = "1"
ElseIf readerVehicle.Item(0) = "Car" And readerMTD.Item(0) > 10000 Then
ppm = 0.25
Car = "1"
Else
ppm = 0.2
Pushbike = "1"
End If
End If
readerMTD.Close()
End If
readerVehicle.Close()
myConnectionRemote.Close()
End Using
'VEHICLE TYPE AND PPM ENDS
'insert parameters etc
Dim cmdInsertWE As New SqlCommand(insertString, myConnectionLocal)
cmdInsertWE.Parameters.AddWithValue("@WeekEnding", CType(txtWEDate.Text, DateTime))
cmdInsertWE.Parameters.AddWithValue("@DateProcessed", Now())
cmdInsertWE.Parameters.AddWithValue("@ExpensesFrom", CType(lblMinDate.Text, DateTime))
cmdInsertWE.Parameters.AddWithValue("@ExpensesTo", CType(lblMaxDate.Text, DateTime))
cmdInsertWE.Parameters.AddWithValue("@DateReceived", CType(Session("daterec"), DateTime))
cmdInsertWE.Parameters.AddWithValue("@NovaRef", Session("novaref"))
cmdInsertWE.Parameters.AddWithValue("@IdentifyingNumber", "1")
cmdInsertWE.Parameters.AddWithValue("@SubID", Session("subid"))
cmdInsertWE.Parameters.AddWithValue("@Car", Car)
cmdInsertWE.Parameters.AddWithValue("@Motorcycle", Motorcycle)
cmdInsertWE.Parameters.AddWithValue("@Pushbike", Pushbike)
cmdInsertWE.Parameters.AddWithValue("@MileageCovered", CType(lblMileageNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@MileageAmountClaimed", CType(lblMileageNumber.Text, Decimal) * ppm)
cmdInsertWE.Parameters.AddWithValue("@Accommodation", CType(lblAccommodationNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@Meals", CType(lblMealsNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@DailyAllowance", CType(lblDailyAllowanceNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@ToolsClothing", CType(lblToolsNumber.Text, Decimal) + CType(lblLaundryNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@ToolsDetails", txtToolsDetails.Text)
cmdInsertWE.Parameters.AddWithValue("@Travel", CType(lblTravelNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@TelephoneCalls", CType(lblTelephoneCallsNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@ProfessionalFees", CType(lblProfessionalFeesNumber.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@FuelVAT", CType(txtRec_Mileage.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@MAVAT", CType(txtRec_Meals.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@TAVAT", CType(txtRec_Travel.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@AVAT", CType(txtRec_Accommodation.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@DAVAT", CType(txtRec_Daily.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@TCVAT", CType(txtRec_ToolsLaundry.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@TelVAT", CType(txtRec_Telephone.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@PfVAT", CType(txtRec_ProFees.Text, Decimal))
cmdInsertWE.Parameters.AddWithValue("@processedby", Page.User.Identity.Name)
cmdInsertWE.ExecuteNonQuery()
cmdInsertWE.Dispose()
myConnectionLocal.Close()
'also clear totals labels
lblMileageNumber.Text = 0
lblAccommodationNumber.Text = 0
lblMealsNumber.Text = 0
lblLaundryNumber.Text = 0
lblDailyAllowanceNumber.Text = 0
lblTravelNumber.Text = 0
lblToolsNumber.Text = 0
txtToolsDetails.Text = String.Empty
lblTelephoneCallsNumber.Text = 0
lblProfessionalFeesNumber.Text = 0
End Using 'myConnectionLocal ENDS
'OPEN REMOTE CONNECTION AND UPDATE STATUS OF LIVE TABLES
Using myConnectionRemote As New SqlConnection(dbconnRemote)
myConnectionRemote.Open()
'Mileage()
updateCheckBoxes(MileageGridView, "MileageHeaderCheckBox", "MileageCheckBox", updateMileage, myConnectionRemote, lblMileageNumber)
'Accommodation()
updateCheckBoxes(AccommodationGridView, "AccommodationHeaderCheckBox", "AccommodationCheckBox", updateAccommodation, myConnectionRemote, lblAccommodationNumber)
'Meals()
updateCheckBoxes(MealsGridView, "MealsHeaderCheckBox", "MealsCheckBox", updateMeals, myConnectionRemote, lblMealsNumber)
'Laundry()
updateCheckBoxes(LaundryGridView, "LaundryHeaderCheckBox", "LaundryCheckBox", updateLaundry, myConnectionRemote, lblLaundryNumber)
'Daily(Allowance)
updateCheckBoxes(DailyAllowanceGridView, "DailyAllowanceHeaderCheckBox", "DailyAllowanceCheckBox", updateDailyAllowance, myConnectionRemote, lblDailyAllowanceNumber)
'Travel()
updateCheckBoxes(TravelGridView, "TravelHeaderCheckBox", "TravelCheckBox", updateTravel, myConnectionRemote, lblTravelNumber)
'Tools()
updateCheckBoxes(ToolsGridView, "ToolsHeaderCheckBox", "ToolsCheckBox", updateTools, myConnectionRemote, lblToolsNumber)
'Telephone(Calls)
updateCheckBoxes(TelephoneCallsGridView, "TelephoneCallsHeaderCheckBox", "TelephoneCallsCheckBox", updateTelephoneCalls, myConnectionRemote, lblTelephoneCallsNumber)
'Professional(Fees)
updateCheckBoxes(ProfessionalFeesGridView, "ProfessionalFeesHeaderCheckBox", "ProfessionalFeesCheckBox", updateProfessionalFees, myConnectionRemote, lblProfessionalFeesNumber)
myConnectionRemote.Close()
End Using 'myConnectionRemote ENDS
txtWEDate.Text = String.Empty
End Sub