2017-10-11 61 views
0

我正在浏览YouTube视频,以了解ASP.NET。我已经得到它的大部分工作,有一个主要的警告:我不能从隐藏的领域ID退休的价值。因此,我没有价值发送到存储过程来创建或更新。无法获取ASP.NET隐藏字段的值

注释掉的是原文。当我有那么执行 .ExecuteNonQuery,我得到以下错误:

"Procedure or function 'ResourceCreateOrUpdate' expects parameter '@ResourceID', which was not supplied.".

当我尝试显示hfResourceID,我没有试图通过0时,对于创建,或资源ID值,即1但是,这个价值并没有达到目标。我知道存储过程的工作原理,因为我可以在SQL Server Management中执行它。

我试着将hfResourceID移动到一个字符串,然后是一个整数值,但我似乎遇到了创建if/else的问题:一切都被标记为错误。当我将鼠标悬停在线条,我得到以下信息,这几乎离开我无言以对:

"Embedded statement cannot be a declaration or labeled statement".

我是否能够得到关于如何清理我的错误任何指针吗?谢谢。

2017年10月13日10:38 @:代码更新

<asp:HiddenField ID="hfResourceID" runat="server" /> 

    protected void btnSave_Click(object sender, EventArgs e) 
    { 
     int intResourceID = 0; 
     bool boolIDHasValue = true; 
     try 
     { 
      intResourceID = Convert.ToInt32(hfResourceID.Value); 
     } 
     catch (Exception ex) 
     { 
      lblErrorMessage.Text = ex.Message; 
      boolIDHasValue = false; 
     } 
     if (boolIDHasValue) 
     { 
      if (sqlconnODRConnection.State == System.Data.ConnectionState.Closed) 
       sqlconnODRConnection.Open(); 
      SqlCommand sqlcmdCreateOrUpdate = new SqlCommand("ResourceCreateOrUpdate", sqlconnODRConnection); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@ResourceID", intResourceID); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@Status", txtStatus.Text.Trim()); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim()); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@MiddleName", txtMiddleName.Text.Trim()); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim()); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@NickName", txtNickName.Text.Trim()); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@Gender", txtGender.Text.Trim()); 
      sqlcmdCreateOrUpdate.Parameters.AddWithValue("@USCitizen", txtUSCitizen.Text.Trim()); 
      sqlcmdCreateOrUpdate.ExecuteNonQuery(); 
      sqlconnODRConnection.Close(); 
      string strResourceID = hfResourceID.Value; 
      Clear(); 
      if (strResourceID == "") 
       lblSuccessMessage.Text = "Saved Successfully"; 
      else 
       lblSuccessMessage.Text = "Updated Successfully"; 
      FillGridView(); 
     } 

    } 
+0

是什么类型的字段?也许尝试hfResourceID.Text而不是.Value,然后将字符串值解析为int或任何类型。 – akerra

+0

那么,你没有提供一个叫做“@ResourceID”的参数。根据错误,存储过程期望该参数。看起来你试图在某个时候提供这个参数,但现在它被注释掉了。注释中的代码不会执行。 – David

+0

此外,“嵌入语句...”错误引用您的if/else块。你在每个块中做的* only *事情是声明一个变量,没有别的。编译器不允许这样做。没有目的,变量会立即超出范围。 – David

回答

-1

就以ASP代码来看看,具体的方式要设置隐藏字段的值。我想它被正确地标记为runat = server,但是可能在asp.net代码中出现了一些问题,请尝试使用console.log函数调试客户端代码,并在控制台浏览器中查看输出。

+0

我在上面的隐藏字段中包含了asp语句。 –

+0

我会阅读如何使用控制台。 –

+0

我试着添加System.Diagnostics.Debug.Writeline(),但是我从未收到任何东西,所以我想我错误地使用它。有没有办法看到sqlcndCreateOrUpdate.ExecuteNonQuery()中的命令? –

0

您对嵌入语句错误是因为你一个声明

if (strResourceID == "") 
     int intResourceID = 0; 
    else 
     int intResourceID = (Convert.ToInt32(hfResourceID.Value)); 

当如果后直接decalring一个变量,否则,那么你需要你的大括号。所以......

if (strResourceID == "") 
{  
    int intResourceID = 0; 
} 
else 
{ 
    int intResourceID = (Convert.ToInt32(hfResourceID.Value)); 
} 

至于我需要看到你的客户端代码等问题。

+0

谢谢你指出。还有另一个If/else语句没有他们,他们也没有得到错误。我会在谨慎的方面犯错,并养成使用大括号的习惯。 –

+0

这是因为你在if(你的'int')后面的下一行做了一个变量的声明。我也省略了大括号,但只有当语句在'if'之后需要一行代码时,它才不是新的变量声明。 :) –

+0

这可能是最好的了解什么范围是什么时候用C#编程,它会清除你的东西 –

1

您从该视频中复制的代码存在一些问题。但这里有一个关于它应该如何完成的片段。我已经添加了3种方法将HiddenField值转换为实际的int。您使用哪一个可以取决于您想要如何处理错误,0值等。不包括在片段中,但我喜欢在使用Trim()时检查IsNullOrEmpty,以排除可能使值不可转换的空间if (!string.IsNullOrEmpty(hfResourceID.Value.Trim()))

int intResourceID = 0; 

//this will try to convert but you won't see exeptions when failed 
Int32.TryParse(hfResourceID.Value, out intResourceID); 

//checks if there is a value in the hiddenfield, but throws yellow screen if not convertible 
if (!string.IsNullOrEmpty(hfResourceID.Value)) 
{ 
    intResourceID = Convert.ToInt32(hfResourceID.Value); 
} 

//catch an error when the value is not convertible, can be wrapped with !string.IsNullOrEmpty(hfResourceID.Value) 
try 
{ 
    intResourceID = Convert.ToInt32(hfResourceID.Value); 
} 
catch (Exception ex) 
{ 
    //handle the error, can be seen with ex.Message 
} 

//if the hidden value is still 0 (for whatever reason) you might not want to execute the query 
//so the next part will return and stop executing the rest of the code 
if (intResourceID == 0) 
{ 
    return; 
} 

//update the database, using 'using' will ensure proper closure of the connection and disposing of any objects 
using (SqlConnection connection = new SqlConnection("myConnectionString")) 
using (SqlCommand command = new SqlCommand("ResourceCreateOrUpdate", connection)) 
{ 
    //set the command type and add the parameters 
    command.CommandType = CommandType.StoredProcedure; 
    command.Parameters.Add("@ResourceID", SqlDbType.Int).Value = intResourceID; 

    try 
    { 
     //open the database connection and execute the command 
     connection.Open(); 
     command.ExecuteNonQuery(); 
    } 
    catch (Exception ex) 
    { 
     //there was an error opening the database connection or with the command, can be viewed with ex.Message 
    } 
} 
+0

这至少检查它是否为空。 – Valkyrie

+0

我试过了。我甚至把我的代码放入一个布尔值true,所以现在我确信我有一个有效的值。 –