469,946 Members | 1,390 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

Unable to cast object

I get this error:

Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText'
to type 'System.Web.UI.WebControls.TextBox'.

Using this code:

Dim test3 As TextBox
test3 = CType(e.Item.FindControl("edit_name"), TextBox)

Dim SQL As String
SQL = "UPDATE phonetest SET name = '" & test3.Text & "' WHERE
ID = " & TKEY

The code above is to work with the aspx page with this code:

<input id="edit_name" type="text"
value='<%#DataBinder.Eval(Container.DataItem, "name")%>' runat="server" />

I took this example from a website. Of course, it was probably written
for framework 1.0 or 1.1. I am using 2.0.

This has become very frustrating as I've tried all kinds of things to
make this work and I've hit a wall.

Any insight would be appreciated at this point!
Sep 13 '06 #1
9 6050
The error message pretty much explains it.

The object you have is of type HtmlInputText. You are trying to cast it to
a TextBox.

These are two different types, and you can't cast one to the other. Since
you are using an HtmlInputText, you have to cast it to an HtmlInputText.

If you want a TextBox, then you should start using one instead.

"Jim in Arizona" <ti*******@hotmail.comwrote in message
news:O5**************@TK2MSFTNGP02.phx.gbl...
>I get this error:

Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText'
to type 'System.Web.UI.WebControls.TextBox'.

Using this code:

Dim test3 As TextBox
test3 = CType(e.Item.FindControl("edit_name"), TextBox)

Dim SQL As String
SQL = "UPDATE phonetest SET name = '" & test3.Text & "' WHERE ID =
" & TKEY

The code above is to work with the aspx page with this code:

<input id="edit_name" type="text"
value='<%#DataBinder.Eval(Container.DataItem, "name")%>' runat="server" />

I took this example from a website. Of course, it was probably written for
framework 1.0 or 1.1. I am using 2.0.

This has become very frustrating as I've tried all kinds of things to make
this work and I've hit a wall.

Any insight would be appreciated at this point!

Sep 13 '06 #2
Marina Levit [MVP] wrote:
The error message pretty much explains it.

The object you have is of type HtmlInputText. You are trying to cast it to
a TextBox.

These are two different types, and you can't cast one to the other. Since
you are using an HtmlInputText, you have to cast it to an HtmlInputText.

If you want a TextBox, then you should start using one instead.
That's the problem. I don't know how to implement it (or what to use).
All the examples I've found either don't include what I need, or what is
included doesn't work.

Could you give me a working example, please?
Sep 13 '06 #3
Jim in Arizona wrote:
Marina Levit [MVP] wrote:
>The error message pretty much explains it.

The object you have is of type HtmlInputText. You are trying to cast
it to a TextBox.

These are two different types, and you can't cast one to the other.
Since you are using an HtmlInputText, you have to cast it to an
HtmlInputText.

If you want a TextBox, then you should start using one instead.

That's the problem. I don't know how to implement it (or what to use).
All the examples I've found either don't include what I need, or what is
included doesn't work.

Could you give me a working example, please?
I tried changing the line in the aspx document from this:

<input id="edit_name" type="text"
value='<%#DataBinder.Eval(Container.DataItem, "name")%>' runat="server" />

To This:

<asp:TextBox ID="tb1" Text='<%#DataBinder.Eval(Container.DataItem,
"name")%>' runat="server" />

but in the code behind page, when I type in tb1.text, it doesn't know
what tb1 is (not declared). I thought once I create a control on the
aspx page, that the code behind page would automatically know of its
existence.
Sep 13 '06 #4
I think you are in a little over your head. I recommend you step back, and
start from the beginning, learn about the ASP.NET object model.

The original code you had was probably fine - except that you were trying to
cast the object to a TextBox not an HtmlInputText. So just replace
'TextBox' with 'HtmlInput'.

When you are binding to a repeater or datalist or whatever you are using
here, the item in the template doesn't actually exist as a variable. This
is because if your datasource has 20 rows, there will be 20 instances of
that object. If there are 100 rows, there will be 100. How would 'tb1' know
which one to refer to?

It can't. A template is just that - a template to use for each row during
binding.

Again, I recommend you start at the beginning, read up on the repeater or
datalist or whatever you are using here to learn how it works, and the
proper way to access the various objects in it.

"Jim in Arizona" <ti*******@hotmail.comwrote in message
news:up**************@TK2MSFTNGP03.phx.gbl...
Jim in Arizona wrote:
>Marina Levit [MVP] wrote:
>>The error message pretty much explains it.

