Скачиваний:
1
Добавлен:
12.04.2025
Размер:
8.45 Mб
Скачать

Приложение д

Листинг – Файл EditProfile.aspx.cs

using System;

using System.Data.SqlClient;

namespace WebApplication3

{

public partial class WebForm6 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

LoadUserData();

}

}

protected void LoadUserData()

{

if (Session["UserId"] == null)

{

lblMessage.Text = "Вы не авторизованы. Пожалуйста, войдите в систему.";

return;

}

int clientId = int.Parse(Session["UserId"].ToString());

string connectionString = "Data Source=localhost;Initial Catalog=travel;Integrated Security=True";

string query = "SELECT Name_C, Surname_C, Patronymic_C, DOB_C, Phone_C FROM Client WHERE Client_ID = @ClientID";

try

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@ClientID", clientId);

connection.Open();

SqlDataReader reader = command.ExecuteReader();

if (reader.Read())

{

txtName.Text = reader["Name_C"].ToString();

txtSurname.Text = reader["Surname_C"].ToString();

txtPatronymic.Text = reader["Patronymic_C"].ToString();

txtDOB.Text = Convert.ToDateTime(reader["DOB_C"]).ToString("yyyy-MM-dd");

txtPhone.Text = reader["Phone_C"].ToString();

}

else

{

lblMessage.Text = "Ошибка загрузки данных.";

}

}

}

catch (Exception ex)

{

lblMessage.Text = "Ошибка загрузки данных: " + ex.Message;

}

}

protected void btnSave_Click(object sender, EventArgs e)

{

if (Session["UserId"] == null)

{

lblMessage.Text = "Вы не авторизованы. Пожалуйста, войдите в систему.";

return;

}

int clientId = int.Parse(Session["UserId"].ToString());

string connectionString = "Data Source=localhost;Initial Catalog=travel;Integrated Security=True";

string query = @"

UPDATE Client

SET Name_C = @Name,

Surname_C = @Surname,

Patronymic_C = @Patronymic,

DOB_C = @DOB,

Phone_C = @Phone

WHERE Client_ID = @ClientID";

try

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@Name", txtName.Text);

command.Parameters.AddWithValue("@Surname", txtSurname.Text);

command.Parameters.AddWithValue("@Patronymic", txtPatronymic.Text);

command.Parameters.AddWithValue("@DOB", Convert.ToDateTime(txtDOB.Text));

command.Parameters.AddWithValue("@Phone", txtPhone.Text);

command.Parameters.AddWithValue("@ClientID", clientId);

connection.Open();

int rowsAffected = command.ExecuteNonQuery();

if (rowsAffected > 0)

{

lblSuccess.Text = "Данные успешно обновлены!";

lblMessage.Text = "";

}

else

{

lblMessage.Text = "Ошибка обновления данных.";

}

}

}

catch (Exception ex)

{

lblMessage.Text = "Ошибка сохранения данных: " + ex.Message;

}

}

}

}

Соседние файлы в предмете Проектирование информационных систем