invalidcastexception

System.InvalidCastException: Specified cast is not valid Only on certain Machines

We have code that casts an object to a short. type.BusinessAreaID = (short)caType.credit_assessment_biz_areaReference.EntityKey.EntityKeyValues[0].Value; The object has the value of 2. This code (unit test) works on the PC of the developer that created the code. But we get a invalid cast exception on the build server and on another d...

Convert enum from object to long without a try block?

Follow along in my test source. What is a good way to get a value from a enum object? Must support long. I am attempting without a try/catch block. enum ELong: long { a = 0x100000000 }; enum ENormal { a = 25 } var l = (object) ELong.a; var n = (object)ENormal.a; //will cast into the correct size int ii =...

InvalidCastException in converter

This is my class that implements IValueConverter: [ValueConversion(typeof(int), typeof(Priority))] public class PriorityConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (Priority) (int) value; } ...

Why am I getting InvalidCastException passing a string to a VariantType parameter?

I have: Protected Sub SepInsert(ByVal mriId As VariantType, ByVal aeId As VariantType, ByVal absId As VariantType) ... End Sub and want to call it with a DropDownList selection like this: Protected Sub cmdNewPrelinkedMri_Click(ByVal sender As Object, ...

Windows Service Webbrowser object invalid cast exception error

Hi all, I'm having a bit of trouble with a Windows Service webbrowser object. It's attempting to load in values of username and password to a site but keeps failing and throwing the following error: System.InvalidCastException: Specified cast is not valid. at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation() a...

InvalidCastException when parsing dates in VB.NET

I'm trying to Parse dates entered into a TextBox into a DateTime value but I keep getting an exception in the TryParseExact method. What I'm trying to do is: DateTime.TryParseExact(tbAddDate.Text.Trim, "yyMMdd", New CultureInfo("sv-SE"), DateTimeStyles.None, row.Date) This throws an InvalidCastException with the message "Conversion fr...

InvalidCastException in DataGridView

(Using VS 2010 Beta 2 - .Net 4.0 B2 Rel) I have a class, MyTable, derived from BindingList where S is a struct. S is made up of several other structs, for example: public class MyTable<S>:BindingList<S> where S: struct { ... } public struct MyStruct { public MyReal r1; public MyReal r2; public MyReal R1 {get{...} set...

Windows Service Fails on Launch

I'm trying to write a windows service. It installs fine, but fails when I run it with the following exception. I've searched for the string "MyNewProgramService", but I can't find any conversions that would throw this error. I've also added try/catch blocks to a bunch of code with custom exception handling without finding where this ex...

Cast error on SQLDataReader

My site is using enterprise library v 5.0. Mainly the DAAB. Some functions such as executescalar, executedataset are working as expected. The problems appear when I start to use Readers I have this function in my includes class: Public Function AssignedDepartmentDetail(ByVal Did As Integer) As SqlDataReader Dim reader As SqlDataRe...

c# InvalidCastException

Hello. I try to use this code : webBrowser.Document.GetElementById("login").SetAttribute("value", "user"); It work great but not when i use it in a new thread. I get an InvalidCastException. What can I do ? ...

LINQ in VB InvalidCastException

Hi, I run into this problem System.InvalidCastException: Unable to cast object of type 'System.Data.Linq.DataQuery1[Student]' to type 'System.Collections.Generic.List1[Student]' When I debug this function: Public Shared Function SearchStudent(ByVal firstname As String) As List(Of Student) Dim db As New DemoDataContext() Dim...

InvalidCastException of a Activator.CreateInstance object during an installation procedure

Hi I have the following procedure private static IMyInterface OpenInstance( string assemblyPath, string classType, string assemblyName, out AppDomain domainInstall) { IMyInterface interface = null; AppDomainSetup domaininfo = new AppDomainSetup(); domaininfo.ApplicationBase = assemblyPath; domainInstall = AppDomain.C...

How can I avoid a InvalidCastException in .NET?

Is there a way to check whether a C# cast will be successful? In some cases; based on how a rendered page is put together; inheriting from different Master Pages, some casts will work and others will not. I am wondering how I can check to see if a cast will be successful or if I just have to catch and handle an invalid cast exception. ...

Why does the tooltip for Convert.ToDateTime() say that it always throws?

The tooltip displayed for Convert.ToDateTime() says that it always throws System.InvalidCastException. Why? ...

[Solved]WPF C# Expand actual TreeNode invalid cast

Hi, i have a TreeView which is binded trough a HierarchicalDataTemplate. Im listing there a List which contains a list of a directories and files. private void getDirectoryList() { using (FileOperations fileOP = new FileOperations()) { this.DokumentBrowser.ItemsSource = fileOP.list_directory(rpath); //Li...

ASP.NET MVC: The model item passed into the dictionary is of type 'ClassA', but this dictionary requires a model item of type 'ClassA'.

I'm getting a peculiar exception using the Html.RenderPartial method. ASP.NET MVC seems to be unable to an object of type ClassA to an object of type ClassA. I was wondering if anyone knows what's going on. Here's a little more background info. I'm having the following hierarchy in place: public interface IInterface { string Name {...

Invalid casting exception on my simple application.

Here's the gist of it: There is a list of classes at my school. Each class belongs to a single Career. I'm creating the create a new class form, eg. Math, Social Studies, etc. On the form I want a dropdownlist to generate with the available Careers we offer. Unable to cast object of type 'System.Collections.Generic.List`1[System.We...

InvalidCastException casting object to its own type

I'm currently completely baffled by the problem I'm having. I'm writing a plug-in for another application that provides a public .NET API. I've created a class named Room and I am using a PropertyGrid to allow users to see and edit the properties of the Room instances. A few properties are restricted to a set of standard values. Thus I a...