C# - "Specified Cast Is Not Valid" Error When Saving LINQ-To-SQL Entity?

Jan 26, 2011

App Details:

C#, ASP.NET MVC, SQL Server 2008 ( Same version & SP level), Linq-To-SQL ORM

I'm trying to diagnose an exception I'm receiving:

"Specified cast is not valid."
at System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager2.TryCreateKeyFromValues(Object[] values, V& v)
at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache2.Find(Object[] keyValues)
at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at Repository.Save()
at etc....

The problem is that this is occurring only on our servers, not on our local development boxes. I've tracked it down to a single property we're modifying:

event.SalesForceId = "701Q0000000AOTIIA4";

If I comment out that line, everything works fine.

I've tried:

1 ) waxing all of the code on the server and redeploying. Same issue.
2 ) Pulling down a copy of the server DB and trying it locally. Works fine.
3 ) Trying another IIS environment we have setup on the same box. Same issue.
4 ) Physically comparing ( using AdeptSQL ) both local and remote schemas. No differences.
5 ) Verifying the datatypes of both columns ( local and remote ) are the same. Also, this column is a FK to another table. I verified that both are of the same data type, down to the collation.

The server is Windows Server 2008 and the local box is Windows 7 x64. Both have all important updates setup.

The only thing I can think of is perhaps since the database and web server are on different boxes that could be an issue? Otherwise, I'm completely stumped.

View 2 Replies


Similar Messages:

C# - LINQ Union With BIT Column Causing Specified Cast Is Not Valid Error?

Mar 8, 2011

I have a complicated join between a few tables but I have managed to replicate the error using linqpad and the small tables below. There are references between the COLNAME column and the YAXIS column and also between COLNAME and XAXIS that is not explicitly defined.

The error is "Specified cast is not valid", which originally I wasted time thinking the problem was converting the data returned to my object in VS 2010, but the error also happens in linqpad with no defined object. It seems insane that a bit column would cause this problem. If I change the column type to a VARCHAR it works fine. If I run the generated SQL from linqpad or sql profiler that also returns fine.

[Code]....

View 1 Replies

ADO.NET :: Specified Cast Is Not Valid During Using LINQ?

Oct 28, 2010

I am having problem, these are the errors:

[Code]....

These are the codes:

[Code]....

View 2 Replies

DataSource Controls :: LINQ VB.net Specified Cast Is Not Valid?

Jun 29, 2010

just needing a 2nd set of eyes looking at this to make sure the error isn't something else other than my LINQ code here. Here's the function class itself:

[Code]....

For some reason it's flagging the 2nd line in the GetJacketByPolicyID function saying the specified cast is not valid. So I'm guessing it's something I'm doing wrong. I'm sure the tblPolicy/tblPolicies class works right since I can create a new instance of a tblPolicy and set a few variables by hand and return it, so that's not it. I've also checked the datarow I'm fetching and there's no null values in the record, so that shouldn't be it either.

View 2 Replies

ADO.NET :: Error: Specified Cast Is Not Valid?

Aug 18, 2010