The object you have is of type HtmlInputText. You are trying to cast it
to a TextBox.

These are two different types, and you can't cast one to the other.
Since you are using an HtmlInputText, you have to cast it to an
HtmlInputText.

If you want a TextBox, then you should start using one instead.

That's the problem. I don't know how to implement it (or what to use).
All the examples I've found either don't include what I need, or what is
included doesn't work.

Could you give me a working example, please?

I tried changing the line in the aspx document from this:

<input id="edit_name" type="text"
value='<%#DataBinder.Eval(Container.DataItem, "name")%>' runat="server" />

To This:

<asp:TextBox ID="tb1" Text='<%#DataBinder.Eval(Container.DataItem,
"name")%>' runat="server" />

but in the code behind page, when I type in tb1.text, it doesn't know what
tb1 is (not declared). I thought once I create a control on the aspx page,
that the code behind page would automatically know of its existence.

Sep 13 '06 #5
Marina Levit [MVP] wrote:
The original code you had was probably fine - except that you were trying to
cast the object to a TextBox not an HtmlInputText. So just replace
'TextBox' with 'HtmlInput'.
When I tried this:

Dim test3 As HtmlInputText
test3 = CType(e.Item.FindControl("edit_name"), HtmlInputText)
....
SQL = "UPDATE phonetest SET name = '" & test3.ToString & "' WHERE ID = "
& TKEY

I don't get an error, but the text I type into the text box turns into:

System.Web.UI.HtmlControls.HtmlInputText

instead of what I typed into the textbox.

You said to replace 'TextBox' with HtmlInput, but there is no HtmlInput.
There is:
HtmlInputButton
HtmlInputCheckBox
HtmlInputControl
HtmlInputFile

and the list goes on and on, but no HtmlInput that was visible. SO, I
tried HTMLInputText (as you can see).

I do realize I need to learn more, and although I rarely just ask for an
answer without trying everything I can first, this time, i'm hoping for
one. I have a general idea of how this works but when examples on the
web don't work when you type them in letter for letter, and when you've
looked all over the web for other examples that don't pan out either it
gets really frustrating. I've been trying and wanting to figure this
problem out for a very, very long time. (putting dynamic buttons per
table row but formatting it in a way that you want, and not just a
simple grid).

Sep 13 '06 #6
I mistyped if I said HtmlInput, I meant HtmlInputText - that is what the
type of the object is.

ToString method in this case returns the type of object. It is not coded to
return the text in the input control. Not sure why you thought it would? I
think it should have a Value property, try that.

Again, I urge you to step back and read up on these objects and how they
work. Had you looked up the HtmlInputText class in the documentation, you
would have see all the properties/methods it has, and probably not assumed
ToString would give you the text in the control.

I think what is happening now, you are sort of guessing as to how things
should work, and so you will have constant issues and problems. You are
coding hoping that something will work - instead of knowing it will. I think
it's important to be familiar with the objects you are using, their
propeties and methods, and the appropriate ways to alter their behavior.

"Jim in Arizona" <ti*******@hotmail.comwrote in message
news:uW**************@TK2MSFTNGP03.phx.gbl...
Marina Levit [MVP] wrote:
>The original code you had was probably fine - except that you were trying
to cast the object to a TextBox not an HtmlInputText. So just replace
'TextBox' with 'HtmlInput'.

When I tried this:

Dim test3 As HtmlInputText
test3 = CType(e.Item.FindControl("edit_name"), HtmlInputText)
...
SQL = "UPDATE phonetest SET name = '" & test3.ToString & "' WHERE ID = " &
TKEY

I don't get an error, but the text I type into the text box turns into:

System.Web.UI.HtmlControls.HtmlInputText

instead of what I typed into the textbox.

You said to replace 'TextBox' with HtmlInput, but there is no HtmlInput.
There is:
HtmlInputButton
HtmlInputCheckBox
HtmlInputControl
HtmlInputFile

and the list goes on and on, but no HtmlInput that was visible. SO, I
tried HTMLInputText (as you can see).

I do realize I need to learn more, and although I rarely just ask for an
answer without trying everything I can first, this time, i'm hoping for
one. I have a general idea of how this works but when examples on the web
don't work when you type them in letter for letter, and when you've looked
all over the web for other examples that don't pan out either it gets
really frustrating. I've been trying and wanting to figure this problem
out for a very, very long time. (putting dynamic buttons per table row but
formatting it in a way that you want, and not just a simple grid).

Sep 13 '06 #7
Marina Levit [MVP] wrote:
I mistyped if I said HtmlInput, I meant HtmlInputText - that is what the
type of the object is.

