Solved

Specified cast is not valid.

Posted on 2006-07-10
16
394 Views
Last Modified: 2010-04-16
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.DataItem; ===> highlighted
Line 179:                        string currentDeduction = (string)objDataRowView[1].ToString();
Line 180:  


fullcode
is

=================

public void dgCompensation_ItemDataBound(Object Sender, DataGridItemEventArgs e)
            {
   
                  if (e.Item.ItemType == ListItemType.EditItem)
                  {
                        DataRowView objDataRowView = (DataRowView)e.Item.DataItem;
                        string currentDeduction = (string)objDataRowView[2].ToString();
   
                        DropDownList ctlDropDownList = (DropDownList)e.Item.FindControl("DeductDropDownList");
                        //
                        
                        if (ctlDropDownList != null)
                        {
                        
                              ctlDropDownList.Items.Add("E");
                        }
                        //
                        ctlDropDownList.SelectedIndex = ctlDropDownList.Items.IndexOf(ctlDropDownList.Items.FindByText(currentDeduction));
                  }
   
            }
            

===================
0
Comment
Question by:SirReadAlot
  • 6
  • 4
  • 2
  • +3
16 Comments
 

Author Comment

by:SirReadAlot
ID: 17072785
sorry, this is a datagrid  edit button.


thanks
0
 

Author Comment

by:SirReadAlot
ID: 17072839
infact what does the msg mean,?
Specified cast is not valid
0
 

Author Comment

by:SirReadAlot
ID: 17072956
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
0
 

Author Comment

by:SirReadAlot
ID: 17072992
maybe this line needs to be converted to string or int
 DataRowView objDataRowView = (DataRowView)e.Item.DataItem;
0
 

Author Comment

by:SirReadAlot
ID: 17073023
do i need somthing like this

         DataRowView objDataRowView = (Int64)(DataRowView)e.Item.DataItem;
0
 
LVL 2

Expert Comment

by:shy_talk
ID: 17074087
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.DataItem)? Shouldn't work, but who knows?
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17074112
about which control do we talk?
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 17074192
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:vo1d
ID: 17074226
DataRowView objDataRowView = (DataRowView)(e.Item.DataItem)
should work.
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 17074265
vo1d

I can't see why brackets around e.Item.DataItem are required
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17074810
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.
0
 

Author Comment

by:SirReadAlot
ID: 17075560
i will try these tomorrow

thanks
0
 
LVL 10

Expert Comment

by:tpwells
ID: 17075581
you can also try this as well:

 DataRowView objDataRowView =  e.Item.DataItem as DataRowView;

//Then check if objDataRowView is not null before continuing
0
 
LVL 10

Expert Comment

by:tpwells
ID: 17075711
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.DataItem);

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.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17075979
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.AlternatingItem ||
          e.Item.ItemType == ListItemType.SelectedItem ||
          e.Item.ItemType == ListItemType.EditItem)

      {  
         Label1.Text += "<br>" + e.Item.ItemType.ToString() +
                        " - " + ((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString();  //<-- the cast
      }

      else

      {
         Label1.Text += "<br>" + e.Item.ItemType.ToString();
      }
 
   }
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17078158
What is e.Item.DataItem.GetType().Name?

Bob
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now