为什么我的布尔值没有被赋予正确的值?

时间:2017-03-01 16:59:04

标签: asp.net vb.net html-select

我有这个ASP.Net代码:

<select name="subcategory" color="<%=Session("TextColor")%>" style="font: 8pt arial" onchange="UpdateFlag=true;">
    <% if not IsNewBusiness then %>
      <option value="0">Existing
      <option value="1">Organic Growth
    <% else %>
      <option value="0">New
      <option value="1">Assumed
    <% end if %>
</select>

...应该根据布尔值IsNewBusiness的值,将一对项中的一个或另一个加载到html select元素。

但是,相同的逻辑总是等于true(“不是IsNewBusiness”),前两个项目(“Existing”和“Organic Growth”)总是填充select元素。

分配值的代码是:

Dim IsNewBusiness As Boolean
. . .
currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
End If

正如下面查询结果中的“NewBiz”列所示,在某些情况下,该值应为true(在NewBiz不为0的情况下为真):

enter image description here

但即使我选择了我网站中的“-1”单位之一,我仍然会在select元素中获得“Existing”和“Organic Growth”,而不是“New”和“Assumed”。

我的逻辑有问题吗?

更新

到达查询代码 。我知道,因为我在那里放了一行伪代码:

Wscript.Echo "Like this?" ' bogus
IsNewBusiness = TRUE 'default (if record not found)

......当我跑的时候,我得到了:

"Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30451: Name 'Wscript' is not declared.

Source Error:



Line 127:        SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
Line 128:        adoRS.Open(SQLString, adoCon)
Line 129:        Wscript.Echo "Like this?"
Line 130:       IsNewBusiness = TRUE 'default (if record not found)
Line 131:        If Not adoRS.EOF Then     

Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 129 

更新2

由于逻辑总是等于假,我必须假设这一行:

IsNewBusiness = adoRS.Fields.Item(0).Value <> 0

...正在到达,并且adoRS.Fields.Item(0).Value始终为0

所以IsNewBusiness从一开始就设置为TRUE,但永远不会成立,尽管在某些情况下应该这样。

更新3

现在这里有一些非常奇怪的东西 - 当我把一年的作业改为一个明显虚假的作品时:

currentYear = 2525 'Year(Now)

(表中没有该年份的参赛作品)

...我仍然得到相同的结果 - “现有”和“有机增长”是填充select元素的两个值。

更新4

当我添加JonP推荐的Response.Writes时,它看起来很好用html:

enter image description here

...但在VBScript中似乎并不需要:

enter image description here

事实上,当我运行它时,我明白了:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30800: Method arguments must be enclosed in parentheses.

Source Error:



Line 127:        SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
Line 128:        adoRS.Open(SQLString, adoCon)
Line 129:        Response.Write "<!-- Is New Biz = " . IsNewBusiness . "-->"    
Line 130:        IsNewBusiness = TRUE 'default (if record not found)
Line 131:        If Not adoRS.EOF Then


Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 129 

至于在html中添加的那个,我可以在View Source中看到它:

Response.Write "<!-- Is New Biz = " . IsNewBusiness . "-->" 

......但这对我没什么好处。该值不在控制台或我能看到的任何其他地方......

更新5

Jon P:当我尝试这样的时候,就像这样:

<% Response.Write "<!-- Is New Biz before select elements assigned = " . IsNewBusiness . "-->" %>

... IsNewBusiness ,在此声明(位于文件顶部):

<script language="VB" runat="Server">
. . .
Dim IsNewBusiness As Boolean

...是红色的(表示它被编译器视为外来抗体),我得到运行时错误:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30800: Method arguments must be enclosed in parentheses.

Source Error:



Line 722:                       </td>
Line 723:                   </tr>
Line 724:                        <% Response.Write "<!-- Is New Biz before select elements assigned = " . IsNewBusiness . "-->" %>
Line 725:                        <tr>
Line 726:                            <td nowrap align="left" valign="top">


Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 724 

更新6

推荐使用语法调整:

<% Response.Write("<!-- Is New Biz = " . IsNewBusiness . "-->") %>

......它仍然失败:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30456: 'IsNewBusiness' is not a member of 'String'.

Source Error:

Line 723:                   </tr>
Line 724:                        <%--<% Response.Write "<!-- Is New Biz before select elements assigned = " . IsNewBusiness . "-->" %>--%>
Line 725:                        <% Response.Write("<!-- Is New Biz = " . IsNewBusiness . "-->") %>
Line 726:                        <tr>
Line 727:                            <td nowrap align="left" valign="top">

...可能因为“IsNewBusiness”是布尔值,需要转换为字符串吗?

更新7

我试过了:

<% Response.Write("<!-- Is New Biz = " . CStr(IsNewBusiness) . "-->") %>

......并得到了,“ BC30456:'CStr'不是'String'的成员。

我也尝试过:

<% Response.Write("<!-- Is New Biz = " . Convert.ToString(IsNewBusiness) . "-->") %>

......并得到了,“ BC30456:'转换'不是'字符串'的成员。

更新8

即使我把它放在一个我认识的地方也可以到达:

<%
Response.Write("<!-- Is New Biz = " & CStr(IsNewBusiness) & "-->")
Unit = Request.QueryString.Item("Unit")
. . .

......我什么也没看到。我不确定到底会发生什么 - 一个“警报”?我点击了F12,控制台里什么也没有......

1 个答案:

答案 0 :(得分:3)

一旦我使用MCND的“debug msg”代码并将数据库代码移到“Save Action”之上,它就可以了。这是在一个小背景下:

<%
. . .   
'determine whether this unit is a new business
currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS = New ADODB.Recordset
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 
    Response.Write("<!-- IsNewBusiness after NOT EOF = " & CStr(IsNewBusiness) & "-->")
End If
adoRS.Close()

If Request.Form.Item("Action") = "Save" Then
. . .