473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)(rowDa ta["totalamoun t"]);

which was initialized as:

Decimal price;

????
..netsports

Nov 17 '05 #1
3 3026
..netsports

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

rowData["totalamoun t"]

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.co m

".Net Sports" <ba********@cox .net> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
IN a datagrid code behind, I'm getting a "Specified cast is not valid"
error
price = (Decimal)(rowDa ta["totalamoun t"]);

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_ItemD ataBound(object sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
DataRowView rowData;
Decimal price;
Decimal newssum;
System.Web.UI.W ebControls.Lite ral totalamountLabe l = null;
System.Web.UI.W ebControls.Lite ral NewsLabel = null;

System.Web.UI.W ebControls.Lite ral totalLabel = null;
System.Web.UI.W ebControls.Lite ral 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.ItemTyp e)
{
case ListItemType.Al ternatingItem:
case ListItemType.Ed itItem:
case ListItemType.It em:
case ListItemType.Se lectedItem:
// 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)(rowDa ta["totalamoun t"]);

Nov 17 '05 #3
Hi,

Hi,

do this:

object o = rowData["totalamoun t"] ;

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.goo glegroups.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_ItemD ataBound(object sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
DataRowView rowData;
Decimal price;
Decimal newssum;
System.Web.UI.W ebControls.Lite ral totalamountLabe l = null;
System.Web.UI.W ebControls.Lite ral NewsLabel = null;

System.Web.UI.W ebControls.Lite ral totalLabel = null;
System.Web.UI.W ebControls.Lite ral 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.ItemTyp e)
{
case ListItemType.Al ternatingItem:
case ListItemType.Ed itItem:
case ListItemType.It em:
case ListItemType.Se lectedItem:
// 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)(rowDa ta["totalamoun t"]);

Nov 17 '05 #4

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

Similar topics

5
3960
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 is "newstotal"), but I'm getting an error (System.InvalidCastException: Specified cast is not valid) from the newstotal line: price =...
4
1305
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 request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. The stack trace...
10
1777
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 the code. Exception Details: System.InvalidCastException: Specified cast is not valid. Line 176. Source Error:
3
10500
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 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...
0
1604
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. ----------------------------------------------------------- Server VS.NET 2003 SQLServer 2000 IIS 66.0
1
1396
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 is "newstotal"), but I'm getting an error (System.InvalidCastException: Specified cast is not valid) from the newstotal line: price =...
1
1715
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 ChkDelete_OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs) DataGrid1_DeleteCommand(sender, e)
2
3517
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 current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details:...
2
2496
by: thithi | last post by:
Server Error in '/Assignment' Application. -------------------------------------------------------------------------------- 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...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.