Object reference not set to an instance of an object

A

Assimalyst

hi,

I'm trying to get a count of duplicate entried in a sql server table.

I have a section of code that is throwing an error:

SqlCommand cmd = new SqlCommand ("proc_DuplicateSurgery", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@sgyName",
SqlDbType.VarChar,50,"strSurgeryName"));
cmd.Parameters.Add(new SqlParameter("@sgyPostcode",
SqlDbType.VarChar,10,"strPostcode"));
SqlParameter objReturnParam = cmd.Parameters.Add(new
SqlParameter("@Duplicates", SqlDbType.Int));
objReturnParam.Direction = ParameterDirection.ReturnValue;

//
// If duplicates occur change FK's to match and delete them
//
if ((int)objReturnParam.Value > 1)
{
.. . .

it appears to run through but when it trys to run the if
((int)objReturnParam.Value > 1) line it returns the error "Object
reference not set to an instance of an object".

Here's the stored procedure too:

CREATE PROCEDURE proc_DuplicateSurgery
(@sgyName varchar(50) = NULL,
@sgyPostcode varchar(10) = NULL,
@Duplicates int = 0
)

AS

SET @Duplicates =(SELECT COUNT(*) FROM tblReferringDoctorSurgery
WHERE sgyName = @sgyName AND sgyPostcode = @sgyPostcode)
RETURN @Duplicates
GO


Any ideas what the problem might be?

Thanks.
 
W

William \(Bill\) Vaughn

Okay, but I don't see where the SqlCommand is being executed...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top