Solved

urgent object cannot be cast from dbnull to other types

Posted on 2006-06-23
7
388 Views
Last Modified: 2008-02-20
hi all ,
i a have a situation when tested in qa it workerd fine cause there were no linked servers involved but whern it is realsed in production it starts showing this error object cannot be cast from dbnull to other types in a part where i want to retrun an identity column using select scope_identity() or Select @@identity in my stored procs.
0
Comment
Question by:jemigossaye
  • 4
  • 3
7 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 16969344
Both those can return null if there hasn't been an identity generated on the current connection.  Presumably, you are casting the return value into an int in your c# - which leads to the cast error as an int can't be null.
0
 

Author Comment

by:jemigossaye
ID: 16969453
the idenitity is being generated and inseretd in the table the thing is select scope idntity or @@identity doesn't work with remote servers and my storedprocs are in local server and the data is inserted in to a thired  party server which don't have any access to except to store data. i am looking for a work arround for this cause i have no idea what to do


thanks
0
 
LVL 21

Expert Comment

by:mastoo
ID: 16969620
Ah, I missed the mention of linked server.  I've seen other people having this problem.  There's an ugly unreliable work-around, but the real fix is to do the sql in a proc executed on the remote server and return identity as an output parameter.
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.

 

Author Comment

by:jemigossaye
ID: 16969694
can you please show an example or a fragment code

thanks
0
 
LVL 21

Expert Comment

by:mastoo
ID: 16970132
There might be an easier way, but basically...

Your remote proc looks like this:

create procedure myremoteproc( @input1 int, @myid int output )
begin

  insert into sometbl( name1 ) values ( @input1 )
  select @myid = scope_identity()

end

I think you can run this directly from code using something like this from c#:

[LINKEDSERVER].database.owner.myproc

and the input and output parameters (the output will be your identity) in the usual c# fashion.
0
 

Author Comment

by:jemigossaye
ID: 16984409
Hello,

I still need help casue the remote server is a third party server and  they are not letting us do a single thing on that remote server  

Can anybody tell or rather HELP ME on another way to solve this
0
 
LVL 21

Accepted Solution

by:
mastoo earned 500 total points
ID: 16985150
If there are some other values inserted that are unique, you could query for the id based on those values.  Otherwise, you're left with something ugly like querying for the last ident on that table or max value, both of which might be somebody else's record inserted shortly after yours.
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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

11 Experts available now in Live!

Get 1:1 Help Now