473,287 Members | 2,682 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,287 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 10481
"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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.