将更新添加到gridview时出错

时间:2011-05-30 19:06:22

标签: c# asp.net sql-server-2008 gridview

我正在尝试和我的gridview更新功能。我收到错误无法将'System.Web.UI.WebControls.DataControlLinkBut​​ton'类型的对象强制转换为'System.Web.UI.WebControls.TextBox'。

我的代码和存储过程如下。任何人都可以看到我可能会出错的地方。当我尝试传递电子邮件的参数时,它会收到错误。 感谢

    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 个答案:

答案 0 :(得分:1)

我建议GridViewED.Rows[e.RowIndex].Cells[4].Controls[0]的控件不是TextBox。没有其他建议,那么你有错误的单元格号或Controls索引?此外,电子邮件文本框可能位于网格中的错误位置?