ToString method in this case returns the type of object. It is not coded to
return the text in the input control. Not sure why you thought it would? I
think it should have a Value property, try that.

Again, I urge you to step back and read up on these objects and how they
work. Had you looked up the HtmlInputText class in the documentation, you
would have see all the properties/methods it has, and probably not assumed
ToString would give you the text in the control.

I think what is happening now, you are sort of guessing as to how things
should work, and so you will have constant issues and problems. You are
coding hoping that something will work - instead of knowing it will. I think
it's important to be familiar with the objects you are using, their
propeties and methods, and the appropriate ways to alter their behavior.
Let me explain what I know and understand so maybe we can close this issue.

On the aspx page I've got:

<input id="edit_name" type="text"
value='<%#DataBinder.Eval(Container.DataItem, "name")%>' runat="server" />

I understand that input is an html control, which has an ID of edit_name.

On the codebehind page, i've got:

Dim test3 As TextBox
test3.Text = CType(e.Item.FindControl("edit_name"), TextBox)

I understand here that it is (should) find the control with the ID of
edit_name, and take that object and try to cast it to an object of type
TextBox (from HTMLInputText, which is an <input>, right?). However,
VisualStudio 2005 puts a big blue underline under the
CType(e.Item.FindControl("edit_name"), TextBox) saying "Value of type
'System.Web.Ui.WebControls.TextBox' cannot be converted to 'String'". If
I add .text at the end of that ctype function, like so:

test3.Text = CType(e.Item.FindControl("edit_name"), TextBox).Text

The underline goes away and all seems well. However, in my SQL string, I
put:

SQL = "UPDATE phonetest SET name = '" & test3 & "' WHERE ID = " & TKEY

and, of course, the "SQL = "UPDATE phonetest SET name = '" & test3"
portion gets underlined saying, "Operator '&' is not defined for
'String' and 'System.Web.UI.WebControls.TextBox'". What?? Why can't I
put a string there? So, if I just add .Text to the end of test3, like so:

SQL = "UPDATE phonetest SET name = '" & test3.Text & "' ....

The Underline goes away. Then, I get a green underline under the test3
portion of the line:

test3.Text = CType(e.Item.FindControl("edit_name"), TextBox).Text

Saying, "Variable 'test3' is used before it has been assigned a value. A
null reference exception could occur at runtime".

Now, correct me if I'm wrong, but I thought I was assigning a value at
that line, well, a value for the text property of the test3 object. In
any case, when I run update routine, I get the ol' error:

Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText'
to type 'System.Web.UI.WebControls.TextBox'.

So, I understand that the 2.0 framework is telling me that it cannot
cast an htmlinputtext object (the <inputhtml from the aspx page) to a
textbox object. I don't understand why that is though.

The HtmlInputText object does not have a text property like the textbox
object does. I did try this:

Dim test3 As HtmlInputText
test3.Value = CType(e.Item.FindControl("edit_name"), HtmlInputText).Value

SQL = "UPDATE phonetest SET name = '" & test3.Value & "' WHERE ID = " & TKEY

BUt, I get the "Object reference not set to an instance of an object"
error. I had to put the .Value at the end of the cast function because
it gave me an error otherwise saying that "Value of type
'System.Web.UI.HtmlControls.HtmlInputText' cannot be converted to 'String'.

In the error: "Object reference not set to an instance of an object", is
it saying that "edit_name" isn't an object instance?

All I want to do is get the value of that control (the text) assigned
into a variable so I can use the contentes of that variable in my SQL
statement. So, yea, I'm shooting in the dark trying to make it work.

Sep 13 '06 #8
In your last example, you declare 'test3', but it never gets a value. You
never instantiate, you never assign it to an existing object. All you
declare is a variable that is capable of pointing to an object. But it
doesn't point to an object. It points to Nothing. And you are trying to
assign a value to a property of an object that doesn't exist.

Please reread my advice from before. You are trying to bite off way more
then you can chew. I think you lack the basic understanding of how VB.NET
and objects work, and especially how the ASP.NET server side objects work.
I think you need to learn the fundamentals before you can get anywhere.
Otherwise you will end up here posting questions about every line of code.

"Jim in Arizona" <ti*******@hotmail.comwrote in message
news:e1**************@TK2MSFTNGP06.phx.gbl...
Marina Levit [MVP] wrote:
>I mistyped if I said HtmlInput, I meant HtmlInputText - that is what the
type of the object is.

ToString method in this case returns the type of object. It is not coded
to return the text in the input control. Not sure why you thought it
would? I think it should have a Value property, try that.

