Connecting Tech Pros Worldwide Forums | Help | Site Map

Loop problem

Newbie
 
Join Date: Aug 2006
Posts: 3
#1: Apr 23 '07
i am new to asp....

i am facing a problem in for loop
what i want is, if the record does not exist loop should execute insert query else it should go to update query ..
while i try to execute the code...if the record is not there it will insert all records after that it goes to the update query also ...and then it will exit from the loop........


for x = 1 to 4
prev = "prev" & x
codeva = "code" & x
curva= "cur" & x
curvalue=trim(Request.Form(curva))
prevvalue= trim(Request.Form(prev))
code=trim(Request.Form(codeva))
if prevvalue= "" then
prevvalue = 0
end if
if curvalue= "" then
curvalue = 0
end if
checksql2="select * from LeaveOpeningBalance where EMPNO = '"& getEmpNum &"' and " & _
"leave_code='"& code &"' and record_active='Y'"
Response.Write "<BR>" & checksql2
set rs7 = conn1.execute(checksql2)
Response.Write("<Br> " & rs7.eof)
if rs7.eof = true then

Response.Write ("<br /> INSERTING DATA --" & sqlUP1)


else

'*************UPDATING DATA**********************
Response.Write ("<br /> UPDATING DATA --" & sqlUP1)

end if


next

can any one help me in this

simi
Familiar Sight
 
Join Date: Feb 2007
Posts: 135
#2: Apr 23 '07

re: Loop problem


hi simi,

Expand|Select|Wrap|Line Numbers
  1. for x = 1 to 4
  2. prev = "prev" & x
  3. codeva = "code" & x
  4. curva= "cur" & x
  5. curvalue=trim(Request.Form(curva))
  6. prevvalue= trim(Request.Form(prev))
  7. code=trim(Request.Form(codeva))
  8. if prevvalue= "" then
  9. prevvalue = 0
  10. end if
  11. if curvalue= "" then
  12. curvalue = 0
  13. end if
  14. checksql2="select * from LeaveOpeningBalance where EMPNO = '"& getEmpNum &"' and " & _
  15. "leave_code='"& code &"' and record_active='Y'"
  16. Response.Write "<BR>" & checksql2
  17. set rs7 = conn1.execute(checksql2)
  18. Response.Write("<Br> " & rs7.eof)
  19. if rs7.eof = true then
  20.  
  21. Response.Write ("<br /> INSERTING DATA --" & sqlUP1)
  22.  
  23.  
  24. else
  25.  
  26. '*************UPDATING DATA**********************
  27. Response.Write ("<br /> UPDATING DATA --" & sqlUP1)
  28.  
  29. end if
  30.  
  31.  
  32. next
the code is not the complete code. atleast u should provide some sort of psudo code so that we can understand where the actual problem lies.

susen
Newbie
 
Join Date: Aug 2006
Posts: 3
#3: Apr 24 '07

re: Loop problem


i have 2 files ...one for entering values in input box (eg: value.asp) and another for saving the values (eg: save.asp)
after submitting value.asp ,i pass values to save.asp....

what i want is while i execute the for loop if there is NO records it should insert the new value 'and if there is record it should run update query...

but now what is happening is, if there is no record it will insert all the new record ,after that, it goes to else condition loop and run update query also.
ie.instead of executing only the insert query, it will execute update query also..




<body>

<form name="confstaffleaveSave" method="post">
<input type="text" name ="emp_num" value ="<%=login%>">
<%

if true then
session.Contents("secondtime")=login

dim TodaysDate
sql3="select to_char(sysdate,'DD-Mon-YY') from dual"
set rs1=conn2.execute(sql3)
TodaysDate=rs1(0).value
' Response.Write ("<br>" + TodaysDate)
dim getReason,getLeaveCode,getHolidayCode,getEmpNum,ge tLeaveName,getApplDate,getJoinDate1,leavearray,get holiday
dim getLeaveYear_From,getLeaveYear_To,getLeave_Code,ge tHOLI_CODE,getValid_to_date,getOpening_Balance,get Current_Credit,getNotHoli,Not_holi_count
dim getLeave_Taken,getAdded_to_AL,getActive,getappcode ,prev,codeva,curva,curvalue,prevvalue,checksql2,ge tLastHoli,last_holi_count
session.Contents("loginempno")=loginuser

'*************************PREVIOUS YEAR LEAVE DETAILS ***********************
getEmpnum=trim(Request.Form("emplist"))
getappcode=Request.Form("appcode")
getLeaveYear_From ="01-Jul-06"
getLeaveYear_To ="30-Jun-07"
'**********************ENTERING LEAVE DETAILS*******************

for i = 1 to 4
prev = "prev" & i ' input box name
codeva = "code" & i ' input box name
curva= "cur" & i ' input box name curvalue=trim(Request.Form(curva))'*****CURRENT leave year credit
prevvalue= trim(Request.Form(prev))'**** leave taken last year
code=trim(Request.Form(codeva)) '*****leave code

