By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

Specified cast is not valid exception

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.