ASP Classic on IIS 7 - variable data seems to pass between sessions - sql

I have a page that collects data that is passed to it via http querystring and parses it and stores it in SQL. The data includes drive data about the server checking in. The check in occurs with a custom VB service.
What seems to happen intermittently, is drive data from one server seems to end up stored under another server. Here's a meta representation of the mix-up:
SERVER--TIME--CDRIVE--DDRIVE--NOTE
SERVER-A--1215--355--126--This is a normal check-in for Server-A
SERVER-B--1215--1267--426--This is a normal check-in for Server-B
SERVER-A--1230--1267--426--This is a garbled check-in with B's data instead of A's
SERVER-B--1230--1267--426--This is normal, but is where the bad data comes from
The mix-up does not occur between any particular servers, just between two servers that happened to check in at the same moment. I have tried disabling output caching and most recently I have tried adding "session.abandon" to my code.
Any guidance would be greatly appreciated. I don't do web development as my primary task. I didn't even notice the issue until I set the drive data to loop after 7 days instead of looping every 24 hours.
Here's the code - the include just creates an "oconn" sql object.
<%language=vbscript%>
<!--#include file="includes/db.inc"-->
<%
'Variable Declaration
'---------------------
Private hsid
'COMMON SECTION
'--------------
hsid = request.querystring("hsid")
if isnumeric(hsid) then
hsid = cint(hsid)
else
oconn.close
session.abandon
response.end
end if
sessionid = now() & hsid
Session("SessionID") = sessionid
response.write(Session("SessionID") & "<br/>")
htime = now()
hdate = datevalue(htime)
hhour = hour(htime)
hminute = minute(htime)
hdur = 300
hver = request.querystring("hver")
if hver = "" then
hver = "Legacy"
end if
hupver = request.querystring("hupver")
if hupver = "" then
hupver = "Legacy"
end if
response.write("hupver = *" & hupver & "*<br/>")
'Stop processing if the server is not supported or the Company is not Icon.
sql = "select u.txtUserFirstName from users u inner join site s on u.fkySiteID = s.keySiteID inner join Company c on s.fkyCompanyID = c.keyCompanyID where u.keyUserID = " & hsid & " and u.binUserSupport = 1 and c.binCompanyIcon = 1"
set rs = oconn.execute(sql)
if rs.eof then
response.write("No Support")
session.abandon
response.end
end if
rs.close
set rs = nothing
function getfree(inputstring)
gfindex = instr(inputstring,"-")
' response.write(gfindex & "<br/>")
outputstring = mid(inputstring, 1, gfindex-1)
getfree = outputstring
end function
function getfull(inputstring)
gfindex = instr(inputstring,"-")
outputstring = mid(inputstring, gfindex+1, len(inputstring)-gfindex)
getfull = outputstring
end function
'DRIVES SECTION
'--------------
drivelow = 0 'captures whether any drive is below 1 GB free space for later use
Private drivearr(3,26)
'drivearr(1,x) is for thresholds, (2,x) is for free space, (3,x) is for total capacity
'all drives will actually count from 3 (c) to 26 (z)
'we collect drive threshold settings from the DriveThreshold table, or otherwise generate a default entry for it.
sql = "select * from DriveThreshold where fkyUserID = " & hsid
set rs = oconn.execute(sql)
if not rs.eof then
drivearr(1,3) = cdbl(rs("intcdrive"))*1024
drivearr(1,4) = cdbl(rs("intddrive"))*1024
drivearr(1,5) = cdbl(rs("intedrive"))*1024
drivearr(1,6) = cdbl(rs("intfdrive"))*1024
drivearr(1,7) = cdbl(rs("intgdrive"))*1024
drivearr(1,8) = cdbl(rs("inthdrive"))*1024
drivearr(1,9) = cdbl(rs("intidrive"))*1024
drivearr(1,10) = cdbl(rs("intjdrive"))*1024
drivearr(1,11) = cdbl(rs("intkdrive"))*1024
drivearr(1,12) = cdbl(rs("intldrive"))*1024
drivearr(1,13) = cdbl(rs("intmdrive"))*1024
drivearr(1,14) = cdbl(rs("intndrive"))*1024
drivearr(1,15) = cdbl(rs("intodrive"))*1024
drivearr(1,16) = cdbl(rs("intpdrive"))*1024
drivearr(1,17) = cdbl(rs("intqdrive"))*1024
drivearr(1,18) = cdbl(rs("intrdrive"))*1024
drivearr(1,19) = cdbl(rs("intsdrive"))*1024
drivearr(1,20) = cdbl(rs("inttdrive"))*1024
drivearr(1,21) = cdbl(rs("intudrive"))*1024
drivearr(1,22) = cdbl(rs("intvdrive"))*1024
drivearr(1,23) = cdbl(rs("intwdrive"))*1024
drivearr(1,24) = cdbl(rs("intxdrive"))*1024
drivearr(1,25) = cdbl(rs("intydrive"))*1024
drivearr(1,26) = cdbl(rs("intzdrive"))*1024
else
sql = "insert into DriveThreshold (fkyUserID, intCDrive, intDDrive, intEDrive, intFDrive, intGDrive, intHDrive, intIDrive, intJDrive, intKDrive, intLDrive, intMDrive, intNDrive, intODrive, intPDrive, intQDrive, intRDrive, intSDrive, intTDrive, intUDrive, intVDrive, intWDrive, intXDrive, intYDrive, intZDrive) values (" & hsid & ", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)"
oconn.execute(sql)
for i = 3 to 26
drivearr(1,i) = 1*1024
next
end if
rs.close
set rs = nothing
'Load the null value (-1) for all positions in the drivearr for free spacce (2,x) and total capacity (3,x)
for i = 3 to 26
drivearr(2,i) = -1
drivearr(3,i) = -1
next
cdrive = request.querystring("c") 'collect C Drive data
' response.write("cdrive=" & cdrive & "<br/>")
if len(cdrive) > 3 then
drivearr(2,3) = getfree(cdrive)
drivearr(3,3) = getfull(cdrive)
end if
if drivearr(2,3) > -1 then 'only if C Drive is being monitored do we do the rest of the work
ddrive = request.querystring("d")
' response.write("ddrive=" & ddrive & "<br/>")
if len(ddrive) > 3 then
drivearr(2,4) = getfree(ddrive)
drivearr(3,4) = getfull(ddrive)
end if
edrive = request.querystring("e")
' response.write("edrive=" & edrive & "<br/>")
if len(edrive) > 3 then
drivearr(2,5) = getfree(edrive)
drivearr(3,5) = getfull(edrive)
end if
fdrive = request.querystring("f")
if len(fdrive) > 3 then
drivearr(2,6) = getfree(fdrive)
drivearr(3,6) = getfull(fdrive)
end if
gdrive = request.querystring("g")
if len(gdrive) > 3 then
drivearr(2,7) = getfree(gdrive)
drivearr(3,7) = getfull(gdrive)
end if
hdrive = request.querystring("h")
if len(hdrive) > 3 then
drivearr(2,8) = getfree(hdrive)
drivearr(3,8) = getfull(hdrive)
end if
idrive = request.querystring("i")
if len(idrive) > 3 then
drivearr(2,9) = getfree(idrive)
drivearr(3,9) = getfull(idrive)
end if
jdrive = request.querystring("j")
if len(jdrive) > 3 then
drivearr(2,10) = getfree(jdrive)
drivearr(3,10) = getfull(jdrive)
end if
kdrive = request.querystring("k")
if len(kdrive) > 3 then
drivearr(2,11) = getfree(kdrive)
drivearr(3,11) = getfull(kdrive)
end if
ldrive = request.querystring("l")
if len(ldrive) > 3 then
drivearr(2,12) = getfree(ldrive)
drivearr(3,12) = getfull(ldrive)
end if
mdrive = request.querystring("m")
if len(mdrive) > 3 then
drivearr(2,13) = getfree(mdrive)
drivearr(3,13) = getfull(mdrive)
end if
ndrive = request.querystring("n")
if len(ndrive) > 3 then
drivearr(2,14) = getfree(ndrive)
drivearr(3,14) = getfull(ndrive)
end if
odrive = request.querystring("o")
if len(odrive) > 3 then
drivearr(2,15) = getfree(odrive)
drivearr(3,15) = getfull(odrive)
end if
pdrive = request.querystring("p")
if len(pdrive) > 3 then
drivearr(2,16) = getfree(pdrive)
drivearr(3,16) = getfull(pdrive)
end if
qdrive = request.querystring("q")
if len(qdrive) > 3 then
drivearr(2,17) = getfree(qdrive)
drivearr(3,17) = getfull(qdrive)
end if
rdrive = request.querystring("r")
if len(rdrive) > 3 then
drivearr(2,18) = getfree(rdrive)
drivearr(3,18) = getfull(rdrive)
end if
sdrive = request.querystring("s")
if len(sdrive) > 3 then
drivearr(2,19) = getfree(sdrive)
drivearr(3,19) = getfull(sdrive)
end if
tdrive = request.querystring("t")
if len(tdrive) > 3 then
drivearr(2,20) = getfree(tdrive)
drivearr(3,20) = getfull(tdrive)
end if
udrive = request.querystring("u")
if len(udrive) > 3 then
drivearr(2,21) = getfree(udrive)
drivearr(3,21) = getfull(udrive)
end if
vdrive = request.querystring("v")
if len(vdrive) > 3 then
drivearr(2,22) = getfree(vdrive)
drivearr(3,22) = getfull(vdrive)
end if
wdrive = request.querystring("w")
if len(wdrive) > 3 then
drivearr(2,23) = getfree(wdrive)
drivearr(3,23) = getfull(wdrive)
end if
xdrive = request.querystring("x")
if len(xdrive) > 3 then
drivearr(2,24) = getfree(xdrive)
drivearr(3,24) = getfull(xdrive)
end if
ydrive = request.querystring("y")
if len(ydrive) > 3 then
drivearr(2,25) = getfree(ydrive)
drivearr(3,25) = getfull(ydrive)
end if
zdrive = request.querystring("z")
if len(zdrive) > 3 then
drivearr(2,26) = getfree(zdrive)
drivearr(3,26) = getfull(zdrive)
end if
'Test to see if any of the drives are low
for i = 3 to 26
if drivearr(2,i) > -1 then
if cdbl(drivearr(2,i)) < cdbl(drivearr(1,i)) then drivelow = 1
end if
next
if drivelow = 1 then 'we set the toggle for the server having 1+ drives low in the Users table.
sql = "update Users set binServerDriveLow = 1 where keyUserID = " & hsid
oconn.execute(sql)
else
sql = "update Users set binServerDriveLow = 0 where keyUserID = " & hsid
oconn.execute(sql)
end if
'----This section determines which of 2016 5-minute-intervals the check-in time matches and rounds the check-in time
rdate = hdate
rdayofweek = weekday(hdate)
rhour = hhour
rsequence = round(hminute/5,0)
if rsequence = 12 then
rsequence = 0
rhour = rhour + 1
if rhour = 24 then
rhour = 0
rdate = dateadd("d", 1, rdate)
rdayofweek = weekday(rdate)
end if
end if
rminute = rsequence * 5
rindex = ((rdayofweek-1) * 288) + (rhour * 12) + rsequence + 1
rtime = rdate & " " & timeserial(rhour, rminute, 0)
response.write("rindex: " & rindex & "<br />")
'----Determines which key value matches the sequence number in the DailyDrive table for use in data punch-down.
sql = "select keyDriveDailyID from DriveDaily where intDriveDailyIndex = " & rindex & " and fkyUserID = " & hsid
set rs = oconn.execute(sql)
if not rs.eof then 'if the sequence is found in the table and has a key value then update the table, otherwise....
ddkey = rs("keyDriveDailyID")
sql = "update DriveDaily set datDriveDailyTime = '" & rtime & "', CDrive = " & drivearr(2,3) & ", CDriveCap = " & drivearr(3,3) & ", DDrive = " & drivearr(2,4) & ", DDriveCap = " & drivearr(3,4) & ", EDrive = " & drivearr(2,5) & ", EDriveCap = " & drivearr(3,5) & ", FDrive = " & drivearr(2,6) & ", FDriveCap = " & drivearr(3,6) & ", GDrive = " & drivearr(2,7) & ", GDriveCap = " & drivearr(3,7) & ", HDrive = " & drivearr(2,8) & ", HDriveCap = " & drivearr(3,8) & ", IDrive = " & drivearr(2,9) & ", IDriveCap = " & drivearr(3,9) & ", JDrive = " & drivearr(2,10) & ", JDriveCap = " & drivearr(3,10) & ", KDrive = " & drivearr(2,11) & ", KDriveCap = " & drivearr(3,11) & ", LDrive = " & drivearr(2,12) & ", LDriveCap = " & drivearr(3,12) & ", MDrive = " & drivearr(2,13) & ", MDriveCap = " & drivearr(3,13) & ", NDrive = " & drivearr(2,14) & ", NDriveCap = " & drivearr(3,14) & ", ODrive = " & drivearr(2,15) & ", ODriveCap = " & drivearr(3,15) & ", PDrive = " & drivearr(2,16) & ", PDriveCap = " & drivearr(3,16) & ", QDrive = " & drivearr(2,17) & ", QDriveCap = " & drivearr(3,17) & ", RDrive = " & drivearr(2,18) & ", RDriveCap = " & drivearr(3,18) & ", SDrive = " & drivearr(2,19) & ", SDriveCap = " & drivearr(3,19) & ", TDrive = " & drivearr(2,20) & ", TDriveCap = " & drivearr(3,20) & ", UDrive = " & drivearr(2,21) & ", UDriveCap = " & drivearr(3,21) & ", VDrive = " & drivearr(2,22) & ", VDriveCap = " & drivearr(3,22) & ", WDrive = " & drivearr(2,23) & ", WDriveCap = " & drivearr(3,23) & ", XDrive = " & drivearr(2,24) & ", XDriveCap = " & drivearr(3,24) & ", YDrive = " & drivearr(2,25) & ", YDriveCap = " & drivearr(3,25) & ", ZDrive = " & drivearr(2,26) & ", ZDriveCap = " & drivearr(3,26) & " where keyDriveDailyID = " & ddkey
' response.write(sql)
' response.write("<br />")
' response.end
oconn.execute(sql)
else '...otherwise add a new entry to the table.
sql = "select numCounter from Counter where txtTableName = 'DriveDaily'"
set cs = oconn.execute(sql)
if not cs.eof then
ddkey = cdbl(cs("numCounter"))
end if
cs.close
set cs = nothing
sql = "insert into DriveDaily(keyDriveDailyID, fkyUserID, datDriveDailyTime, intDriveDailyIndex, CDrive, CDriveCap, DDrive, DDriveCap, EDrive, EDriveCap, FDrive, FDriveCap, GDrive, GDriveCap, HDrive, HDriveCap, IDrive, IDriveCap, JDrive, JDriveCap, KDrive, KDriveCap, LDrive, LDriveCap, MDrive, MDriveCap, NDrive, NDriveCap, ODrive, ODriveCap, PDrive, PDriveCap, QDrive, QDriveCap, RDrive, RDriveCap, SDrive, SDriveCap, TDrive, TDriveCap, UDrive, UDriveCap, VDrive, VDriveCap, WDrive, WDriveCap, XDrive, XDriveCap, YDrive, YDriveCap, ZDrive, ZDriveCap) values (" & ddkey & ", " & hsid & ", '" & rtime & "', " & rindex & ", " & drivearr(2,3) & ", " & drivearr(3,3) & ", " & drivearr(2,4) & ", " & drivearr(3,4) & ", " & drivearr(2,5) & ", " & drivearr(3,5) & ", " & drivearr(2,6) & ", " & drivearr(3,6) & ", " & drivearr(2,7) & ", " & drivearr(3,7) & ", " & drivearr(2,8) & ", " & drivearr(3,8) & ", " & drivearr(2,9) & ", " & drivearr(3,9) & ", " & drivearr(2,10) & ", " & drivearr(3,10) & ", " & drivearr(2,11) & ", " & drivearr(3,11) & ", " & drivearr(2,12) & ", " & drivearr(3,12) & ", " & drivearr(2,13) & ", " & drivearr(3,13) & ", " & drivearr(2,14) & ", " & drivearr(3,14) & ", " & drivearr(2,15) & ", " & drivearr(3,15) & ", " & drivearr(2,16) & ", " & drivearr(3,16) & ", " & drivearr(2,17) & ", " & drivearr(3,17) & ", " & drivearr(2,18) & ", " & drivearr(3,18) & ", " & drivearr(2,19) & ", " & drivearr(3,19) & ", " & drivearr(2,20) & ", " & drivearr(3,20) & ", " & drivearr(2,21) & ", " & drivearr(3,21) & ", " & drivearr(2,22) & ", " & drivearr(3,22) & ", " & drivearr(2,23) & ", " & drivearr(3,23) & ", " & drivearr(2,24) & ", " & drivearr(3,24) & ", " & drivearr(2,25) & ", " & drivearr(3,25) & ", " & drivearr(2,26) & ", " & drivearr(3,26) & ")"
' response.write(sql)
' response.end
oconn.execute(sql)
sql = "update Counter set numCounter = " & ddkey+1 & " where txtTableName = 'DriveDaily'"
oconn.execute(sql)
end if
rs.close
set rs = nothing
end if
'HEARTBEAT SECTION-------------------------------------------------------------
' DATETIME SUBSECTION
' -------------------
dt = request.querystring("dt")
if dt = "" then
' do nothing
else
dtyear = mid(dt,1,4)
dtmonth = mid(dt,5,2)
dtday = mid(dt,7,2)
dthour = mid(dt,9,2)
dtminute = mid(dt,11,2)
dtstring = dtmonth & "/" & dtday & "/" & dtyear & " " & dthour & ":" & dtminute
end if
sql = "select datHeartLast, intHeartNext from Users where keyUserID = " & hsid
set rs = oconn.execute(sql)
if not rs.eof then
hlast = rs("datHeartLast")
hnext = rs("intHeartNext")
end if
rs.close
set rs = nothing
response.write("<br />hlast = " & hlast & "<br />hnext = " & hnext)
if hnext > 0 then
hgap = datediff("s",hlast,htime)
if hnext + 60 < hgap then
sql = "select numCounter from Counter where txtTableName = 'Heart'"
set rs = oconn.execute(sql)
if not rs.eof then
heartid = cdbl(rs("numCounter"))
end if
rs.close
set rs = nothing
sql = "insert into Heart (keyHeartID, fkyServerID, datHeartDownStart, datHeartDownEnd) values (" & heartid & ", " & hsid & ", '" & hlast & "', '" & htime & "')"
oconn.execute(sql)
sql = "update Counter set numCounter = " & heartid + 1 & " where txtTableName = 'Heart'"
oconn.execute(sql)
end if
else
sql = "update Users set datHeartFirst = '" & htime & "' where keyUserID = " & hsid
oconn.execute(sql)
end if
sql = "update Users set datHeartLast = '" & htime & "', intHeartNext = " & hdur & ", datHeartDT = '" & dtstring & "', fkyHeartTicketID = null, binHeartEmailSent = 0, txtHeartVersion = '" & hver & "', txtHeartUpVersion = '" & hupver & "' where keyUserID = " & hsid
oconn.execute(sql)
oconn.close
set oconn = nothing
response.write(rindex & " " & htime & " " & rtime)
session.abandon
%>

