Existing Or-6ren">
gpt4 book ai didi

asp.net - 为什么我的 bool 值没有被分配正确的值?

转载 作者:行者123 更新时间:2023-12-01 00:29:09 27 4
gpt4 key购买 nike

我有这个 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>

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

但是,相同的逻辑始终等于 true(“not 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”列所示,在某些情况下,该值应该为真(NewBiz 不为 0 时为真):

enter image description here

但即使我在我的网站中选择了“-1”个单位之一,我仍然在选择元素中得到“现有”和“有机增长”,而不是"new"和“假设”。

我的逻辑有问题吗?

更新

正在访问查询代码。我知道这是因为我在那里放了一行假代码:
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)

(表中没有当年的条目)

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

更新 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中添加的那个,我可以在查看源代码中看到:
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”是一个 bool 值,需要转换为字符串?

更新 7

我试过这个:
<% Response.Write("<!-- Is New Biz = " . CStr(IsNewBusiness) . "-->") %>

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

我也试过:
<% Response.Write("<!-- Is New Biz = " . Convert.ToString(IsNewBusiness) . "-->") %>

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

更新 8

即使我把它放在一个我知道可以到达的地方:
<%
Response.Write("<!-- Is New Biz = " & CStr(IsNewBusiness) & "-->")
Unit = Request.QueryString.Item("Unit")
. . .

...我什么也没看见。我不确定会发生什么 - “警报”?我按F12,控制台中没有任何内容......

最佳答案

一旦我使用了来自 MCND 的“调试消息”代码并将数据库代码移动到“保存操作”上方,它就可以正常工作。这是在一个小的上下文中:

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

关于asp.net - 为什么我的 bool 值没有被分配正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com