urgent object cannot be cast from dbnull to other types

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.
jemigossayeAsked:
Who is Participating?
 
mastooConnect With a Mentor Commented:
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
 
mastooCommented:
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
 
jemigossayeAuthor Commented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
mastooCommented:
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
 
jemigossayeAuthor Commented:
can you please show an example or a fragment code

thanks
0
 
mastooCommented:
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
 
jemigossayeAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.