2017-10-11 53 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,所以現在我確信我有一個有效的值。 –