if prevvalue= "" then
prevvalue = 0
end if
if curvalue= "" then
curvalue = 0
end if
Response.Write ("<br />Loop---"& i &"<br />")

'CHECKING IF ANY OF THE LEAVE SELECTED EXISTS FOR THAT PARTICULAR EMPLOYEE

checksql2="select * from LeaveOpeningBalance where EMPNO = '"& getEmpNum &"' and " & _
"leave_code='"& code &"' and record_active='Y'"

set rs7=conn1.execute(checksql2)

if rs7.eof=true then '***********NO RECORDS

'*************INSERTING NEW LEAVE DATA **********************

sql_insert1 = "insert into LeaveOpeningBalance(empno,LeaveYear_From,LeaveYear _To," & _
" Leave_Code,Valid_to_date,Opening_Balance,Current_C redit,Leave_Taken,Added_to_AL, " & _
" Active,CREATED_ON,CREATED_BY,LAST_MODIFYON,LAST_MO DIFYBY,RECORD_ACTIVE ) values " & _
" ('"& getEmpNum &"',to_date('"& getLeaveYear_From &"','DD-Mon-YY'), " & _
" to_date('"& getLeaveYear_To &"','DD-Mon-YY'),'"& code &"'," & _
" '','"& prevvalue &"','"& curvalue &"', " & _
" '0','N','Y','"& TodaysDate &"','"& loginuser &"','"& TodaysDate &"','"& loginuser &"','Y')"
Response.Write ("<br /> INSERTING DATA --" & sql_insert1)
conn1.execute(sql_insert1)


else ' RECORD EXISTS


'*************UPDATING DATA**********************


sqlUP1 = "update LeaveOpeningBalance set OPENING_BALANCE='"& prevvalue &"', " & _
" LAST_MODIFYON='"& TodaysDate &"',LAST_MODIFYBY='"& loginuser &"' " & _
" where EMPNO = '"& getEmpNum &"' and Leave_CODE='"& code &"' " & _
" and LeaveYear_From =to_date('"& getLeaveYear_From &"','DD-Mon-YY') " & _
" and LeaveYear_To=to_date('"& getLeaveYear_To &"','DD-Mon-YY') and record_Active='Y' "
conn1.execute(sqlUP1)
Response.Write ("<br /> UPDATING DATA --" & sqlUP1)


end if

next

conn1.close
conn2.close
%>

<table width="75%" border="0" align="center" cellspacing="0" bordercolor="#336699">
<tr>
<td><div align="center">Thank You for submitting your leave details of previous year and current year..</div></td>
</tr>
<Tr><td align="center"><br><input type="button" value="Back" id=button1 name=ContBtn onClick="javascript:gotoMain();"></td></tr>
</table>
</form>
</body>
Familiar Sight
 
Join Date: Feb 2007
Posts: 135
#4: Apr 24 '07

re: Loop problem


Hi simi,
I have checked ur code. It looks like proper. but one thing I am not getting clearly. why r u running the check, insert or update within a loop for 4 times?

It seems to me that due to this loop if the record for a particular empno does not exists then it inserts the record and then for next 3 times its updating the same record. getEmpnum=trim(Request.Form("emplist")) as this variable is having same value so within the loop its inserting the record for first time and then updating the record for next 3 times.

susen
Newbie
 
Join Date: Aug 2006
Posts: 3
#5: Apr 25 '07

re: Loop problem


i have 4 input boxes with the name s
prev1,prev2,prev3,prev4 ..these are values for 4 different type of leaves.

actually i am passing 4 values(ie. 4 different leave types) for same employee.

for each type of leave he entered,there should be one record.
so, before executing the query
first il take leavetype prev1 value and check if there is any record for the employee for that particular leave type.....
if there is no record i will insert new one
if there is record i will just update it...

next i go for the next leavetype prev2 value ...and so on...


prev = "prev" & i '********input box name for leave type
prevvalue= trim(Request.Form(prev)) '****previous year balance

simi

Quote:

Originally Posted by devsusen

Hi simi,
I have checked ur code. It looks like proper. but one thing I am not getting clearly. why r u running the check, insert or update within a loop for 4 times?

It seems to me that due to this loop if the record for a particular empno does not exists then it inserts the record and then for next 3 times its updating the same record. getEmpnum=trim(Request.Form("emplist")) as this variable is having same value so within the loop its inserting the record for first time and then updating the record for next 3 times.

susen

Familiar Sight
 
Join Date: Feb 2007
Posts: 135
#6: Apr 27 '07

re: Loop problem


ok, I got ur point. Now try to print the checksql2 query if its generated correctly or not.

Expand|Select|Wrap|Line Numbers
  1. checksql2="select * from LeaveOpeningBalance where EMPNO = '"& getEmpNum &"' and leave_code='"& code &"' and record_active='Y'"
in the query u r checking for prev1, prev2,.... but for code.

susen
Reply


Similar ASP / Active Server Pages bytes