Specified cast is not valid error on datagrid

  • Thread starter Thread starter .Net Sports
  • Start date Start date
N

.Net Sports

IN a datagrid code behind, I'm getting a "Specified cast is not valid"
error


price = (Decimal)(rowData["totalamount"]);

which was initialized as:

Decimal price;

????
..netsports
 
..netsports

Set a break on that line. When the debugger hits that line, place the
following in the watch window of the debugger:

rowData["totalamount"]

What is the type of the value that is returned? Based on that, you
should be able to determine the problem.

Hope this helps.
 
Nicholas, I get a object 'rowData' doesnt have an indexer error when
looking at this in the debugger watch window.

here is the coding for the rowData (the formatting in this reply
message may be slightly off):

private void dglvboard_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataRowView rowData;
Decimal price;
Decimal newssum;
System.Web.UI.WebControls.Literal totalamountLabel = null;
System.Web.UI.WebControls.Literal NewsLabel = null;

System.Web.UI.WebControls.Literal totalLabel = null;
System.Web.UI.WebControls.Literal newstotalLabel = null;
// check the type of item that was databound and only take action if
it
// was a row in the datagrid
switch (e.Item.ItemType)
{
case ListItemType.AlternatingItem:
case ListItemType.EditItem:
case ListItemType.Item:
case ListItemType.SelectedItem:
// get the data for the item being bound
rowData = (DataRowView)(e.Item.DataItem);

// get the value for the list price and add it to the sum


// get the control used to display the list price
// NOTE: This can be done by using the FindControl method of the
// passed item because ItemTemplates were used and the anchor
// controls in the templates where given IDs. If a standard
// BoundColumn was used, the data would have to be accessed
// using the cellscollection (e.g.
e.Item.Cells(1).controls(1)
// would access the label control in this example.


// now format the list price in currency format

// get the value for the discounted price and add it to the sum

price = (Decimal)(rowData["totalamount"]);
 
Hi,

Hi,

do this:

object o = rowData["totalamount"] ;

Decimal d = (Decimal)o;

put a breakpoint in the latter, and add o in the watch, see the type being
reported.

you could do a similar thing with rowData = (DataRowView)(e.Item.DataItem);
as you did not post where you bind the data, I cannot be sure the casting is
working fine (should be though ).


cheers
 
Back
Top