urgent object cannot be cast from dbnull to other types

Posted on 2006-06-23
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.
Question by:jemigossaye
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 21

Expert Comment

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.

Author Comment

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

LVL 21

Expert Comment

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.
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now


Author Comment

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

LVL 21

Expert Comment

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

Your remote proc looks like this:

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

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


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


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

Author Comment

ID: 16984409

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
LVL 21

Accepted Solution

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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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