473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error: specified cast is not valid. Why not?

Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using it
to populate some controls on the form. Amongst the fields pulled out of
the table are two integer fields, which I am trying to cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...

if (dtrDilemmas.Re ad()) {
litDilCurrentYe sNo.Text = (String)dtrDile mmas["CurrentYes "] + "
votes yes and " + (String)dtrDile mmas["CurrentNo"] + " votes no";
}

where dtrDilemmas is the SqlDataReader that contains just one record. In
that record are two fields, CurrentYes and CurrentNo, which are both
integers. I have other (varchar) fields which I can use just fine. When
it hits the line shown above, I get the error "specified cast is not
valid".

Anyone any idea why not and how I fix it? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
5 1900
Try

litDilCurrentYe sNo.Text = ((int)dtrDilemm as
["CurrentYes "]).Tostring() + "votes yes and " + ((int)
dtrDilemmas["CurrentNo"]).Tostring() + " votes no";

HTH,

Elton Wang
el*******@hotma il.com

-----Original Message-----
Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using itto populate some controls on the form. Amongst the fields pulled out ofthe table are two integer fields, which I am trying to cast as stringsso they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...
if (dtrDilemmas.Re ad()) {
litDilCurrentYe sNo.Text = (String)dtrDile mmas ["CurrentYes "] + "votes yes and " + (String)dtrDile mmas["CurrentNo"] + " votes no"; }

where dtrDilemmas is the SqlDataReader that contains just one record. Inthat record are two fields, CurrentYes and CurrentNo, which are bothintegers. I have other (varchar) fields which I can use just fine. Whenit hits the line shown above, I get the error "specified cast is notvalid".

Anyone any idea why not and how I fix it? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
.

Nov 19 '05 #2
You can't cast the object as a string because it isn't a string. However,
you can use the DataReader's methods to get the string out of it, such as
GetString(). Example:

litDilCurrentYe sNo.Text =
dtrDilemmas.Get String(dtrDilem mas.GetOrdinal( "CurrentYes ")) ...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Alan Silver" <al*********@no spam.thanx> wrote in message
news:tv******** ******@nospamth ankyou.spam...
Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using it
to populate some controls on the form. Amongst the fields pulled out of
the table are two integer fields, which I am trying to cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...

if (dtrDilemmas.Re ad()) {
litDilCurrentYe sNo.Text = (String)dtrDile mmas["CurrentYes "] + "
votes yes and " + (String)dtrDile mmas["CurrentNo"] + " votes no";
}

where dtrDilemmas is the SqlDataReader that contains just one record. In
that record are two fields, CurrentYes and CurrentNo, which are both
integers. I have other (varchar) fields which I can use just fine. When
it hits the line shown above, I get the error "specified cast is not
valid".

Anyone any idea why not and how I fix it? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #3
Thanks to both of you. It's obvious now, but it needed pointing out ;-)
Try

litDilCurrentY esNo.Text = ((int)dtrDilemm as
["CurrentYes "]).Tostring() + "votes yes and " + ((int)
dtrDilemmas["CurrentNo"]).Tostring() + " votes no";

HTH,

Elton Wang
el*******@hotm ail.com

-----Original Message-----
Hello,

I have an ASP.NET page where I am grabbing an

SqlDataReade r and using it
to populate some controls on the form. Amongst the fields

pulled out of
the table are two integer fields, which I am trying to

cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for

clarity) ...

if (dtrDilemmas.Re ad()) {
litDilCurrentYe sNo.Text = (String)dtrDile mmas

["CurrentYes "] + "
votes yes and " + (String)dtrDile mmas["CurrentNo"] + "

votes no";
}

where dtrDilemmas is the SqlDataReader that contains just

one record. In
that record are two fields, CurrentYes and CurrentNo,

which are both
integers. I have other (varchar) fields which I can use

just fine. When
it hits the line shown above, I get the error "specified

cast is not
valid".

Anyone any idea why not and how I fix it? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
.


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #4
Really, I thought you were able to downcast from Object to String since
string inherits from Object. Can you shed some light on this subject for the
benefit of all?

"Kevin Spencer" wrote:
You can't cast the object as a string because it isn't a string. However,
you can use the DataReader's methods to get the string out of it, such as
GetString(). Example:

litDilCurrentYe sNo.Text =
dtrDilemmas.Get String(dtrDilem mas.GetOrdinal( "CurrentYes ")) ...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Alan Silver" <al*********@no spam.thanx> wrote in message
news:tv******** ******@nospamth ankyou.spam...
Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using it
to populate some controls on the form. Amongst the fields pulled out of
the table are two integer fields, which I am trying to cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...

