-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateUser.aspx.cs
More file actions
140 lines (120 loc) · 5.2 KB
/
Copy pathCreateUser.aspx.cs
File metadata and controls
140 lines (120 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SW
{
public partial class CreateUser : System.Web.UI.Page
{
//string connectionString = "Data Source=JARVIS;Initial Catalog=SW;Integrated Security=True";
string connectionString = "Data Source=DC-SRV02\\SQLEXPRESS;Initial Catalog=SW;User ID=SWweb; Password=ScottWilson2025";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CarregarUsuarios();
if (Session["UserCategory"] == null || Session["UserCategory"].ToString() != "Admin")
{
Response.Redirect("~/Login.aspx");
}
string query = "SELECT Id, Name FROM Categories";
DataTable categories = Database.ExecuteQuery(query);
ddlCategory.DataSource = categories;
ddlCategory.DataTextField = "Name";
ddlCategory.DataValueField = "Id";
ddlCategory.DataBind();
}
}
protected void btnCreateUser_Click(object sender, EventArgs e)
{
string username = txtUsername.Text.Trim();
string password = txtPassword.Text.Trim();
int categoryId = Convert.ToInt32(ddlCategory.SelectedValue);
string query = "INSERT INTO Users (Username, Password, CategoryId) VALUES (@Username, @Password, @CategoryId)";
SqlParameter[] parameters = {
new SqlParameter("@Username", username),
new SqlParameter("@Password", password),
new SqlParameter("@CategoryId", categoryId)
};
int rowsAffected = Database.ExecuteNonQuery(query, parameters);
if (rowsAffected > 0)
{
lblMessage.Text = "User created successfully.";
txtUsername.Text = string.Empty;
txtPassword.Text = string.Empty;
}
else
{
lblMessage.Text = "Error creating user.";
}
}
// Método para carregar os usuários no GridView
private void CarregarUsuarios()
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Users", con);
DataTable dt = new DataTable();
da.Fill(dt);
gvUsuarios.DataSource = dt;
gvUsuarios.DataBind();
}
}
// Método para entrar no modo de edição de um usuário
protected void gvUsuarios_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
gvUsuarios.EditIndex = e.NewEditIndex;
CarregarUsuarios();
}
// Método para cancelar a edição
protected void gvUsuarios_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
gvUsuarios.EditIndex = -1;
CarregarUsuarios();
}
// Método para atualizar as informações do usuário
protected void gvUsuarios_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
// Obtém o Id do usuário que está sendo editado
int userId = Convert.ToInt32(gvUsuarios.DataKeys[e.RowIndex].Value.ToString());
// Obtém os novos valores inseridos pelo admin
string novoNome = ((TextBox)gvUsuarios.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
string novoEmail = ((TextBox)gvUsuarios.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
// Atualiza no banco de dados
using (SqlConnection con = new SqlConnection(connectionString))
{
string query = "UPDATE Users SET Username = @Nome WHERE Id = @Id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Nome", novoNome);
cmd.Parameters.AddWithValue("@Email", novoEmail);
cmd.Parameters.AddWithValue("@Id", userId);
con.Open();
cmd.ExecuteNonQuery();
}
// Sai do modo de edição
gvUsuarios.EditIndex = -1;
CarregarUsuarios();
}
// Método para excluir um usuário
protected void gvUsuarios_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
// Obtém o Id do usuário que está sendo excluído
int userId = Convert.ToInt32(gvUsuarios.DataKeys[e.RowIndex].Value.ToString());
// Deleta o usuário do banco de dados
using (SqlConnection con = new SqlConnection(connectionString))
{
string query = "DELETE FROM Users WHERE Id = @Id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", userId);
con.Open();
cmd.ExecuteNonQuery();
}
// Recarrega a lista de usuários
CarregarUsuarios();
}
}
}