I've already checked the row returned by my sproc and all the fields match my OcItemConstraintDetails class.
Not sure why I am getting the error below :(Error: Specified cast is not valid.

[Code]....

My code:

[Code]....

View 4 Replies

Specified Cast Is Not Valid Error On Development But Not On Test Server?

Sep 1, 2010

I have a .Net 2 solution with some asp classic pages.I get a specified cast is not valid error when I access the page on my local. Below is the vb function where I get the Error:

Public Function Retrieve(ByVal vntSeqId As String, ByRef vntPart As String, ByRef vntPartKey As String, ByRef vntEntDate As String _
, ByRef vntStatus As String, ByRef vntOrigGblId As String, ByRef vntOrigNetId As String, ByRef vntOrigName As String, ByRef vntOrigPhone As String _ [code]...

At first, I thought the problem is with the source code and we were thinking the source code we have is different with the ones deployed on test and prod servers. But when I tried to upload it on the test server, I don't get the specified cast is not valid error.

View 1 Replies

ADO.NET :: Cast Error When Inserting Record With Linq (to Sql)?

Aug 22, 2010

I'm getting the following error message:

Unable to cast object of type 'System.Data.Linq.DataQuery`1[Username]' to type 'Username'.

I've been searching but can't find a solution. This particular table has 4 columns. The first column is the int indentifier. Column 2 is "username" (varchar50) and is the primary key. Column3 is "userID" (uniqueidentifier) and column 4 is "active" (varchar5)

[Code]....

View 7 Replies

"Specified Cast Is Not Valid" Error Message When Deploy A Web Application

Feb 9, 2011

we have created web application project and on local computer everyting works fine. after doing deployment on our production server we get a error below

Specified cast is not valid.

Description: An unhandled exception occurred during the execution of the current web request. 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.

View 3 Replies

Cast Exception When Try To Insert Entity In Entity Framework (using Code-f)

Feb 9, 2011

I get an cast exception when i am trying to insert an entity in Entity Framework (using code-first). From this code :

public virtual T Insert(T entity)
{
return Context.Set<T>().Add(entity);
}

The cast exception is like "impossible to cast ...Collection'1(Entity) to type (Entity)" I can't figure out why. I am pretty sure ive done everything right. Post entity

public class Post
{
public long PostId { get; private set; }
public DateTime date { get; set; }
[Required]
public string Subject { get; set; }
public User User { get; set; }
public Category Category { get; set; }
[Required]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; private set; }
public Post()
{
Category = new Category();
if (Tags == null)
Tags = new Collection<Tag>();
}................................

View 9 Replies

DataSource Controls :: Entity SQL Error - 'CONVERT' Can't Be Resolved Into Valid Type Or Function

May 19, 2010

I have a SQL which is running good at SQL Server Mgm studio:

"select d.EmployeeID, CONVERT(VARCHAR(10),d.LogTime,111) as Date1, MIN(CONVERT(VARCHAR(8), d.LogTime, 108)) as FirstIn
from LogTable d where d.dwStatus=0 group by d.EmployeeID, CONVERT(VARCHAR(10), d.LogTime,111)"

However, When i put it into my code (Entity SQL):

"select d.EmployeeID, CONVERT(VARCHAR(10),d.LogTime,111) as Date1, MIN(CONVERT(VARCHAR(8), d.LogTime, 108)) as FirstIn
from IGPSiteEntities.AccessLogSet AS d where d.dwStatus=0 group by d.EmployeeID, CONVERT(VARCHAR(10), d.LogTime,111)"

I got error :

'CONVERT' cannot be resolved into a valid type of function. Near simple identifier, line1, column 200.

LogTime is a DateTime format e.g 05/05/2010 14:12:12

I want to select ONLY the Date (e.g 05/05/2010) and select ONLY the time (e.g 14:12:12) so i use CONVERT function.

View 9 Replies

C# - Specified Cast Is Not Valid?

Mar 11, 2011

At the moment I'm trying to keep a certain site (asp.net) alive long enough for its replacement to be build in php. However I don't know much about .net or asp.net.

We are getting this error:

Specified cast is not valid. Description: An unhandled exception occurred during the execution of the current web request. 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.

[Code]....

View 3 Replies

SQL Server :: System.InvalidCastException: Specified Cast Is Not Valid?

Jul 25, 2010

I've got a list of teams which seperate into different leagues. I'm trying to show the user the league that they are in and show it from where they are in the league. This is the error I'm recieving: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: System.InvalidCastException: Specified cast is not valid.Source Error:

[Code]....

Source File: E:WebSitesohuntControlsLeagueWidget.ascx.vb Line: 24 Stack Trace:

[Code]....

This is my page code:

[Code]....

Code-behind:

[Code]....

View 9 Replies

C# - Specific Cast Is Not Valid While Retrieving Scope_identity?

Jun 12, 2010

I am getting exception: "Specific cast is not valid", here is the code

con.Open();
string insertQuery = @"Insert into Tender (Name, Name1, Name2) values ('Val1','Val2','Val3');Select Scope_Identity();";
SqlCommand cmd = new SqlCommand(insertQuery, con);
cmd.ExecuteNonQuery();
tenderId = (int)cmd.ExecuteScalar();

View 4 Replies

SQL Server :: Data Types Uniqueidentifier Specified Cast Is Not Valid?

Nov 16, 2010

have a table that keeps track of customer data that is beyond the scope of the Membership Class, this table I am also using the Membership.CreateUser() generated user ID.When I use my INSERT statement however to create a new row in my table I keep getting the Specified cast is not valid error.This is my SQL Command (The ...refer to other fields)

Dim mySqlCommand As String = "INSERT INTO Customers (UserID, ...) VALUES (@UserID, ...)"
I send it to my Write_Database(mySqlCommand, Membership.GetUser(txtUserName.Text).ProviderUserKey, ...) function which then
I also tried sending he UserID as: CType(Membership.GetUser(txtUserName.Text).ProviderUserKey, SqlGuid)

Neither Works...both cause errors...My Write_Database Function is declared as follows:

Public Function Write_Database(ByVal MysqlQuery As String, ByVal UserID As System.Data.SqlTypes.SqlGuid, ...)

However Visual Studio shows that the error originates in the function calling code...

View 4 Replies

Forms Data Controls :: Specified Cast Is Not Valid.on Formview Checkbox

Aug 12, 2010

There is select and update SQLs in sqldatasource and checkbox in formview which is 2way databinded to an sqlserver bit field type.

it always gives same error

Specified cast is not valid

I excpect nobody posts me an "eval" solution.because update is a need for checkboxes which are attached to a bit field.

a ready-to-use small sample project with DB I attached HERE

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: System.InvalidCastException: Specified cast is not valid.

Source Error:

[Code]....

View 1 Replies

Security :: Specified Cast Is Not Valid - Custom Oledb Role Provider?

Feb 5, 2010

I have a custom oledb role provider that pretty much a cust and past of:

[URL]

except I've used oledb instead of odbc. I'm connecting to an oracle database and the tables have been created with out any issues. now if I go into the Website Administration Tool and try to create a new role I get the error

"Specified cast is not valid."

I can create a role directly in the database (through TOAD) and the role will show up in the .NET WAT, so I know it can make the connection and read the info just fine. I just can't create or delete or modify a role without the above error.

I'm using C# in VS 2008 Pro.

My provider code is as follows:

[Code]....

View 1 Replies

ADO.NET :: LINQ To Entity Error Seeing Relationships Across Multiple Tables?

Jan 24, 2011

I'm a newbie to this so bear with me. From what I've read online LINQ to Entity should see the relationships without having to specify the joins between multiple tables. I have a relationship that goes across 4 tables Employee, EmployeeDepartmentLink, EmployeeProjectLink, and Project. The idea is an employee can be in multiple departments and work on multiple projects with it being specified which department the employee is in for that project.

This is a C# MVC3 program connecting to a SQL database using VS 2010 Express.

I have no problem with running this query to find employees on a project:

[Code]....

Tring to do the reverse, finding projects that an employee worked on results in an error:

[Code]....

Error message: The specified type member 'EmployeeDepartmentLinks' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. So I have 2 questions,

1) Why would it work in one direction and not the other? I've played around with it for a couple days with the same results.

2) Does it matter that my primary key and foreign keys are not named the same since all the relationships are connected in SQL? Ex: in Employee (EmployeePK)--(EmployeeFK) in EmployeeDepartmentLink Trying different stuff in LINQPad I was able to get the 2nd query to work if I specified all the joins which I thought was not neccesary with LINQ to Entity. I converted an SQL query that I made on the server into a LINQ query below which works.

