2011-05-30 54 views
0

我想和我的gridview更新功能。我收到錯誤無法轉換'System.Web.UI.WebControls.DataControlLinkBut​​ton'類型的對象來鍵入'System.Web.UI.WebControls.TextBox'。錯誤添加更新到gridview

我的代碼和存儲過程如下。任何人都可以看到我可能會出錯的地方。當我嘗試傳遞電子郵件參數時,它會得到錯誤。 感謝

protected void GridViewED_RowUpdating(object sender, GridViewUpdateEventArgs e) 
     { 
       SqlDataReader myDataReader = default(SqlDataReader); 

       SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RescueAnimalsIrelandConnectionString"].ConnectionString); 
       SqlCommand command = new SqlCommand("sp_UpdateRescueDetails", MyConnection); 

       if (!User.Identity.IsAuthenticated) 
       { 
       } 
       else 
       { 
        command.Parameters.Add("@PostalAddress", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[1].Controls[0]).Text; 

        command.Parameters.Add("@TelephoneNo", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 

        command.Parameters.Add("@Website", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 

        command.Parameters.Add("@Email", SqlDbType.VarChar).Value = ((TextBox)GridViewED.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 

       } 

       try 
       { 
        command.CommandType = CommandType.StoredProcedure; 
        MyConnection.Open(); 


        myDataReader = command.ExecuteReader(CommandBehavior.CloseConnection); 
        // myDataReader.Read(); 


        GridViewED.DataSource = myDataReader; 
        GridViewED.DataBind(); 

        if (GridViewED.Rows.Count >= 1) 
        { 

         GridViewED.Visible = true; 
         lblMsg.Visible = false; 

        } 
        else if (GridViewED.Rows.Count < 1) 
        { 
         GridViewED.Visible = false; 

         lblMsg.Text = "Your search criteria returned no results."; 
         lblMsg.Visible = true; 
        } 

        MyConnection.Close(); 
       } 
       catch (SqlException SQLexc) 
       { 
        Response.Write("Read Failed : " + SQLexc.ToString()); 
       } 
       GridViewED.EditIndex = -1; 
      } 
     } 




Alter PROC [dbo].[sp_UpdateRescueDetails] 
(
@UserName nvarchar(50), 
@PostalAddress nvarchar(50), 
@TelephoneNo nvarchar (50), 
@Website nvarchar(50), 
@Email nvarchar(50) 
) 
AS 
BEGIN 


Update [RescueDetails] 
set [PostalAddress] = @PostalAddress, [Telephone_No] = @TelephoneNo, 
[Website] = @Website, [Email] = @Email 
Where [UserName] = @UserName 

回答

1

我會建議在GridViewED.Rows[e.RowIndex].Cells[4].Controls[0]控制不是TextBox。沒有其他建議其他,那麼你有錯誤的細胞數量或Controls指數?另外,也許電子郵件文本框在網格中的位置不對?

+0

是的,它非常簡單。我已經開始從1開始計算單元格,但應該從0開始。謝謝 – mar84 2011-05-30 19:38:54