Specified cast is not valid

  • Thread starter Thread starter Andy Sutorius
  • Start date Start date
A

Andy Sutorius

Hi,

I have researched this error and have found that many before me have
struggled with this. That makes me feel a little better. However, despite
all of the solutions I have seen and implemented I still have the error. My
situation is that I have a datagrid and I click on the update button I get
the error. The code:

Dim txtDescription As TextBox
txtDescription = e.Item.Cells(2).FindControl("txtArticleDesc")

I have tried e.Item.FindControl("txtArticleDesc") and I get the same error

Your help is appreciated,

Andy
 
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 72: If IsValid Then
Line 73: intID = dgrdArticle.DataKeys(e.Item.ItemIndex)
Line 74: txtDescription =
CType(e.Item.Cells(2).FindControl("txtArticleDesc"), TextBox)
Line 75: txtLink = e.Item.Cells(2).FindControl("txtArticleLink")
Line 76: chkArchive =
e.Item.Cells(3).FindControl("chkArticleArchive")

Source File: C:\Inetpub\wwwroot\sutorius\admin\editarticle.aspx.vb Line:
74

Just as an extra measure I tried Cells(1) and Cells(0) and I get the same
error
I also tried CType(e.Item.FindControl("txtArticleDesc"), Textbox) and get
the same error

Andy
 
The best thing to do in this case is use the debugger and set a break point
at this statement.

Then use immediate statements to determine the type of control and which
control position it is in. I always reference by position and have never had
this problem, eg

ctype (e.Item.Cells(2).controls(1), textbox)

type the following in the debugger to see each control in the cell
?e.Item.Cells(2).controls(0)
?e.Item.Cells(2).controls(1)
..
..
..
 
You aren't grabbing a TextBox with FindControl. You are getting something
else.

Read HoustonFreeways Response.

You can also use GetType to return a string of the type of object you are
getting if the debugger isn't an option.
 
Back
Top