Again, I urge you to step back and read up on these objects and how they
work. Had you looked up the HtmlInputText class in the documentation, you
would have see all the properties/methods it has, and probably not
assumed ToString would give you the text in the control.

I think what is happening now, you are sort of guessing as to how things
should work, and so you will have constant issues and problems. You are
coding hoping that something will work - instead of knowing it will. I
think it's important to be familiar with the objects you are using, their
propeties and methods, and the appropriate ways to alter their behavior.

Let me explain what I know and understand so maybe we can close this
issue.

On the aspx page I've got:

<input id="edit_name" type="text"
value='<%#DataBinder.Eval(Container.DataItem, "name")%>' runat="server" />

I understand that input is an html control, which has an ID of edit_name.

On the codebehind page, i've got:

Dim test3 As TextBox
test3.Text = CType(e.Item.FindControl("edit_name"), TextBox)

I understand here that it is (should) find the control with the ID of
edit_name, and take that object and try to cast it to an object of type
TextBox (from HTMLInputText, which is an <input>, right?). However,
VisualStudio 2005 puts a big blue underline under the
CType(e.Item.FindControl("edit_name"), TextBox) saying "Value of type
'System.Web.Ui.WebControls.TextBox' cannot be converted to 'String'". If I
add .text at the end of that ctype function, like so:

test3.Text = CType(e.Item.FindControl("edit_name"), TextBox).Text

The underline goes away and all seems well. However, in my SQL string, I
put:

SQL = "UPDATE phonetest SET name = '" & test3 & "' WHERE ID = " & TKEY

and, of course, the "SQL = "UPDATE phonetest SET name = '" & test3"
portion gets underlined saying, "Operator '&' is not defined for 'String'
and 'System.Web.UI.WebControls.TextBox'". What?? Why can't I put a string
there? So, if I just add .Text to the end of test3, like so:

SQL = "UPDATE phonetest SET name = '" & test3.Text & "' ....

The Underline goes away. Then, I get a green underline under the test3
portion of the line:

test3.Text = CType(e.Item.FindControl("edit_name"), TextBox).Text

Saying, "Variable 'test3' is used before it has been assigned a value. A
null reference exception could occur at runtime".

Now, correct me if I'm wrong, but I thought I was assigning a value at
that line, well, a value for the text property of the test3 object. In any
case, when I run update routine, I get the ol' error:

Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText'
to type 'System.Web.UI.WebControls.TextBox'.

So, I understand that the 2.0 framework is telling me that it cannot cast
an htmlinputtext object (the <inputhtml from the aspx page) to a textbox
object. I don't understand why that is though.

The HtmlInputText object does not have a text property like the textbox
object does. I did try this:

Dim test3 As HtmlInputText
test3.Value = CType(e.Item.FindControl("edit_name"), HtmlInputText).Value

SQL = "UPDATE phonetest SET name = '" & test3.Value & "' WHERE ID = " &
TKEY

BUt, I get the "Object reference not set to an instance of an object"
error. I had to put the .Value at the end of the cast function because it
gave me an error otherwise saying that "Value of type
'System.Web.UI.HtmlControls.HtmlInputText' cannot be converted to
'String'.

In the error: "Object reference not set to an instance of an object", is
it saying that "edit_name" isn't an object instance?

All I want to do is get the value of that control (the text) assigned into
a variable so I can use the contentes of that variable in my SQL
statement. So, yea, I'm shooting in the dark trying to make it work.

Sep 13 '06 #9
Marina Levit [MVP] wrote:
In your last example, you declare 'test3', but it never gets a value. You
never instantiate, you never assign it to an existing object. All you
declare is a variable that is capable of pointing to an object. But it
doesn't point to an object. It points to Nothing. And you are trying to
assign a value to a property of an object that doesn't exist.

Please reread my advice from before. You are trying to bite off way more
then you can chew. I think you lack the basic understanding of how VB.NET
and objects work, and especially how the ASP.NET server side objects work.
I think you need to learn the fundamentals before you can get anywhere.
Otherwise you will end up here posting questions about every line of code.
That's what I needed. I quite often tend to forget to instantiate
objects. (not using New). So, yea, "Dim test3 as NEW HtmlInputText".

Now it is working. Finally, after several years of wanting to do this,
it is working. Now i'm on my way to making bigger and better applications.

Thanks for your help, Marina!

Jim.
Sep 13 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Pankaj Jain | last post: by
reply views Thread by hlyall1189 | last post: by
1 post views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
1 post views Thread by =?Utf-8?B?U2NvdHQ=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.