我有这个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的情况下为true):
但是,即使我在网站中选择“ -1”单位之一,仍会在选择元素中看到“现有”和“有机增长”,而不是“新建”和“假设”。
我的逻辑有问题吗?
更新
查询代码已到达。我知道,因为我在那儿放了一行伪代码:
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
由于逻辑总是等于false,因此我必须假设这一行:
IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
...已达到,并且adoRS.Fields.Item(0).Value始终为0
因此,IsNewBusiness首先会设置为TRUE,但永远不会保持true,尽管在某些情况下应该如此。
更新3
现在这真的很奇怪-当我将年份的分配更改为明显的伪造时:
currentYear = 2525 'Year(Now)
(表中没有该年份的条目)
...我仍然得到相同的结果-“现有”和“有机增长”是填充选择元素的两个值。
更新4
当我添加JonP推荐的Response.Writes时,通过html看起来还不错:
...但是在VBScript中似乎并不需要:
实际上,当我运行它时,我得到以下信息:
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
乔恩·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键,控制台中什么也没有。
最佳答案
一旦我使用了来自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
. . .