if (dtrDilemmas.Re ad()) {
litDilCurrentYe sNo.Text = (String)dtrDile mmas["CurrentYes "] + "
votes yes and " + (String)dtrDile mmas["CurrentNo"] + " votes no";
}

where dtrDilemmas is the SqlDataReader that contains just one record. In
that record are two fields, CurrentYes and CurrentNo, which are both
integers. I have other (varchar) fields which I can use just fine. When
it hits the line shown above, I get the error "specified cast is not
valid".

Anyone any idea why not and how I fix it? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)


Nov 19 '05 #5
You can cast anything to a string that IS a string. For example, if you use
object (generally not a good idea), you can store ANY kind of object in it.
So, let's say that the object contains a number. Could you cast it as a
string? No. You would have to Convert it to a string. This can be done using
the .ToString() method, or Convert.ToStrin g().

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Tampa .NET Koder" <Ta***********@ discussions.mic rosoft.com> wrote in
message news:0F******** *************** ***********@mic rosoft.com...
Really, I thought you were able to downcast from Object to String since
string inherits from Object. Can you shed some light on this subject for
the
benefit of all?

"Kevin Spencer" wrote:
You can't cast the object as a string because it isn't a string. However,
you can use the DataReader's methods to get the string out of it, such as
GetString(). Example:

litDilCurrentYe sNo.Text =
dtrDilemmas.Get String(dtrDilem mas.GetOrdinal( "CurrentYes ")) ...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Alan Silver" <al*********@no spam.thanx> wrote in message
news:tv******** ******@nospamth ankyou.spam...
> Hello,
>
> I have an ASP.NET page where I am grabbing an SqlDataReader and using
> it
> to populate some controls on the form. Amongst the fields pulled out of
> the table are two integer fields, which I am trying to cast as strings
> so they can be displayed in a Literal control.
>
> I am using the following code (heavily edited for clarity) ...
>
> if (dtrDilemmas.Re ad()) {
> litDilCurrentYe sNo.Text = (String)dtrDile mmas["CurrentYes "] + "
> votes yes and " + (String)dtrDile mmas["CurrentNo"] + " votes no";
> }
>
> where dtrDilemmas is the SqlDataReader that contains just one record.
> In
> that record are two fields, CurrentYes and CurrentNo, which are both
> integers. I have other (varchar) fields which I can use just fine. When
> it hits the line shown above, I get the error "specified cast is not
> valid".
>
> Anyone any idea why not and how I fix it? TIA
>
> --
> Alan Silver
> (anything added below this line is nothing to do with me)


Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5840
by: Ron Holmes | last post by:
I posted this question on the Crystal Reports Support site and I am still waiting for an answer. Using Crystal Reports 9.0 Developer Full edition: My Crystal report .RPT file has a Picture box which is an OLE Object located in the Page Header section of my .RPT file. This is the call which works perfectly in VB6 to format the crystal reports Page Header section of my report file EquipLst.rpt.
13
3328
by: LL | last post by:
Hi, How to fix this problem: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Specified cast is not valid.
5
3975
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which is "newstotal"), but I'm getting an error (System.InvalidCastException: Specified cast is not valid) from the newstotal line: price = (Decimal)(rowData); newstotal = (rowData); newstotal is an integer, just wondering what datatype i should use
4
5758
by: SRLoka | last post by:
I have an application that has five threads. All five threads write to the same MSMQ. All five threads are using the same global instance to send data to the MSMQ server(on same machine). Occasionally, I get the erroror below. The data(string) being sent is not null or empty. Is this a problem related to multiple threads ? Right now I have coded it like this Controller.MSMQ.Send(Record); Should I change to
8
9473
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
1
1403
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which is "newstotal"), but I'm getting an error (System.InvalidCastException: Specified cast is not valid) from the newstotal line: price = (Decimal)(rowData); newstotal = (rowData); newstotal is an integer, just wondering what datatype i should use
0
1975
by: kcm | last post by:
Hello all, Not sure if this is the right place to post this. I ran up the samples for adventureworks for MSSQL2005. I setup the shop front. The web site is up. But when I try to register a new user via the web page I get this error. Specified cast is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it...
3
3699
by: keithb | last post by:
Using a GridView, I get a "Specified cast is not valid" error when binding the Visible propery of a hyperlink control to a DataTable text field. The error goes away if I replace the data binding statement with "true" or "false". This code causes the error: <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("c2Hyperlink1Link") %>' Target="_blank"
1
4442
vinci
by: vinci | last post by:
Please help me out... i only have two weeks to do it... here is my code: Dim ex As New Object Dim wb As Excel.Workbook Dim sheet As Excel.Worksheet
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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
10003
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...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
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();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
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
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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.