Related

Save variable to session in VB.net

This is the code inside of my Handler.ashx at the end of this loop I want to save the variable whereclause to the session so it will know what the previous where clause was.
I am open to any way of doing it, but I believe saving to the session would help me complete this.
I tried: Session["whereclause"] = whereclause but it gave me an error
'4.3 Filtering implementation
If Not (FilterTerm = "" Or FilterCol = "") Then
If Not (FilterCol = "[Study Time]") And InStr(whereclause, "WHERE", vbTextCompare) = 0 Then
whereclause = whereclause & " WHERE " & FilterCol & " = '" & FilterTerm & "'"
ElseIf InStr(whereclause, "WHERE", vbTextCompare) = 0 And FilterCol = "[Study Time]" Then
whereclause = whereclause & " WHERE " & FilterCol & " = '12/30/1899 " & FilterTerm & "'"
ElseIf (FilterCol = "[Study Time]") Then
whereclause = whereclause & " AND " & FilterCol & " = '12/30/1899 " & FilterTerm & "'"
Else
whereclause = whereclause & " AND " & FilterCol & " = '" & FilterTerm & "'"
End If
End If

SQL query is returning multiple rows to my app

I have below code in my app. But it's returning double or triple insert rows. I want to prevent double inserts in my app. But I don't know how. Could you check my SQL query?
IF NOT EXISTS (SELECT TALEPID FROM DEVIR_TALEP_KUPURLU_TEKTEK
WHERE TALEPID = 1
AND TORBABARKOD = N'43vv'
AND DEVIR_TALEP = 110
AND PARATURU = 1
AND KUPUR_TL_KR = N'1tl'
AND ADET = -10
AND CIT_BIRIMID = 64
AND TORBABARKOD IN (SELECT DISTINCT TORBABARKOD
FROM KUPUR_TALEP_TORBALAMA
WHERE TALEPID = 1
AND UPDT_TORBABARKOD = N'43vv'
AND KUPUR_TL_KR = N'1tl'
AND ADET = 10
AND CIT_BIRIMID = 64
AND MUT_CITKABUL = 0
AND PARA_TURU = 1))
BEGIN
UPDATE KUPUR_TALEP_TORBALAMA
SET MUT_CITKABUL = 1,
MUT_CIT_ONAY_PERSONEL_ID = 12,
MUT_CIT_ONAY_PERSONEL = 'tezer',
MUT_CIT_ONAYTARIH = GETDATE()
WHERE ID = 33
IF EXISTS(SELECT ID FROM BIRIMLER_MASTER_KASA
WHERE BIRIMID = 88
AND PARATURU = 1
AND KUPURTURU = 10
AND kupur_birim = N'tl'
AND KUPUR_TL_KR = N'1tl')
BEGIN
UPDATE BIRIMLER_MASTER_KASA
SET ADET = ADET -10
WHERE BIRIMID = 453
AND PARATURU = 1
AND KUPURTURU = 10
AND kupur_birim = N'tl'
AND KUPUR_TL_KR = N'1tl'
END
ELSE
BEGIN
INSERT INTO BIRIMLER_MASTER_KASA (BIRIMID, PARATURU, KUPURTURU, ADET, CIT_BIRIMID, bolen, kupur_birim, KUPUR_TL_KR)
VALUES (453, 1, 10, -10, 64, 1, 'tl', '10tl')
END;
INSERT INTO DEVIR_TALEP_KUPURLU_TEKTEK (MUS_ID, PARATURU, KUPUR, ADET, DEVIR_TALEP, ISLEMTARIHI,
CIT_BIRIMID, bolen, kupur_birim, KUPUR_TL_KR, NM_merkez_kasa_giris_cikis_kod,
NM_merkez_kasa_giris_cikis, hangi_kasayaislendi, TALEPID, TORBABARKOD, sayfakod, kasaturu, vfv)
VALUES (23, 1, 10, -10, 110, GETDATE(),
64, 1, 'tl', '1tl', 1,
'NM de Müşteri Kasasından Çıkan Şube Talep' , 455, 12, '145254',1111111, '0', 698)
END
And this is in my app code. If you will check my code U can see my method. If you have any suggestion for me you can type me. My code:
For Each sndr As GridViewRow In GridView1.Rows
'atm kasaya yüklenen para , burada nm merkezden çıkış var
Dim partr As HiddenField = sndr.FindControl("HiddenField15")
Dim kupr As Label = sndr.FindControl("Label5")
Dim adt As Label = sndr.FindControl("Label7")
Dim nryid As HiddenField = sndr.FindControl("HiddenField16")
Dim tlpid As HiddenField = sndr.FindControl("HiddenField21")
talepidgnl = tlpid.Value
Dim parabirim As Label = sndr.FindControl("Label6")
Dim kprtl_kr As HiddenField = sndr.FindControl("HiddenField14")
Dim bolen As HiddenField = sndr.FindControl("HiddenField8")
Dim islemid As HiddenField = sndr.FindControl("HiddenField9") 'KUPUR_TALEP_TORBALAMA.ID id
Dim torba As Label = sndr.FindControl("Label18")
KASABUL(nryid.Value)
If Session("birime_ait_kasa") = 0 Then
baglikasabak(nryid.Value)
If Session("birime_ait_kasa") = 0 Then
Page.ClientScript.RegisterStartupScript(Me.GetType(), "CallMyFunction", "hatacal('DİKKAT !!! Birim Bir Kasaya Bağlı Değil Yöneticiniz ile Görüşünüz');", True)
Return False
End If
End If
str = "if NOT exists (select TALEPID FROM DEVIR_TALEP_KUPURLU_TEKTEK "
str = str & " WHERE TALEPID=" & tlpid.Value & " AND TORBABARKOD=N'" & torba.Text & "' AND DEVIR_TALEP=510 AND PARATURU=" & partr.Value
str = str & " AND KUPUR_TL_KR =N'" & kprtl_kr.Value & "' AND ADET=-" & adt.Text * 1 & " AND CIT_BIRIMID=" & Session("BIRIMID")
str = str & " AND TORBABARKOD IN ( SELECT DISTINCT UPDT_TORBABARKOD FROM KUPUR_TALEP_TORBALAMA WHERE TALEPID=" & tlpid.Value
str = str & " AND UPDT_TORBABARKOD=N'" & torba.Text & "' "
str = str & " AND KUPUR_TL_KR =N'" & kprtl_kr.Value & "' AND ADET=" & adt.Text * 1 & " AND CIT_BIRIMID=" & Session("BIRIMID") & " AND MUT_CITKABUL=0 and PARA_TURU=" & partr.Value & "))"
str = str & " begin "
'BURASI 2
str = str & " UPDATE KUPUR_TALEP_TORBALAMA set MUT_CITKABUL= 1,MUT_CIT_ONAY_PERSONEL_ID=" & Session("PERSONELID")
str = str & " , MUT_CIT_ONAY_PERSONEL='" & Session("PERSONELADI") & "',MUT_CIT_ONAYTARIH=GETDATE() WHERE ID=" & islemid.Value & " and MUT_CITKABUL=0"
'df.Rows.Add(str)
'atm kasaya yüklenen para , burada nm merkezden çıkış var
' str = ""
str = str & " IF EXISTS(SELECT ID FROM BIRIMLER_MASTER_KASA WHERE BIRIMID=" & Session("birime_ait_kasa") & " AND PARATURU=" & partr.Value & " AND KUPURTURU=" & kupr.Text & " and kupur_birim =N'" & parabirim.Text & "' and KUPUR_TL_KR=N'" & kprtl_kr.Value & "') BEGIN "
str = str & " UPDATE BIRIMLER_MASTER_KASA SET ADET=ADET -" & adt.Text * 1 & " WHERE BIRIMID=" & Session("birime_ait_kasa") & " AND PARATURU=" & partr.Value & " AND KUPURTURU=" & kupr.Text & " and kupur_birim=N'" & parabirim.Text & "' and KUPUR_TL_KR=N'" & kprtl_kr.Value & "' END "
str = str & " ELSE BEGIN insert BIRIMLER_MASTER_KASA (BIRIMID,PARATURU,KUPURTURU,ADET,CIT_BIRIMID,bolen,kupur_birim,KUPUR_TL_KR) VALUES(" & Session("birime_ait_kasa") & "," & partr.Value & "," & kupr.Text & ", - " & adt.Text * 1 & "," & Session("BIRIMID") & "," & bolen.Value & ",'" & parabirim.Text & "' ,'" & kprtl_kr.Value & "' ) End;"
'df.Rows.Add(str)
'0- taleplerde (110-510) nm_kasadan çıkan , 1 de ise devir(150) yada atm kasa boşalan dan nm_kasaya giren
'devirde ise nm merkezine giriş
'22
'str = ""
str = str & " INSERT DEVIR_TALEP_KUPURLU_TEKTEK (MUS_ID,PARATURU,KUPUR,ADET,DEVIR_TALEP,ISLEMTARIHI, CIT_BIRIMID,"
str = str & " bolen,kupur_birim,KUPUR_TL_KR,NM_merkez_kasa_giris_cikis_kod,NM_merkez_kasa_giris_cikis,hangi_kasayaislendi,TALEPID,TORBABARKOD,sayfakod,kasaturu,vfv) "
str = str & " VALUES ("
str = str & nryid.Value & "," & partr.Value * 1 & ","
str = str & kupr.Text * 1 & ",-" & adt.Text * 1 & ",510," 'atm mutabakat kapat sayfası yap
str = str & " GETDATE()," & Session("BIRIMID")
str = str & "," & bolen.Value
str = str & ",'" & parabirim.Text & "' "
str = str & ",'" & kprtl_kr.Value & "' "
str = str & ",1"
str = str & ",'NM de Müşteri Kasasından Çıkan ATM Yatan'," & Session("birime_ait_kasa")
str = str & "," & tlpid.Value & ",'" & torba.Text & "',2222222" & ",'0',758"
str = str & ")"
str = str & " END else begin select 'yok' end "
df.Rows.Add(str)
str = ""
Next
Thanks...

