Sql UPDATE command has no effect and throws no error only in web page, same code in console project works

StackOverflow https://stackoverflow.com/questions/4078271

  •  28-09-2019
  •  | 
  •  

Question

I have a .NET 3.5 web application which has a set of classes handling entity persistence. The INSERT and SELECT prepared commands work. However the UPDATE command never works (no database record is updated) and it never throws an exception. Also it always returns 1, so even the command.ExecuteNonQuery() returns a valid number of affected rows.

Now when I take the same entity class and run it in a test console application, the prepared statement works.

This is really frustrating and a complete show stopper. I have even tried this in Mono on Ubuntu, Mac OS X and Windows. All perform the same (no records updated in web app, insert works, and console app works).

    public void Store()
    {
        SqlConnection conn = new SqlConnection(this.connection_string);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        int i = 0;
        if (this.id == 0)
        {
            // INSERT a new RECORD
            cmd.CommandText = "INSERT INTO [VtelCenter] ([CommonName],[Location]) VALUES (@commonname, " +
                "@location)";
            cmd.Parameters.Add("@commonname", SqlDbType.NVarChar, this.CommonName.Length);
            cmd.Parameters["@commonname"].Value = this.CommonName;
            cmd.Parameters.Add("@location", SqlDbType.NVarChar, this.Location.Length);
            cmd.Parameters["@location"].Value = this.Location;
        }
        else
        {
            // UPDATE an existing RECORD
            cmd.CommandText = "UPDATE [VtelCenter] SET [CommonName] = @commonname, [Location] = @location, " +
                "[Status] = @status WHERE [ID] = @id";
            //cmd.CommandText = "EXEC [dbo].[UpdateVtelCenter] @id, @commonname, @location, @status";
            cmd.Parameters.Add("@commonname", SqlDbType.NVarChar, this.commonName.Length);
            cmd.Parameters["@commonname"].Value = this.CommonName;
            cmd.Parameters.Add("@location", SqlDbType.NVarChar, this.Location.Length);
            cmd.Parameters["@location"].Value = this.Location;
            cmd.Parameters.Add("@status", SqlDbType.Int);
            cmd.Parameters["@status"].Value = (int) this.Status;
            cmd.Parameters.Add("@id", SqlDbType.Int);
            cmd.Parameters["@id"].Value = this.Id;

        }
        cmd.Prepare();
        i = cmd.ExecuteNonQuery();            
        if (i != 1) 
            throw new Exception(string.Format("Incorrect number of records stored: {0}, should be 1.", i));
        conn.Close();
    }
Was it helpful?

Solution

A couple of thoughts to help with debugging this.

  1. Look for any UPDATE triggers (either AFTER or INSTEAD OF) on the VtelCenter table that might be changing your expected results.
  2. Run a SQL Profiler trace on your database server so you can capture the query being passed in on that side.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top