473,416 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Specified cast is not valid exception

PK9
I am looking for assistance in pinpointing the cause of the following
exception. I am getting a "Specified Cast is not valid" exception on my
page. I am trying to populate a datagrid. One of my columns is a template
column where I'd like to evaluate the data brought back from the db and
populate the column with a "Y" or "N" depending on the value in the db. Here
is the code that is causing it as well as the error (below).
ERROR MESSAGE:
Specified cast is not valid
Exception Details: System.InvalidCastException: Specified cast is not valid.
<%# ((bool)DataBinder.Eval(Container.DataItem, "ReportsTo")) == false ? "N"
: "Y" %>
Code (in aspx page):
<asp:TemplateColumn HeaderText="Reports To" >
<ItemTemplate>
<%# ((bool)DataBinder.Eval(Container.DataItem, "ReportsTo")) == false ?
"N" : "Y" %>
</ItemTemplate>
</asp:TemplateColumn>

--
PK9
Nov 19 '05 #1
3 10487
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
I am looking for assistance in pinpointing the cause of the following
exception. I am getting a "Specified Cast is not valid" exception on my
page. I am trying to populate a datagrid. One of my columns is a
template
column where I'd like to evaluate the data brought back from the db and
populate the column with a "Y" or "N" depending on the value in the db.
Here
is the code that is causing it as well as the error (below).
ERROR MESSAGE:
Specified cast is not valid
Exception Details: System.InvalidCastException: Specified cast is not
valid.
<%# ((bool)DataBinder.Eval(Container.DataItem, "ReportsTo")) == false ?
"N"
: "Y" %>


What is the type of DataBinder.Eval(Container.DataItem, "ReportsTo")? Try

<%# DataBinder.Eval(Container.DataItem, "ReportsTo").GetType().FullName %>

for debugging purposes.

John Saunders
Nov 19 '05 #2
PK9
The "ReportsTo" field is an integer. The DataBinder.Eval call returns an
object (unless you pass in a format specifier eg. {0:d}). So I'm now trying
to use the following syntax because an object is returned:
<%# Convert.ToBoolean(DataBinder.Eval(Container.DataIt em, "ReportsTo"))) ==
false ? "N" : "Y" %>

This might work if there weren't any dbnull values in the database. So I'm
now trying to figure out how to check for dbnull values in the
OnItemDataCommand event and replace those values with the appropriate text
("N").

"John Saunders" wrote:
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
I am looking for assistance in pinpointing the cause of the following
exception. I am getting a "Specified Cast is not valid" exception on my
page. I am trying to populate a datagrid. One of my columns is a
template
column where I'd like to evaluate the data brought back from the db and
populate the column with a "Y" or "N" depending on the value in the db.
Here
is the code that is causing it as well as the error (below).
ERROR MESSAGE:
Specified cast is not valid
Exception Details: System.InvalidCastException: Specified cast is not
valid.
<%# ((bool)DataBinder.Eval(Container.DataItem, "ReportsTo")) == false ?
"N"
: "Y" %>


What is the type of DataBinder.Eval(Container.DataItem, "ReportsTo")? Try

<%# DataBinder.Eval(Container.DataItem, "ReportsTo").GetType().FullName %>

for debugging purposes.

John Saunders

Nov 19 '05 #3
You are trying to cast an Int32 to a boolean. This is where the invalid
cast is coming from.

Why don't you just put the logic in your sql statment using a case statement
in your select?

Then you don't have to worry about determining Y or N.

bill

"PK9" <PK*@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
The "ReportsTo" field is an integer. The DataBinder.Eval call returns an
object (unless you pass in a format specifier eg. {0:d}). So I'm now trying to use the following syntax because an object is returned:
<%# Convert.ToBoolean(DataBinder.Eval(Container.DataIt em, "ReportsTo"))) == false ? "N" : "Y" %>

This might work if there weren't any dbnull values in the database. So I'm now trying to figure out how to check for dbnull values in the
OnItemDataCommand event and replace those values with the appropriate text
("N").

"John Saunders" wrote:
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
I am looking for assistance in pinpointing the cause of the following
exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my columns is a
template
column where I'd like to evaluate the data brought back from the db and populate the column with a "Y" or "N" depending on the value in the db. Here
is the code that is causing it as well as the error (below).
ERROR MESSAGE:
Specified cast is not valid
Exception Details: System.InvalidCastException: Specified cast is not
valid.
<%# ((bool)DataBinder.Eval(Container.DataItem, "ReportsTo")) == false ? "N"
: "Y" %>


What is the type of DataBinder.Eval(Container.DataItem, "ReportsTo")? Try
<%# DataBinder.Eval(Container.DataItem, "ReportsTo").GetType().FullName %>
for debugging purposes.

John Saunders

Nov 19 '05 #4

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

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
6
by: Raj | last post by:
I have a c# program in which I used cast. That worked perfectly on .Net platform 1.0. When I moved to .Net 2003, I am getting the execption "Specified cast is not valid". I have not made any...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
3
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
0
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
2
by: Kashiefah | last post by:
Hi, I keep on receiving this error when I click on the edit email link from the datagrid:Specified cast is not valid. Description: An unhandled exception occurred during the execution of the...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
2
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.