0

我有一个 asp/razor 网页,我用它来填充镜头数据及其选项。

如果存在,我想返回一个名为 SID 的值,如果不存在则为空白。

我正在使用 if 语句执行此操作,但浏览器报告错误:


Server Error in '/' 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: CS0103: The name 'SID' does not exist in the current context

Source Error:

Line 238:            <div class="row">
Line 239:                <span class="label"><label for="salesid">SalesID:</label></span>
Line 240:                <input type="text" name="salesid" id="salesid" value=@SID size="15" />
Line 241:            </div>}
Line 242:            else {@message;} 

Source File: ~\DiscountCustomers.cshtml    Line: 240 

--------------------------------------------------------------------------------
@{
   Layout = "~/_SiteLayout.cshtml"; 

   var db = Database.Open("A-LensCatFE-01SQL") ; 

   var CustomerCode = UrlData[0];

   var message = "";

   // vars for dropdowns:
   var ListSRP =db.Query("Select * FROM lstSRPBasedOn");
   var LensName = db.Query("Select Form, ShortName FROM dbo.qryLAStyleMin ORDER BY Form");

   // vars for modal forms insert data:
   var query = ("Select * from dbo.qryCustomersAll Where CustomerCode= @0");
   var data = db.QuerySingle(query, CustomerCode);
   var emcid = data.CID;

   var query1 = ("Select CatID from dbo.qryCustomersAll Where CustomerCode= @0");
   var catid = db.QueryValue(query1, CustomerCode);

   var query3 =("Select * from dbo.qryPromo2 Where CustomerCode=@0");
   var data3 = db.QuerySingle(query3, CustomerCode);

   if (data3 != null)
               {var SID = data3.SalesID;}
               else 
               { message="Data not found";} 

  }
---------------------------------------------------------------
 @if (data3 != null){
            <div class="row">
                <span class="label"><label for="salesid">SalesID:</label></span>
                <input type="text" name="salesid" id="salesid" value=@SID size="15" />
            </div>}
            else {@message;}
---------------------------------------------------------------

我究竟做错了什么?我怎样才能解决这个问题?谢谢你。

4

1 回答 1

0

SIDif语句中声明,因此它不在范围内。将声明移到if语句之外。

(假设 SID 是int

int SID = 0;
if (data3 != null)
    {SID = data3.SalesID;}
else 
    { message="Data not found";}
于 2013-05-22T14:35:53.287 回答