problem converting a regular query to parametrized query

I came across this old piece of code that looks like this:
Dim sql As String = "exec wa_vterm_Insert_upd_Purchase_Card #merchant_id = '" & db.SqlSafe(sMerchantID) & "'" &
", #reference = '" & db.SqlSafe(reference) & "' " &
", #national_tax = " & nationalTax & " " &
", #customer_vat_number = '" & db.SqlSafe(customerVatNumber) & "' " &
", #discount_amount = " & discountAmount & " " &
", #duty_amount = " & dutyAmount & " " &
", #vat_invoice_number = '" & db.SqlSafe(vatInvoiceNumber) & "' " &
", #vat_tax_amount = " & vatTaxAmount & " " &
", #vat_tax_rate = " & vatTaxRate & " " &
", #destination_country_code = '" & db.SqlSafe(destCountryCode) & "' "
Trace.WriteLine(sql, LogLevel.Debug)
db.ExecuteNonQuery(sql)
I am converting this to parameterized query and I coded this:
Dim cmd As New SqlCommand("wa_vterm_Insert_upd_Purchase_Card", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#merchant_id", sqlDbType:=SqlDbType.VarChar).Value = sMerchantID
cmd.Parameters.Add("#reference", sqlDbType:=SqlDbType.VarChar).Value = reference
cmd.Parameters.Add("#national_tax", sqlDbType:=SqlDbType.VarChar).Value = nationalTax
cmd.Parameters.Add("#customer_vat_number", sqlDbType:=SqlDbType.VarChar).Value = customerVatNumber
cmd.Parameters.Add("#discount_amount", sqlDbType:=SqlDbType.VarChar).Value = discountAmount
cmd.Parameters.Add("#duty_amount", sqlDbType:=SqlDbType.VarChar).Value = dutyAmount
cmd.Parameters.Add("#vat_invoice_number", sqlDbType:=SqlDbType.VarChar).Value = vatInvoiceNumber
cmd.Parameters.Add(" #vat_tax_amount", sqlDbType:=SqlDbType.Decimal).Value = vatTaxAmount
cmd.Parameters.Add(" #vat_tax_rate", sqlDbType:=SqlDbType.Decimal).Value = vatTaxRate
cmd.Parameters.Add(" #destination_country_code", sqlDbType:=SqlDbType.VarChar).Value = destCountryCode
Try
cmd.Connection.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Trace.WriteLine(ex.Message & ex.Source, LogLevel.Debug)
Finally
cmd.Connection.Close()
End Try
however with the new piece of code I am getting this exception:
"Procedure or function 'wa_vterm_Insert_upd_Purchase_Card' expects parameter '#vat_tax_amount', which was not supplied."
I don't understand why??
As per the suggestion above the answer was a space in front of the #var_tax_amount

MS Access 2010 VBA Integer Variable not changing in Loop

I have code that is looping through an array of member numbers and retrieving records for each. Each time, I need to use the count of the records returned up to 12. However, once the variable that is to hold the count is set, it will not reset with the next call. It also "jumps" from the first to the last record rather than looping through each. In other words, if there are 4 records returned by the recordset, it will execute for the first and the last and then give an error of "No Current Record" Here is my code:
Dim x As Integer
For i = 1 To intMembers
strGetMemberInfo = "SELECT PatientRecords.[Medication Name], PatientRecords.[First Name], PatientRecords.[Last Name],PatientRecords.[doc phone]" _
& " FROM PatientRecords WHERE member_no ='" & arrMembers(i) & "'"
Set rstMedicine = dbs.OpenRecordset(strGetMemberInfo, dbOpenSnapshot)
Dim intMedicine As Integer
intMedicine = rstMedicine.RecordCount
If intMedicine > 12 Then
intMedicine = 12
End If
Do Until rstMedicine.EOF
For x = 1 To intMedicine
strMedicationField = strMedication & x
strDoctorFNameField = strDoctorFName & x
strDoctorLNameField = strDocotrLName & x
strDoctorPhoneField = strDoctorPhone & x
strSQL = "UPDATE TransformationTable SET " & strMedicationField & " = '" & rstMedicine.Fields("[Medication Name]").Value & "'," & strDoctorFNameField & " = '" & rstMedicine.Fields("[First Name]").Value & "', " & strDoctorLNameField & " = '" & Replace(rstMedicine.Fields("[Last Name]"), "'", "''") & "', " & strDoctorPhoneField & " = '" & rstMedicine.Fields("[doc phone]").Value & "' WHERE member_no ='" & arrMembers(i) & "'"
dbs.Execute strSQL
rstMedicine.MoveNext
Next x
Loop
rstMedicine.Close
Set rstMedicine = Nothing
Next i
In the above code, intMedicinegets set by the first recordset and NEVER changes even though rstMedicine.RecordCount does change.
Any help is appreciated
You have 2 different issues. First, use rstMedicine.MoveLast to move to the bottom of the recordset and get the full count. Second. Your limiting the number of "cycles" to 12, but you are not exiting the loop after intMedicine is 12, so it is still trying to get to the end of the recordset because your code says "Do Until rstMedicine.EOF". Change your code to this:
Dim x As Integer
For i = 1 To intMembers
strGetMemberInfo = "SELECT PatientRecords.[Medication Name], PatientRecords.[First Name], PatientRecords.[Last Name],PatientRecords.[doc phone]" _
& " FROM PatientRecords WHERE member_no ='" & arrMembers(i) & "'"
Set rstMedicine = dbs.OpenRecordset(strGetMemberInfo, dbOpenSnapshot)
rstMedicine.MoveLast
Dim intMedicine As Integer
intMedicine = rstMedicine.RecordCount
If intMedicine > 12 Then
intMedicine = 12
End If
rstMedicine.MoveFirst
Do Until rstMedicine.EOF
For x = 1 To intMedicine
strMedicationField = strMedication & x
strDoctorFNameField = strDoctorFName & x
strDoctorLNameField = strDocotrLName & x
strDoctorPhoneField = strDoctorPhone & x
strSQL = "UPDATE TransformationTable SET " & strMedicationField & " = '" & rstMedicine.Fields("[Medication Name]").Value & "'," & strDoctorFNameField & " = '" & rstMedicine.Fields("[First Name]").Value & "', " & strDoctorLNameField & " = '" & Replace(rstMedicine.Fields("[Last Name]"), "'", "''") & "', " & strDoctorPhoneField & " = '" & rstMedicine.Fields("[doc phone]").Value & "' WHERE member_no ='" & arrMembers(i) & "'"
dbs.Execute strSQL
rstMedicine.MoveNext
If x = 12 Then
Exit Do
End If
Next x
Loop
rstMedicine.Close
Set rstMedicine = Nothing
Next i

Compare records in Excel spreadsheet to what is in DB

I originally thought that i was having a problem with the record set howeverm, it looks like now having done some tinkering that when i pass a value of null into my SQL Query String and attaempt to excute it does not recognise the string NULL to be NULL in SQL
Eg
rst.OPEN "SELECT * FROM XXX WHERE Y = int AND Z = NULL"
Does not work however
rst.OPEN "SELECT * FROM XXX WHERE Y = int AND Z = datetime"
Is working fine??
Sub CompareExcel()
' Macro1 Macro
' Macro recorded Aking
Dim TargetDB As String
Dim TargetDBUser As String
Dim TargetDBPassword As String
ThisWorkbook.Sheets("INPUT").Select
TargetDB = Cells(2, 14)
TargetDBUser = Cells(3, 14)
TargetDBPassword = Cells(4, 14)
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.Open TargetDB, TargetDBUser, TargetDBPassword
ThisWorkbook.Sheets("Summary").Select
ActiveWorkbook.Sheets("Summary").Range("A7:A20000").Delete
ThisWorkbook.Sheets("INPUT").Select
ActiveWorkbook.Sheets("INPUT").Select
RowCounter = Application.WorksheetFunction.CountA(Range("A2:A20"))
CountAllExcellRecords = 0
CountSuccessRecords = 0
RowInSourceSheet = 1
Do While RowInSourceSheet <= RowCounter
SourceSheet = ThisWorkbook.Sheets("INPUT").Range("B1").Offset(RowInSourceSheet, 0)
Worksheets(SourceSheet).Activate
RowCountSourceSheet = Application.WorksheetFunction.CountA(Range("A:A"))
CriteriaBuilder = "('"
ColumnCountSourceSheet = ThisWorkbook.Sheets(SourceSheet).UsedRange.Rows(1).Columns.Count
SourceSheetRow = 1
Do While SourceSheetRow < RowCountSourceSheet
QvAriable = " WHERE"
QtAble = ""
SourceSheetColumn = 0
Do While SourceSheetColumn < ColumnCountSourceSheet
qvAriablen = ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
qvAriablem = ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(SourceSheetRow, SourceSheetColumn)
Worksheets(SourceSheet).Activate
Cells.Range("A1").Offset(SourceSheetRow, SourceSheetColumn).Activate
Application.Volatile
Select Case True
Case IsEmpty(ActiveCell): CellType = "Blank"
If qvAriablem = "" Then toSQL = " Null "
qvAriablem = toSQL
QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & ""
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
Case Application.IsText(ActiveCell): DataType = "Text"
QvAriable = QvAriable & " " & qvAriablen & " = '" & qvAriablem & "'"
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
Case Application.IsLogical(ActiveCell): CellType = "Logical"
QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " "
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
Case Application.IsErr(ActiveCell): CellType = "Error"
QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " "
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
Case IsDate(ActiveCell): DataType = "Date"
qvAriablem = Format(qvAriablem, "MM/DD/YYYY")
QvAriable = QvAriable & " " & qvAriablen & " = '" & Format(qvAriablem, "MM/DD/YYYY") & "'"
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
Case InStr(1, ActiveCell.Text, ":") <> 0: CellType = "Time"
qvAriablem = Format(qvAriablem, "MM/DD/YYYY")
QvAriable = QvAriable & " " & qvAriablen & " = '" & Format(qvAriablem, "MM/DD/YYYY") & "'"
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
Case IsNumeric(ActiveCell): CellType = "Value"
Worksheets("INPUT").Activate
RowCountExceptions = Application.WorksheetFunction.CountA(Range("I:I"))
Exceptions = 2
eXceptionCount = 0
Do While Exceptions <= RowCountExceptions
If qvAriablen = Cells(Exceptions, 9) Then
eXceptionCount = eXceptionCount + 1
QvAriable = QvAriable & " " & qvAriablen & " = '" & qvAriablem & "'"
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
End If
Exceptions = Exceptions + 1
Loop
If eXceptionCount = 0 Then
QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " "
QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
End If
End Select
If SourceSheetColumn < ColumnCountSourceSheet - 1 Then
QvAriable = QvAriable & " AND "
QtAble = QtAble & ", "
End If
SourceSheetColumn = SourceSheetColumn + 1
Loop
rst.Open "SELECT * FROM " & SourceSheet & QvAriable, cnn
'rst.Open "SELECT * FROM UNIT_FUND WHERE CURRENCY_CODE = 'GBP' AND UNIT_FUND_CODE = '150' AND FUND_NAME = 'MetLife Fidelity Cash Fund' AND EFFECTIVE_DATE = '10/21/2009' AND CEASE_DATE =" & "NULL"
Sheets("Summary").Range("G2").CopyFromRecordset rst
CountAllExcellRecords = CountAllExcellRecords + 1
If rst.RecordCount > 0 Then
CountSuccessRecords = CountSuccessRecords + 1
Else
ThisWorkbook.Sheets("Summary").Select
ActiveWorkbook.Sheets("Summary").Select
a = "SELECT * FROM " & SourceSheet & QvAriable
NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
Cells(NextRow, 1) = a
End If
rst.Close
SourceSheetRow = SourceSheetRow + 1
Loop
RowInSourceSheet = RowInSourceSheet + 1
Loop
ThisWorkbook.Sheets("Summary").Select
Cells(3, 3) = CountAllExcellRecords
Cells(4, 3) = CountSuccessRecords
Cells(5, 3) = CountSuccessRecords - CountAllExcellRecords
End Sub
The RecordCount property depends on the cursor location and type. It appears that all you are doing is checking to see if you got any records, so you can just use:
If Not rst.EOF Then
instead of:
If rst.RecordCount <> 0 Then