473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to cast object

I get this error:

Unable to cast object of type 'System.Web.UI. HtmlControls.Ht mlInputText'
to type 'System.Web.UI. WebControls.Tex tBox'.

Using this code:

Dim test3 As TextBox
test3 = CType(e.Item.Fi ndControl("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='<%#DataB inder.Eval(Cont ainer.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 6404
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*******@hotm ail.comwrote in message
news:O5******** ******@TK2MSFTN GP02.phx.gbl...
>I get this error:

Unable to cast object of type 'System.Web.UI. HtmlControls.Ht mlInputText'
to type 'System.Web.UI. WebControls.Tex tBox'.

Using this code:

Dim test3 As TextBox
test3 = CType(e.Item.Fi ndControl("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='<%#DataB inder.Eval(Cont ainer.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
HtmlInputTex t.

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='<%#DataB inder.Eval(Cont ainer.DataItem, "name")%>' runat="server" />

To This:

<asp:TextBox ID="tb1" Text='<%#DataBi nder.Eval(Conta iner.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*******@hotm ail.comwrote in message
news:up******** ******@TK2MSFTN GP03.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='<%#DataB inder.Eval(Cont ainer.DataItem, "name")%>' runat="server" />

To This:

<asp:TextBox ID="tb1" Text='<%#DataBi nder.Eval(Conta iner.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.Fi ndControl("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.H tmlControls.Htm lInputText

instead of what I typed into the textbox.

You said to replace 'TextBox' with HtmlInput, but there is no HtmlInput.
There is:
HtmlInputButton
HtmlInputCheckB ox
HtmlInputContro l
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*******@hotm ail.comwrote in message
news:uW******** ******@TK2MSFTN GP03.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.Fi ndControl("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.H tmlControls.Htm lInputText

instead of what I typed into the textbox.

You said to replace 'TextBox' with HtmlInput, but there is no HtmlInput.
There is:
HtmlInputButton
HtmlInputCheckB ox
HtmlInputContro l
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='<%#DataB inder.Eval(Cont ainer.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.Fi ndControl("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.Fi ndControl("edit _name"), TextBox) saying "Value of type
'System.Web.Ui. WebControls.Tex tBox' cannot be converted to 'String'". If
I add .text at the end of that ctype function, like so:

test3.Text = CType(e.Item.Fi ndControl("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.Tex tBox'". 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.Fi ndControl("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.Ht mlInputText'
to type 'System.Web.UI. WebControls.Tex tBox'.

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.Fi ndControl("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.Ht mlInputText' 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*******@hotm ail.comwrote in message
news:e1******** ******@TK2MSFTN GP06.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='<%#DataB inder.Eval(Cont ainer.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.Fi ndControl("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.Fi ndControl("edit _name"), TextBox) saying "Value of type
'System.Web.Ui. WebControls.Tex tBox' cannot be converted to 'String'". If I
add .text at the end of that ctype function, like so:

test3.Text = CType(e.Item.Fi ndControl("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.Tex tBox'". 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.Fi ndControl("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.Ht mlInputText'
to type 'System.Web.UI. WebControls.Tex tBox'.

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.Fi ndControl("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.Ht mlInputText' 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
7389
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 is exposed to client through remoting on Http channal hosting into IIS. There is a class B which is also available through remoting hosted on IIS on the same URI. B creates new of A inside a function. It succeed and able to create instance of A inside B first time. But it failes in 2nd attempt when...
3
13005
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 object of type 'System.Byte' to type 'System.String'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
8
6511
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 : ''''''''''''nodes is a TreeNodeCollection, s is string
0
1577
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 the right direction. I am getting this error after converting to .NET 2.0. Unable to cast object of type 'Oracle.DataAccess.Client.OracleCommand' to type 'System.Data.Common.DbCommand'
0
1630
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 building the page. I have typecasted the lines as follows: ((StyleSheetProvider)this.Page).GetStyleSheetPath(); Is there some different way of typecasting that needs to be done in vs2005? Thanks in advance.
2
13592
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 validate(string url, XmlDocument xmlDocument) { this.Url = url;
3
10258
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 cast object of type 'System.Int32' to type 'System.String'.
10
2539
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 that I got out of a hash table into a variable of type Foo.Bar. Foo.Bar data = (For.Bar)input; Thanks,
1
3754
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 happens irregularly, which means sometimes it can work and sometimes not, without making any changes to the code. Is this a bug concern? Or do you have any advice for this issue? Here is the error message I get:
1
2345
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 been placed in the App_Code folder. The web.config has been updated with the necessary section. Using System.Web.Configuration.WebConfiguration.GetSection(), the config information is returned without any issues when the GetSection is set to an object. When the object is casted explicitly...
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.