creating controls dynamically

I'm trying to create a control from a string in a database:

Dim asm As [Assembly] =
Reflection.Assembly.LoadFrom("C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Web.dll")

Dim typ As Type = asm.GetType("System.Web.UI.WebControls.TextBox", True,
True)

Dim o As Object = Activator.CreateInstance(typ)

Me.Controls.Add(CType(o, Control))

I get "Specified cast is not valid." with the line of code directly above.
How do I/ Can I cast a type to a control?

paulscalfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hismightinessCommented:
I may have not paid enough attention but should this be change to?:

Me.Controls.Add(CType(o, TextBox))

OR

Me.Controls.Add(CType(o, System.Web.UI.WebControls.TextBox))
0
paulscalfAuthor Commented:
"System.Web.UI.WebControls.TextBox" is a string being passed into the function therefore, neither of these will work. We are creating a data driven app. I'd like to pass in the name of a control, again as a string, e.g."System.Web.UI.WebControls.TextBox", and be able to create a control to add to the form or webform... Thanks.
0
Jeff CertainCommented:
The issue is that you are returning a string, and passing it to get the type of an object... which will return string for your case.

Try this approach.

private function getTypeFromName(typeName as string) as type
select case typeName
case  "System.Web.UI.WebControls.TextBox"
  return GetType(textbox)
end select
end function

Then, use this code...

Dim typ As Type = asm.GetType(getTypeFromName(typeName), True,
True)

Dim o As Object = Activator.CreateInstance(typ)

Me.Controls.Add(CType(o, Control))
0
paulscalfAuthor Commented:
I can't use a case. I would have to add to cases to the getTypeFromName function for each control I wanted to create. How do I/ Can I from only a string, "System.Web.UI.WebControls.TextBox" create a control and add it to a form or webform?
0
Jeff CertainCommented:
I've had to do this in the past and couldn't find any way other than using a select statement to return the proper type. Although .Net supports the Type.ToString, there doesn't seem to be any way to do the reverse.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.