protected void UserList_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = UserList.Rows[e.RowIndex]; UserInfo info = new UserInfo(); info.FirstName = (row.Cells[1].Controls[0] as TextBox).Text; info.LastName = (row.Cells[2].Controls[0] as TextBox).Text; info.Login = (row.Cells[3].Controls[0] as TextBox).Text; info.Question = (row.Cells[4].Controls[0] as TextBox).Text; info.Answer = (row.Cells[5].Controls[0] as TextBox).Text; info.Categories = (row.Cells[6].FindControl("UserCatigories") as DropDownList).SelectedValue; info.UserId = int.Parse((row.Cells[6].FindControl("UserId") as Label).Text); try { Administraion.UpdateUser(info); } catch (Exception ex) { MessageError.Text = ex.Message; MessageError.Visible = true; } UserList_RowCancelEditing(sender, new GridViewCancelEditEventArgs(e.RowIndex)); }
internal static void UpdateUser(UserInfo info) { SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString); string str = "update users set firstname = @firstname, lastname = @lastname, login = @login, question = @question, answer = @answer, categories = @categories where user_id = @id"; SqlCommand cmd = new SqlCommand(str, con); cmd.Parameters.AddWithValue("firstname", info.FirstName); cmd.Parameters.AddWithValue("lastname", info.LastName); cmd.Parameters.AddWithValue("login", info.Login); cmd.Parameters.AddWithValue("question", info.Question); cmd.Parameters.AddWithValue("answer", info.Answer); cmd.Parameters.AddWithValue("categories", info.Categories); cmd.Parameters.AddWithValue("id", info.UserId); try { string login = LoGiN.LoginForID(info.UserId); string usRole=""; if(Roles.GetRolesForUser(login)[0].Length > 0) usRole = Roles.GetRolesForUser(login)[0]; con.Open(); cmd.ExecuteNonQuery(); if (usRole != info.Categories) { if(!string.IsNullOrEmpty(usRole)) Roles.RemoveUserFromRole(login, usRole); Roles.AddUserToRole(login, info.Categories); } } catch (Exception) { throw new ApplicationException("Не удалось обновить информация о пользователе"); } finally { con.Close(); } }