compile error

  • Thread starter Thread starter LL
  • Start date Start date
L

LL

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.

Source Error:


Line 138: </ItemTemplate>
Line 139: <EditItemTemplate>
Line 140: <asp:CheckBox id=edit_Status runat="server" Checked='<%#
DataBinder.Eval(Container.DataItem, "Status")%>'>
Line 141: </asp:CheckBox>
Line 142: </EditItemTemplate>
 
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.

Source Error:


Line 138: </ItemTemplate>
Line 139: <EditItemTemplate>
Line 140: <asp:CheckBox id=edit_Status runat="server"
Checked='<%# DataBinder.Eval(Container.DataItem, "Status")%>'>
Line 141: </asp:CheckBox>
Line 142: </EditItemTemplate>

probably your StatusField isn'T of Type bool?

if it is nummeric (0,1) you could do this:
Checked='<%# 1==DataBinder.Eval(Container.DataItem, "Status")%>'>

if it'S text ("true", "false") you could do this:

Checked='<%# "true" == DataBinder.Eval(Container.DataItem, "Status")%>'>

I guess you got the idea ...
 
Hi,

If I change to GetSelectedIndex(1) , then it works.
Please help me on 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 originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 131:
Line 132:<EditItemTemplate>
Line 133: <asp:DropDownList id=edit_AdminRights runat="server"
SelectedIndex='<%# GetSelectedIndex((int)DataBinder.Eval(Container.DataItem,
"AdminRights")) %>'>
Line 134: <asp:ListItem Value="0" Selected="True">Read
Only</asp:ListItem>
Line 135: <asp:ListItem Value="1">Read/Edit</asp:ListItem>


Source File: C:\Project\test\Maintenance.aspx Line: 133
 
LL said:
If I change to GetSelectedIndex(1) , then it works.
Please help me on this error:


Specified cast is not valid.

That suggests that DataBinder.Eval(Container.DataItem,"AdminRights")
isn't returning an integer. Try writing to the debug log:

DataBinder.Eval(Container.DataItem,"AdminRights").GetType()
 
Thanks Jon.

It's 'Byte'.


Jon Skeet said:
That suggests that DataBinder.Eval(Container.DataItem,"AdminRights")
isn't returning an integer. Try writing to the debug log:

DataBinder.Eval(Container.DataItem,"AdminRights").GetType()
 
Hi,

How to fix this one:
(int) ((ListBox)E.Item.FindControl("edit_" + "AdminRights")).SelectedValue
error: '(int)' cannot be performed on '"0"'

How to get the
SelectedValue?

Thanks again.
 
LL said:
How to fix this one:
(int) ((ListBox)E.Item.FindControl("edit_" + "AdminRights")).SelectedValue
error: '(int)' cannot be performed on '"0"'

How to get the
SelectedValue?

Well I'd start by making your code more readably in the first place -
if you break that single line up into several, chances are it'll become
obvious.

That seems a strange compilation error to me, but SelectedValue returns
a string, not an integer. You'll need to use Int32.Parse or
Convert.ToInt32 in order to convert a string to an integer.
 
Thanks Jon!

How to assign the listbox's value to a command parameter?
myCommnd.Parameters["x"].Value = ((ListBox)E.Item.FindControl("edit_" +
"AdminRights")).SelectedValue?
 
ll said:
How to assign the listbox's value to a command parameter?
myCommnd.Parameters["x"].Value = ((ListBox)E.Item.FindControl("edit_" +
"AdminRights")).SelectedValue?

I don't know off-hand, to be honest. I would *guess* that you don't
need the ".Value" bit there, but that's only a guess.
 
Thanks Jon.

I found it. The control is a "Dropdown" box not "ListBox". So when I run it.
got run time error: Invalid cast...
but why when I debug it. I can print the value out though ((ListBox)E....?




Jon Skeet said:
ll said:
How to assign the listbox's value to a command parameter?
myCommnd.Parameters["x"].Value = ((ListBox)E.Item.FindControl("edit_" +
"AdminRights")).SelectedValue?

I don't know off-hand, to be honest. I would *guess* that you don't
need the ".Value" bit there, but that's only a guess.
 
ll said:
I found it. The control is a "Dropdown" box not "ListBox". So when I run it.
got run time error: Invalid cast...
but why when I debug it. I can print the value out though ((ListBox)E....?

Again, break your long lines down a bit so each only does one thing -
then you'll be much more likely to be able to find the problem quickly.
 
Back
Top