为什么没有为我的布尔值分配正确的值? [英] Why is my Boolean not being assigned the correct value?

查看:75
本文介绍了为什么没有为我的布尔值分配正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个ASP.Net代码:

I've got this ASP.Net code:

<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元素中.

...which should, based on the value of the Boolean IsNewBusiness, load one or the other of a pair of items to the html select element.

但是,相同的逻辑总是等于true(不是IsNewBusiness"),而前两个项目("Existing"和"Organic Growth")始终是填充select元素的项目.

However, the same logic always equates to true ("not IsNewBusiness"), and the first two items ("Existing" and "Organic Growth") are always the ones that populate the select element.

分配值的代码为:

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的情况下为true):

As the "NewBiz" column in the query results below show, there are some cases where the value should be true (it's true where NewBiz is not 0):

但是,即使我在网站中选择"-1"单位之一,在选择元素中仍会获得现有"和有机增长",而不是新建"和假定".

But even when I choose one of the "-1" Units in my site, I still get "Existing" and "Organic Growth" in the select element, not "New" and "Assumed".

我的逻辑有问题吗?

已到达查询代码 .我知道是因为我在那儿放了一行伪代码:

The query code is being reached. I know that because I put a line of bogus code there:

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

...当我运行它时,我得到了:

...and when I ran it, I got:

"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

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

UPDATE 2

Since the logic is always equating to false, I have to assume that this line:

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

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

...is being reached, and that adoRS.Fields.Item(0).Value is always 0

因此,IsNewBusiness首先设置为TRUE,但永远不会保持为true,尽管在某些情况下应该如此.

So IsNewBusiness is set to TRUE to start with, but never remains true, although it should on some occasions.

现在这真的很奇怪-当我将年份的分配更改为明显的伪造时:

Now here's something really bizarre - when I change the assignment to the year to a manifestly bogus one:

currentYear = 2525 'Year(Now)

(表中没有该年份的条目)

(there are no entries for that year in the table)

...我仍然得到相同的结果-现有"和有机增长"是填充选择元素的两个值.

...I still get the same results - "Existing" and "Organic Growth" are the two values that fill the select element.

当我添加JonP推荐的Response.Writes时,通过html看起来还不错:

When I add the Response.Writes recommended by JonP, it looks fine by the html:

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

...but doesn't seem to be wanted when in the VBScript:

实际上,当我运行它时,我得到了:

In fact, when I run it, I get this:

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中看到它:

As for the one added amidst the html, I can see it in View Source:

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

...但是那对我没有多大帮助.该值不在控制台中或我可以看到的其他任何地方...

...but that doesn't do me much good. The value is not in the console or anywhere else that I can see...

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

Jon P: When I try that, like so:

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

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

...IsNewBusiness, which is declared here (at the top of the file):

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

...是红色的(表明编译器将其视为外来抗体),并且出现运行时错误:

...is red (indicating that it is viewed by the compiler as a foreign antibody), and I get the runtime error:

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

建议对语法进行调整:

UPDATE 6

With the syntax tweak recommended:

<% 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"是布尔值,需要转换为字符串吗?

...possibly because "IsNewBusiness" is a Boolean, and needs to be converted to a string?

我尝试过:

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

...然后得到" BC30456:'CStr'不是'String'的成员."

...and got, "BC30456: 'CStr' is not a member of 'String'."

我也尝试过:

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

...然后得到," BC30456:'Convert'不是'String'的成员."

...and got, "BC30456: 'Convert' is not a member of 'String'."

即使我把它放在一个我知道可以到达的地方:

Even when I put it in a place I know is getting reached:

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

...我什么也没看到.我不确定究竟会发生什么-警报"?我按F12键,控制台中什么也没有...

...I see nothing. I'm not sure exactly what to expect - an "alert"? I hit F12, and there is nothing in the Console...

推荐答案

一旦我使用了来自MCND的"debug msg"代码并将数据库代码移至"Save Action"上方,它就可以正常工作.这里是在一个小上下文中:

Once I used the "debug msg" code from MCND and moved the database code above the "Save Action" it works just fine. Here it is in a little context:

<%
. . .   
'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
. . .

这篇关于为什么没有为我的布尔值分配正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