View 5 Replies

Trying To Compare LINQ-to-Entity To Date Results In Error

Mar 14, 2011

I have a simple If..Then like so:

If rhcexists.First.SignatureDate > Date.Today.AddMonths(-6) Then
End If

rhcexists is a simple query to the Entity Model:

Dim rhcexists = From p In dbContracts.Signatures _
Where p.StudentID = CStr(Session("people_code_id")) _
And p.ContractType = "rhc" _
Order By p.ID Descending _
Select p

Problem is that this comparison results in an error:

LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression.

This is occurring on the If clause. Any ideas why this occurring and how I can fix it?

I am using ASP.NET 4, EF 4, and VS 2010 SP1.

View 1 Replies

DataSource Controls :: Linq Update Via .attach() Giving Entity Exists Error?

Jan 5, 2010

I have a "simple" method that takes a Linq entity containing the information I want to update. The method then creates a list of counties the information is to be updated for, and tries to update the information for each county.

So user goes in fills form out selects update All counties, and the information in the form should be updated for all the counties for the member. Here is the code.

[Code]....

The first pass through the foreach runs perfectly, the second time through I get an error: Cannot attach an entity that already exists.

What is best way to work around this?

I have though about putting the foreach loop in my business_logic layer, and have it pass both the newRecord and OldRecord, but figured I would ask, before making the code change.

