Reputation: 13
I am trying to and an update function to my gridview. I am getting the error Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
My code and stored procedure are below. Can anyone see where I might be going wrong. It gets the error when I try to pass the parameter for email. Thanks
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
Upvotes: 0
Views: 208
Reputation: 29186
I would suggest that the control at GridViewED.Rows[e.RowIndex].Cells[4].Controls[0]
is not a TextBox
. Not much else to suggest other then you have the wrong cell number or Controls
index? Also, maybe the email textbox is in the wrong place in the grid?
Upvotes: 1