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++)
{
开发者_JAVA百科 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.
Consider changing the keyword UPGRADE
in your SQL to UPDATE
, perhaps that's it.
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.");
Should your sql statement not be UPDATE instead of UPGRADE?
using (var command = new SqlCommand("UPDATE [Users] SET [Role] = @role WHERE [UserID] = @userId", connection))
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.
精彩评论