2015-06-08 33 views
0

好吧,所以我有ListView,我有一些問題從列表視圖和數據庫中刪除項目。我正在嘗試使用OnItemDeleting完成此操作。當我運行它時,它會返回成功;然而,它並不是我猜測,我仍然沒有從DataKeyNames中檢索我選擇的imageId(出於某種原因)。有什麼建議麼?鏈接按鈕從ListView和DB中刪除C#

這裏是我的列表視圖:

<asp:ListView ID="ListView2" runat="server" GroupItemCount="3" DataKeyNames="ImageId" OnItemDeleting="ListView2_ItemDeleting"> 

這裏是鏈接按鈕:

<asp:LinkButton runat="server" Id="lvBtn" CommandName="Delete" Text="Remove" OnClientClick="return confirm('Remove this image?')"/> 

這裏是我的代碼隱藏的C#:

protected void ListView2_ItemDeleting(object sender, ListViewDeleteEventArgs e) 
    { 
     string programId = Request.QueryString["ProgramId"]; 

     ListView2.SelectedIndex = Convert.ToInt32(e.ItemIndex); 
     string imageId = ListView2.DataKeys[e.ItemIndex].Value.ToString(); 

     // Execute the delete command 
     bool success = CatalogAccess.DeleteProgramImageRelation(imageId, programId); 

     ListView2.EditIndex = -1; 

     // Display status message 
     statusLabel2.Text = success ? "Image removed successfully" : "Failed to remove image"; 

     // Reload the grid 
     DataBind(); 
    } 

目錄訪問:

// Delete program image relationship 
public static bool DeleteProgramImageRelation(string ProgramId, string ImageId) 
{ 
    // get a configured DbCommand object 
    DbCommand comm = GenericDataAccess.CreateCommand(); 
    // set the stored procedure name 
    comm.CommandText = "DeleteProgramImageRelation"; 
    // create a new parameter 
    DbParameter param = comm.CreateParameter(); 
    param.ParameterName = "@ProgramId"; 
    param.Value = ProgramId; 
    param.DbType = DbType.Int32; 
    comm.Parameters.Add(param); 
    // create a new parameter 
    param = comm.CreateParameter(); 
    param.ParameterName = "@ImageId"; 
    param.Value = ImageId; 
    param.DbType = DbType.Int32; 
    comm.Parameters.Add(param); 
    // execute the stored procedure; 
    int result = -1; 
    try 
    { 
     result = GenericDataAccess.ExecuteNonQuery(comm); 
    } 
    catch 
    { 
     // any errors are logged in DataAccess, we ignore them here 
    } 
    // result will be 1 in case of success 
    return (result != -1); 
} 
+0

你可以在'CatalogAccess.DeleteProgramImageRelation(imageId,programId)'中顯示你在做什麼嗎? – Rahul

+0

@Rahul我更新了所以你可以看到它 – Faron

+0

程序執行後'result'的值是多少?嘗試調試,看看你得到什麼類型的異常? – Rahul

回答

0

問題解決了!原來我ProgramId沒有被捕獲的ID所以我做了小的調整,像這樣:

private string currentProgramId; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     currentProgramId = Request.QueryString["ProgramId"]; 

然後我剛剛更新了我引用的其餘部分。因此,它現在的作品完美。

0

我不確定你在做什麼,但我相信你會得到一些SQLException。嘗試通過在result = GenericDataAccess.ExecuteNonQuery(comm);中放置斷點並進行驗證來進行調試。

從您發佈的代碼方法CatalogAccess.DeleteProgramImageRelation(imageId, programId)看起來像你的過程需要一個INT類型參數,而你逝去的String類型參數可以看出以下

DbParameter param = comm.CreateParameter(); 
param.ParameterName = "@ProgramId"; 
param.Value = ProgramId; -- ProgramId is String 
param.DbType = DbType.Int32; -- You are specifying Int32 
+0

你知道嗎,你可能是對的。我要去看看我能否改變這一點。 – Faron

+0

不是問題。現在調試,看看我是否抓住了一切。 – Faron

+0

您可能需要發佈一次過程代碼,以查看它正在執行的操作。 – Rahul