View 1 Replies

ADO.NET :: LINQ And Cast Row To IEnumerable?

Apr 1, 2011

table1 row = _db.table1.FirstOrDefault(r => r.ReferenceID == refNumber);

Is there anyway to cast row to IEnumerable?

((IEnumerable<table1>)row) - does not work

View 1 Replies

C# - Parameter Is Not Valid Exception When Saving Image In Winform?

Nov 22, 2010

I am getting a parameter is not valid exception..While saving the image

Here is my code to save the image

if (!File.Exists(pictureBox1.Tag.ToString()))
{
Image image = pictureBox1.Image;
image.Save(pictureBox1.Tag.ToString(), ImageFormat.Jpeg);
}
else
{
string delStr = pictureBox1.Tag.ToString();
pictureBox1.Image.Dispose();
File.Delete(delStr);
Image image = pictureBox1.Image;
image.Save(delStr, ImageFormat.Jpeg);
pictureBox1.Image = Image.FromFile(delStr);
}

View 2 Replies

C# - LinqTOsql Returning A "specified Cast Not Valid" Exception At Runtime?

Nov 16, 2010

I have two linqTOsql entities that has a parent and child relationship, one to many. I'm running into an issue that when I retrieve the parent record, I am unable to loop through the related records in the child table.This code fails:

public string test()
{
string output;

[code]...

View 1 Replies

C# - Specified Cast Is Not Valid. Enum / Try Also Enum.Parse But Didnt Work?

Sep 28, 2010

whats wrong with this code, i try also Enum.Parse but didnt work.

public enum RoleNames
{
Administrator,

[code]...

View 1 Replies

Unable To Cast A Partial Class Based On LINQ To SQL Object

Mar 28, 2011

I have a database table called Animals which I drag on to my Linq to SQL DBML file.

This creates me a class called Animal.

I then create a class to extend it, like so:

public partial class Animal{
//my extra code here
}

But what I also want to do is have a class like this:

public class Zebra : Animal{
//even more extra code here
}

The trouble is I get an InvalidCastException from my repository, when I do this:

Animal a = dataContext.Animals.Where(c=>c.id.Equals(id)).SingleOrDefault();
return (Zebra)a;

I've stripped back my classes to the point where they're completely empty in an attempt to work why it doesn't work. To no avail.

This casting process did used to work in the non-LINQ-2-SQL project that I'm switching to MVC/LINQ.

View 1 Replies

Exclude Some Fields Of An Entity When Saving?

Feb 8, 2011

All my entities have the following properties:

public virtual DateTime CreatedDate {get; set;}
public virtual string CreatedBy { get; set; }
public virtual DateTime UpdatedDate { get; set; }
public virtual string UpdatedBy { get; set; }

I've implemented a EventListener (IPreUpdateEventListener, IPreInsertEventListener) so that I can fill these properties before inserting/updating my entity.I do not load my entity just before saving cause I've got all my fields in a view (id, version). The only thing I would like to avoid to put in hidden fields (of my view) are CreatedDate and CreateBy.Since I fill these fields only once, when the entity is created the first time, I was wondering if there's a way to exclude them when I update (only update) my entitiy?!

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved