Alexandria
Alexandria

Reputation: 183

How to know if row is updated

I need to stop users over writting someone elses approval on the application. I have put in Where IS NULL which works to stop it overwritting the data but i need to then display a label which says record not updated as already approved.

How do I know if sql query updated or didnt and get that information so I can do an If statement.

If row updated then 
    label.text = "data approved" 
else   
    label.text = "You can not approve this as it's already approved"


CheckBox ckb = (CheckBox)sender;
        GridViewRow row = (GridViewRow)ckb.Parent.Parent;           //Get row selected
        int idx = row.RowIndex;

        //Get activity number ID for update query
        Label lblDHActivityNoApp = (Label)row.Cells[0].FindControl("lblDHActivityNoApp");
        string Number = lblDHActivityNoApp.Text.ToString();

        string connectionString = ConfigurationManager.ConnectionStrings["PlusConnectionString"].ConnectionString;
        string UpdateSql = "UPDATE Activities SET Status = @Status, ApprovedBy=@ApprovedBy, ApprovedDate=@ApprovedDate WHERE ApprovedBy IS NULL and (ActivityNo=@No)";
        //Create SQL connection
        SqlConnection con = new SqlConnection(connectionString);

        //Create SQL Command And Sql Parameters
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = UpdateSql;

        SqlParameter Status = new SqlParameter("@Status", SqlDbType.VarChar, 40);
        Status.Value = "Approved";
        cmd.Parameters.Add(Status);

        SqlParameter ApprovedDate = new SqlParameter("@ApprovedDate", SqlDbType.VarChar, 50);
        DateTime myDateTime = DateTime.Now; ;
        ApprovedDate.Value = myDateTime.ToString("dd/MM/yy HH:mm:ss");
        cmd.Parameters.Add(ApprovedDate);

        SqlParameter ApprovedBy = new SqlParameter("@ApprovedBy", SqlDbType.VarChar, 40);
        ApprovedBy.Value = (string)Session["Username"];
        cmd.Parameters.Add(ApprovedBy);

        SqlParameter No = new SqlParameter("@No", SqlDbType.VarChar, 50);
        No.Value = Number;
        cmd.Parameters.Add(No);

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            gbDHdetailsApp.DataBind();  //Reloads gridview with checked data       
        }

        catch (SqlException ex)
        {
            string errorMessage = "Not approved, please contact support";
            errorMessage += ex.Message;
            throw new Exception(errorMessage);
        }

        finally
        {
            con.Close();
        }

Upvotes: 1

Views: 87

Answers (2)

RvdK
RvdK

Reputation: 19790

ExecuteNonQuery return the number of rows affected. If this is 0, you can throw an Exception.

Upvotes: 4

Rahul
Rahul

Reputation: 77846

ExecuteNonQuery() returns number of rows affected and has a return type INT and thus you can do like

int rowsAffected = cmd.ExecuteNonQuery();

if(rowsAffected > 0)
{
  // do something
}

Upvotes: 1

Related Questions