简体   繁体   中英

What wrong with my code? Why couldn't I assign “0” to data that being retrieved from database

Since in database there are some records that have Null value of them, this code was being created to filter the recordset that have no value assigned in "rsActQty" or "rsSumQty". However no matter what I tried, it seemed that this code cannot detect Null value. And I don't know what did I done wrong. So could you please give me some suggestion? Thank you. (This db was in MSACCESS)

What I want is : - To display only records that have value in either "rsActQty" OR "rsSumQty". - When one of this fields was NULL assign "0" to it.

<%
if Rs.eof then 
    response.write "<tr><td colspan=""6"">&nbsp;"
    call displayNotFoundRecord
    response.write "</td></tr>"
Else


Dim ActQty, sumQty, Total, rsActQty, rsSumQty, rsPdtn_qty_est, inv_date, res_date
    Do while Rs.AbsolutePage = strPageCurrent And Not Rs.EOF

        inv_id = rs.fields.item("inv_idRS")
        rsActQty = rs.fields.item("sumOfinv_qty_act")
        rsSumQty = rs.fields.item("SumOfres_qty")
        rsPdtn_qty_est = rs.fields.item("SumOfpdtn_qty_est")
        inv_date = rs.fields.item("inv_dateRS")
        res_date = rs.fields.item("res_dateRS")

            if rsActQty = "" then 
                ActQty = 0
            else
                ActQty = rsActQty
            end if

            if rsSumQty = "" then
                sumQty = 0
            else
                sumQty = rsSumQty
            end if 


        if rsActQty <> "" OR rsSumQty <> "" then

                if inv_date = "" then
                    dateshow = res_date
                else
                    dateshow =  inv_date
                end if

                if res_date = "" then
                    dateshow = res_date
                else
                    dateshow = res_date
                end if

                total = rs.fields.item("Total")
%>

<tr class='difcursor'> 
<td class="btline" width="25" align="center"><input type="checkbox" name="inv_id" value="<%=inv_id%>" onClick="highlightRow(this,'#FFFFCC','#EFF4FA');"></td>
<td class="btline" <%=genLink(inv_id)%> nowrap style="padding-right: 10px">&nbsp;<%=rs.fields.item("pd_id")%> </td>
<td class="btline" <%=genLink(inv_id)%> nowrap style="padding-right: 10px">&nbsp;<%=rs.fields.item("pd_name")%></td>
<td class="btline" <%=genLink(inv_id)%> nowrap style="padding-right: 10px">&nbsp;<%=PcsToDz(ActQty)%></td>
<td class="btline" <%=genLink(inv_id)%> nowrap style="padding-right: 10px">&nbsp;<%'=PcsToDz(rs.fields.item("SumOfpdtn_qty_est"))%></td>
<td class="btline" <%=genLink(inv_id)%> nowrap style="padding-right: 10px">&nbsp;<%=dateshow%></td>
<td class="btline" <%=genLink(inv_id)%> nowrap style="padding-right: 10px">&nbsp;<%=PcsToDz(sumQty)%></td>
<td class="btline" <%=genLink(inv_id)%> nowrap style="padding-right: 10px">&nbsp;<%=PcsTODz(Total)%></td>
<td class="btline"></td>
</tr>

<%
        end if

        Rs.movenext
    Loop
End if
Rs.close
set Rs=nothing
Call DBConnClose()
%>

Try using the isNull function. For instance

if not isNull(rs.fields.item("sumOfinv_qty_act")) then 
    ActQty = rs.fields.item("sumOfinv_qty_act")
else
    ActQty = 0
end if

Null is not same as blank value.

You could try forcing the null to a string and do the comparing:

    rsActQty = "" & rs.fields.item("sumOfinv_qty_act")
    rsSumQty = "" & rs.fields.item("SumOfres_qty")

you could also handle null in your sql query like:

select iif(isnull(fieldname),0,fieldname) as fieldname from table

which will return a 0 for the null value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM