Click to See Complete Forum and Search --> : Invalid cast exception


aquafin
August 28th, 2005, 10:31 PM
I am dynamically creating a text box and adding it to the DataList
But it gives me an error at line : 7 ,Specified cast is not valid.

How do i add the text box control to the data list.

1: TextBox1 = New TextBox
2: TextBox1.ID = "tb" & i
3: TextBox1.Style("Position") = "Absolute"
4: TextBox1.Style("Top") = "25px"
5: TextBox1.Style("Left") = "100px"
6: TextBox1.Text = StrQuestion.Substring(0, 1)
7: DG1.Controls.Add(CType(TextBox1, System.Web.UI.Control))

exterminator
September 1st, 2005, 04:18 PM
Try using the Convert class.

TSmooth
September 2nd, 2005, 07:40 AM
I could be wrong but I'm pretty sure you don't need to cast that at all. Just make it DG1.Controls.Add(TextBox1).

aquafin
September 5th, 2005, 10:04 PM
I tried without using cast but it still gives me the same error.Could anyone please solve this for me?

mmetzger
September 6th, 2005, 09:26 AM
What is DG1 defined as? Trying this with a datagrid doesn't give the same error but complains about the same id being present multiple times.

aquafin
September 6th, 2005, 09:25 PM
DG1 is defined as datalist ,
I will try it with datagrid