3


I need to get the updated row's first column value. However when I run query Update ClaimDetails set sStatus='False' OUTPUT inserted.slno as Slno where inVoiceNo='******' and sStatus='True'
in Management Studio it returns the right value. But when I try to get the value using Executescalar() it returns null

My code:

 bool isupdated = false;
        int modified=0;
        try
        {

            string updateqry = "Update ClaimDetails set sStatus=@sStatus OUTPUT inserted.slno as Slno where inVoiceNo=@inVoiceNo and sStatus='True'";
            SqlCommand cmd = new SqlCommand(updateqry, con);
            cmd.Parameters.AddWithValue("@sStatus", sStatus);
            cmd.Parameters.AddWithValue("@inVoiceNo", inVoiceNo);
            connect();
            if (cmd.ExecuteNonQuery() > 0)
            {
                isupdated = true;
                 //modified = (int)cmd.ExecuteScalar();
                object a = cmd.ExecuteScalar();
                if (a != null)
                    modified = Convert.ToInt32(a);

            }
        }
        catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        finally { disconnect(); }
        return modified;

When I used modified = (int)cmd.ExecuteScalar(); it gave me an exception error so I used object

1
  • " it gave me an exception error" what exception did it give you? May 27, 2017 at 13:02

3 Answers 3

1

The code is executing the same UPDATE command twice, once with ExecuteNonQuery (which discards the scalar result and returns the count of rows affected) and again with ExecuteScalar. Because of the hard-coded 'True' in the WHERE clause, the second invocation of same command will never update a row when the provided sStatus value is 'False'. The scalar result will always be null in that case.

I think you can refactor the code as below to get the desired result.

object a = cmd.ExecuteScalar();
if (a != null)
{
    isupdated = true;
    modified = Convert.ToInt32(a);
}
0
1

The only time that ExecuteScalar returns null is if no rows are returned. If one row is returned and the value is null, then DbNull.Value is returned instead.

So: no rows are matching. Check what @inVoiceNo is, and whether or not it exists in the table you're running against. Common reasons for confusion:

  • case sensitivity in the database
  • unicode vs ascii/code-page values
  • char vs varchar, nchar vs nvarchar
  • running against the wrong data
2
  • I ran same query in Management Studio, and it gave me right result ie; `44019' which is right
    – mark
    May 27, 2017 at 13:19
  • @mark so now you need to debug it. Forget everything you think you know about the values of the parameters and the state of the database - and indeed, which database you think you're talking to. Check everything May 27, 2017 at 13:21
0

in case someone want the actual number of records on update not just handling the null value ,, in the stored procedure our DB developer set return @@ROWCOUNT [typo] instead SELECT @@ROWCOUNT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.