472,328 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 6243
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast...
8
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ...
0
by: sam | last post by:
Hi: I am not sure if this is the right place to post this question. Please let me know if it is not and I appreciate if someone could point me in...
0
by: hlyall1189 | last post by:
Hi, I recently started upgrading some of my old vs 2003 apps to vs 2005 and used the conversion tool but now i get the following error after...
2
by: John Smith | last post by:
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol { public XmlDocument...
3
by: keithb | last post by:
What could be causing this? this code: String Com = ""; if (Com != (String)rw.ItemArray) fails at runtime with the error message: Unable to...
10
by: mypetrock | last post by:
Has anyone run into this error message? Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'. I'm trying to cast an object of type Foo.Bar...
1
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a master page and a content page to that master. When I try to work with them I get an inconsistent error of casting ability and it...
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.