How to check if stored procedure returns data or not?


How to check if stored procedure returns data or not?



I have a stored procedure GetLastRecordId() in SQL Server which returns the last record id in controller of ASP.NET MVC project. But the problem is that when there is no data in the table, it shows a result like below so I can't check against null:


GetLastRecordId()



enter image description here



My stored procedure:


ALTER PROCEDURE [dbo].[GetLastRecordId]
AS
BEGIN
SET NOCOUNT ON;

SELECT
CustomerCreditAppId, CompanyName
FROM
tblname
WHERE
CustomerCreditAppId = (SELECT MAX(CustomerCreditAppId) FROM tblname)
END





Expand the results view and re-upload your screenshot maybe? Also, copy/paste the stored procedure and add corresponding tag?
– Rafalon
Jul 2 at 14:01






I cannot see anything in this screenshot :-)
– חיים פרידמן
Jul 2 at 14:04





I have expanded the result and just uploaded new screenshot @Rafalon
– chhaya bhudiya
Jul 2 at 14:08






The debugger may be your problem here. Evaluating a Linq query in the debugger can change the results that you get when it's executed in code. Note the " results ... cannot be enumerated more than once" message.
– D Stanley
Jul 2 at 16:03





This is a dreadful idea. You set your PK field to be auto-incremented, not attempt to set it yourself (if multiple users are making a request at the same time, they will all get the same value)
– Stephen Muecke
Jul 3 at 4:05




1 Answer
1



You can use Any to check if the Enumerable is not empty:



spdata.Any()





That is giving me below exception message. 'spdata.Any()' threw an exception of type 'System.InvalidOperationException' bool {System.InvalidOperationException}
– chhaya bhudiya
Jul 2 at 14:10





Is there an InnerException?
– D Stanley
Jul 2 at 15:58


InnerException





Could you show us the content of GetLastRecordId please ?
– Habib Rahmoun
Jul 3 at 8:18






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?