Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 26th, 2011, 11:41 AM
Authorized User
 
Join Date: Aug 2006
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default SqlCommandExecuteNonQuery() issue

Hi
I've got a stored proc which I've tested that updates or inserts records based on a few simple parameters. I've also tested it that it doesn't insert or update if an invalid I'd id is tried for example

Everything works fine in query anaylser - it says the number of records affected, e.g. 1, 0, etc. Plus I've got no select statement in my stored proc either. But whatever I do, when I execute the storedproc with the SqlCommand.ExecuteNonQuery() and examine the result it's always "1". Even if no records are affected.

I've tried using set nocount off and selecting @@recordcount but to no avail. I'm probably missing something really small, so your help would be appreciated thanks!

SPROC:

BEGIN
IF(@CommunicationId = 0)
BEGIN
INSERTINTO
Communication
(GroupId, PersonId, IsPrimaryContactMethod, CommunicationValue, CommunicationType, Note, CommunicationStatus)
VALUES
(@GroupId, @PersonId, @IsPrimaryContactMethod, @CommunicationValue, @CommunicationType, @Note, @CommunicationStatus)
END
ELSE
BEGIN
UPDATE
Communication
SET
GroupId = @GroupId,
PersonId = @PersonId,
IsPrimaryContactMethod = @IsPrimaryContactMethod,
CommunicationValue = @CommunicationValue,
CommunicationType = @CommunicationType,
Note = @Note,
CommunicationStatus = @CommunicationStatus
WHERE
CommunicationId = @CommunicationId
END
END

Code:

publicbool UpdateCommunication(Communication communication)
{
int i = 0;
using (SqlCommand sqlCmd = _cmdHelper.GetCommand("Communication_UPDATE"))
{
sqlCmd.Parameters.Add(
"@CommunicationId", SqlDbType.Int).Value = communication.CommunicationId;
sqlCmd.Parameters.Add(
"@GroupId", SqlDbType.Int).Value = communication.GroupId;
sqlCmd.Parameters.Add(
"@PersonId", SqlDbType.Int).Value = communication.PersonId;
sqlCmd.Parameters.Add(
"@IsPrimaryContactMethod", SqlDbType.Bit).Value = communication.IsPrimaryContactMethod;
sqlCmd.Parameters.Add(
"@CommunicationValue", SqlDbType.VarChar,255).Value = communication.CommunicationValue;
sqlCmd.Parameters.Add(
"@CommunicationType", SqlDbType.TinyInt).Value = communication.CommunicationType;
sqlCmd.Parameters.Add(
"@Note", SqlDbType.VarChar,255).Value = communication.Note;
sqlCmd.Parameters.Add(
"@CommunicationStatus", SqlDbType.TinyInt).Value = communication.CommunicationStatus;
i = sqlCmd.ExecuteNonQuery();
}
return i == 1;
}
 
Old October 26th, 2011, 12:35 PM
Authorized User
 
Join Date: Aug 2006
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Worked it out. I was changing the communication scope variable inside the using statment during testing - this wasn't changing the actual value of the variable once inside the using statement. All fine now.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Not sure Excel issue or Batch file issue - Application.Workbooks("Book2.xls").Close justinferns Excel VBA 3 October 4th, 2011 03:09 PM
Rsync issue ajit Linux 1 January 17th, 2008 06:37 AM
Reports Issue Brendan Bartley Access 1 December 19th, 2007 10:26 PM
Another issue islandtiu BOOK: Beginning ASP 3.0 1 February 14th, 2005 11:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.