SirReadAlot
asked on
Specified cast is not valid.
Hi experts,
I keep getting the above error each time I click on the edit button
is there anything I can do to recitify this?
here is the code giving error
if (e.Item.ItemType == ListItemType.EditItem)
Line 177: {
Line 178: DataRowView objDataRowView = (DataRowView)e.Item.DataIt em; ===> highlighted
Line 179: string currentDeduction = (string)objDataRowView[1]. ToString() ;
Line 180:
fullcode
is
=================
public void dgCompensation_ItemDataBou nd(Object Sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DataRowView objDataRowView = (DataRowView)e.Item.DataIt em;
string currentDeduction = (string)objDataRowView[2]. ToString() ;
DropDownList ctlDropDownList = (DropDownList)e.Item.FindC ontrol("De ductDropDo wnList");
//
if (ctlDropDownList != null)
{
ctlDropDownList.Items.Add( "E");
}
//
ctlDropDownList.SelectedIn dex = ctlDropDownList.Items.Inde xOf(ctlDro pDownList. Items.Find ByText(cur rentDeduct ion));
}
}
===================
I keep getting the above error each time I click on the edit button
is there anything I can do to recitify this?
here is the code giving error
if (e.Item.ItemType == ListItemType.EditItem)
Line 177: {
Line 178: DataRowView objDataRowView = (DataRowView)e.Item.DataIt
Line 179: string currentDeduction = (string)objDataRowView[1].
Line 180:
fullcode
is
=================
public void dgCompensation_ItemDataBou
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DataRowView objDataRowView = (DataRowView)e.Item.DataIt
string currentDeduction = (string)objDataRowView[2].
DropDownList ctlDropDownList = (DropDownList)e.Item.FindC
//
if (ctlDropDownList != null)
{
ctlDropDownList.Items.Add(
}
//
ctlDropDownList.SelectedIn
}
}
===================
ASKER
infact what does the msg mean,?
Specified cast is not valid
Specified cast is not valid
ASKER
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.InvalidCastExceptio n: Specified cast is not valid
Exception Details: System.InvalidCastExceptio
ASKER
maybe this line needs to be converted to string or int
DataRowView objDataRowView = (DataRowView)e.Item.DataIt em;
DataRowView objDataRowView = (DataRowView)e.Item.DataIt
ASKER
do i need somthing like this
DataRowView objDataRowView = (Int64)(DataRowView)e.Item .DataItem;
DataRowView objDataRowView = (Int64)(DataRowView)e.Item
Have no time to try this, but I suppose it would do no good to put brackets round the expression, like the Help files do, e.g. ((DataRowView)e.Item.DataI tem)? Shouldn't work, but who knows?
about which control do we talk?
What is in your e.Item.DataItem? can you test it for null first. Also wrap the code with try catch block which may give more specific error. The casting looks OK to me, It just depends what you have in your e.Item.DataItem
DataRowView objDataRowView = (DataRowView)(e.Item.DataI tem)
should work.
should work.
vo1d
I can't see why brackets around e.Item.DataItem are required
I can't see why brackets around e.Item.DataItem are required
it seems that the compiler gots problem on cast above 3rd level sometimes.
i got such problems and if you take a look in the DataGridItemEventArgs example, ms is casting with brackets.
i got such problems and if you take a look in the DataGridItemEventArgs example, ms is casting with brackets.
ASKER
i will try these tomorrow
thanks
thanks
you can also try this as well:
DataRowView objDataRowView = e.Item.DataItem as DataRowView;
//Then check if objDataRowView is not null before continuing
DataRowView objDataRowView = e.Item.DataItem as DataRowView;
//Then check if objDataRowView is not null before continuing
the line "Specified cast is not valid" means that your code is trying to convert, "cast", an object from one type to another and the type that it is getting changed to is not a valid type for the original object. Take if you are changing the string "7" to int that converts fine because "7" is a valid number. However "HELLO7" can not be converted, or cast, is not a valid integer so you get "cast is not valid" errors.
What is stored in e.Item.DataItem? Is it an object that can be converted to a DataRowView?
You have one comment that uses this conversion (Int64)(DataRowView)e.Item .DataItem and tries to save it as a DataRowView. This line makes me think that you have a Int64 number in e.Item.DataItem. If thats the case then you should save it to a variable of type "long".
long myvar = Convert.ToInt64(e.Item.Dat aItem);
But like gbzhhu says you will want to test to make sure that the DataItem is not null first or you will get the error here as well.
What is stored in e.Item.DataItem? Is it an object that can be converted to a DataRowView?
You have one comment that uses this conversion (Int64)(DataRowView)e.Item
long myvar = Convert.ToInt64(e.Item.Dat
But like gbzhhu says you will want to test to make sure that the DataItem is not null first or you will get the error here as well.
this is an extract of a msdn example.
as you can see, the cast is in brackets:
void Item_Created(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingIt em ||
e.Item.ItemType == ListItemType.SelectedItem ||
e.Item.ItemType == ListItemType.EditItem)
{
Label1.Text += "<br>" + e.Item.ItemType.ToString() +
" - " + ((DataRowView)e.Item.DataI tem).Row.I temArray[1 ].ToString (); //<-- the cast
}
else
{
Label1.Text += "<br>" + e.Item.ItemType.ToString() ;
}
}
as you can see, the cast is in brackets:
void Item_Created(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingIt
e.Item.ItemType == ListItemType.SelectedItem ||
e.Item.ItemType == ListItemType.EditItem)
{
Label1.Text += "<br>" + e.Item.ItemType.ToString()
" - " + ((DataRowView)e.Item.DataI
}
else
{
Label1.Text += "<br>" + e.Item.ItemType.ToString()
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks