473,394 Members | 1,841 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,394 software developers and data experts.

Specified cast is not valid error on datagrid

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

Nov 17 '05 #1
3 3015
..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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

".Net Sports" <ba********@cox.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 17 '05 #2
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"]);

Nov 17 '05 #3
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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
".Net Sports" <ba********@cox.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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"]);

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which...
4
by: .Net Sports | last post by:
I'm getting a "Specified cast is not valid" error that does not point to the line with the code in error, but instead :An unhandled exception was generated during the execution of the current web...
10
by: Arjen | last post by:
Hello, 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...
3
by: PK9 | last post by:
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...
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....
1
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which...
1
by: planet | last post by:
Hi, I wish I can solve this problem not having recode the sub. Here is goes: I got this error' specified cast is not valid' at the second line. from the code Sub...
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...
2
by: thithi | last post by:
Server Error in '/Assignment' Application. -------------------------------------------------------------------------------- Specified cast is not valid. Description: An unhandled exception...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.