Hi, I am using a C# application that interacts with a TSQL stored procedure.

The problem is that my SqlDataReader property .RecordsAffected always returns -1 regardless of whatever kind of select, update, or delete query I run.

Here is my relevant code:

SqlDataReader myReader = null;

 SqlCommand command = new SqlCommand("CSP_CT_233", myConnection);
myReader = command.ExecuteReader();

while (myReader.Read())
        {

           blah blah blah //this stuff works fine
        }

myReader.Close();
Console.Writeline([B]myReader.RecordsAffected.ToString()[/B]);

Recommended Answers

All 6 Replies

Try not closing the reader before you ask whats effected

Ok it works now ok, but when there is only 1 row affected, it will return 2, otherwise it works fine

Sounds odd, sure theres only 1 row?

yes I'm sure I'm double checking the results with SQL Server

The problem seems to be when my my update where field differs from my set field

(this is updating one record)

update table set field1=1 where field1=0 //returns 1
update table set field1=1 where field2=0 //returns 2

anyone seen this before?

no, only when theres more than 1 line with field2=0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.