Ivan Stoyanov
Ivan Stoyanov

Reputation: 5482

System.Exception: Incorrect syntax I can't find the problem

So I have a grid view with checkboxes in it. This is the code behind the page.

protected void BtnApproveUsers_Click(object sender, EventArgs e)
    {

        var num = new List<int>();

        try
        {
            for (var i = 0; i< GvApproveUser.Rows.Count; i++)
            {
                var row = GvApproveUser.Rows[i];
                var isChecked = ((CheckBox) row.FindControl("ChbSelect")).Checked;

                if (isChecked)
                {
                    num.Add(System.Convert.ToInt32(GvApproveUser.Rows[i].Cells[1].Text));
                    Authentication.ApproveUser(num, GvApproveUser.Rows.Count);
                }
            }
           throw new Exception("The registration forms were approved.");
        }
        catch (Exception exception)
        {

           throw new Exception(exception.Message);
        }

    }

And this is the method.

public static void ApproveUser(List<int> userIds, int rowCount)
    {
        using (var connection = Utils.Database.GetConnection())
            try
            {
                for (var i = 0; i < rowCount; i++)
                {
                    using (var command = new SqlCommand("UPGRADE [Users] SET [Role] = @role WHERE [UserID] = @userId", connection))
                    {
                        command.Parameters.AddWithValue("@role", "User");
                        command.Parameters.AddWithValue("@userId", userIds[i]);

                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception exception)
            {

                throw new Exception(exception.Message);
            }

    }

And this is the exception:

Incorrect syntax near 'Role'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Incorrect syntax near 'Role'.

Source Error:

Line 52: { Line 53:
Line 54: throw new Exception(exception.Message); Line 55: } Line 56:

I can't find the problem. Pls help.

Upvotes: 1

Views: 451

Answers (4)

Michael Ciba
Michael Ciba

Reputation: 561

Off topic but why on earth are you throwing an exception which appears to be a means of informing users that the registration forms have been approved?

throw new Exception("The registration forms were approved.");

Upvotes: 3

Iain
Iain

Reputation: 2550

Firstly, do you mean UPDATE rather than UPGRADE in your sql?

Secondly if you change your exception block as follows:

catch (Exception exception)
{
    throw;
}

then you will preserve the original stack trace giving you an exact line number which caused the error.

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166396

Should your sql statement not be UPDATE instead of UPGRADE?

using (var command = new SqlCommand("UPDATE [Users] SET [Role] = @role WHERE [UserID] = @userId", connection))

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391346

Consider changing the keyword UPGRADE in your SQL to UPDATE, perhaps that's it.

Upvotes: 5

Related Questions