0% found this document useful (0 votes)
9 views6 pages

Nah

Uploaded by

trananhduc6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Nah

Uploaded by

trananhduc6996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

<add name="conn1"

connectionString="Data Source=ADMIN-PC\
SQLEXPRESS;Initial Catalog=baikiemtra1;Integrated
Security=True" />

public class AccountController : Controller{


private DataAccess dataAccess = new DataAccess();
public ActionResult Register(){
return View();
}
[HttpPost]
public ActionResult Register(KhachHang KH) {
if (ModelState.IsValid) {
dataAccess.Register(KH.TaiKhoan, KH.MatKhau);
return RedirectToAction("Login");
}
return View(KH);
}
public ActionResult Login(){
return View();
}
[HttpPost]
public ActionResult Login(string taikhoan, string matkhau)
{
if (dataAccess.Login(taikhoan, matkhau)){
Session["TaiKhoan"] = taikhoan;
return RedirectToAction("Index", "Sach");
}
ViewBag.ErrorMessage = "Tài khoản hoặc mật khẩu
không đúng!";
return View();
}
public ActionResult Logout(){
Session.Clear();
return RedirectToAction("Index", "Sach");
}
}
@using (Html.BeginForm("Login", "Account",
FormMethod.Post)){
<div>
<label for="TaiKhoan">Tài khoản</label>
<input type="text" name="TaiKhoan" />
</div>
<div>
<label for="MatKhau">Mật khẩu</label>
<input type="password" name="MatKhau" />
</div>
<button type="submit">Đăng Nhập</button>
}
@if (!string.IsNullOrEmpty(ViewBag.ErrorMessage)){
<div>@ViewBag.ErrorMessage</div>
}

@model WebApplication2.Models.KhachHang

@using (Html.BeginForm("Register", "Account",


FormMethod.Post)){
<div>
@Html.LabelFor(m => m.TaiKhoan)
@Html.TextBoxFor(m => m.TaiKhoan)
</div>
<div>
@Html.LabelFor(m => m.MatKhau)
@Html.PasswordFor(m => m.MatKhau)
</div>
<button type="submit">Đăng Ký</button>
}
public class SachController : Controller{
public ActionResult Index(){
var tensach = Request.QueryString["tensach"];
DataAccess dataAccess = new DataAccess();
DataTable dt = new DataTable();
if (!string.IsNullOrWhiteSpace(tensach)){
dt = dataAccess.GetData("SELECT * FROM dbo.sach
WHERE tensach LIKE '%" + tensach + "%'");

if (dt.Rows.Count == 0){
ViewBag.Message = "Không tồn tại sản phẩm
này.";
}
}
else
dt = dataAccess.GetData("SELECT * FROM
dbo.sach");
return View(dt);
}
public ActionResult Details(int id){
DataAccess dataAccess = new DataAccess();
DataTable dt = dataAccess.GetData("SELECT * FROM
dbo.sach WHERE MaSach = " + id);
return View(dt.Rows[0]);
}
}

@model System.Data.DataRow
<h2>Chi tiết thông tin thuốc</h2>
<p><img src="@Url.Content("~/Content/Images/" +
@Model["AnhBia"])" style="max-width: 200px; width :
100%" class="img-fluid" /></p>
<p>Mã sách: @Model["MaSach"]</p>
<p>Tên sách: @Model["TenSach"]</p>
<p>Giá: @Model["GiaBan"]</p>
<p>Ngày cập nhật: @Model["NgayCapNhat"]</p>
<p>Số lượng: @Model["SoLuongTon"]</p>
<form action="/sach">
<input type="submit" value="Quay lại" />
</form>
@model System.Data.DataTable
<h2>Danh sách sách</h2>
<form action="/sach/index" method="get">
<input type="text" name="tensach" value=""
placeholder="Nhập tên sách" />
<input type="submit" value="Tìm kiếm" />
</form>

@if (ViewBag.Message != null){


<p>@ViewBag.Message</p>
}
else{
<table class="table table-bordered mt-2">
<thead>
<tr>
<th>Tên sách</th>
<th>Giá</th>
<th>Ảnh</th>
</tr>
</thead>
<tbody>
@if (Model != null && Model.Rows.Count > 0) {
foreach (System.Data.DataRow item in
Model.Rows) {
<tr>
<td><a
href="/sach/details/@item["MaSach"]"> @item["TenSach"]
</a></td>
<td>@item["GiaBan"]</td>
<td><img
src="@Url.Content("~/Content/Images/" +
@item["AnhBia"])" style="max-width: 200px; width : 100%"
class="img-fluid" /></td>
</tr>
}
}
</tbody>
</table>
}
public class DataAccess{
private string conn1 =
System.Configuration.ConfigurationManager.ConnectionStrin
gs["conn1"].ConnectionString;
public DataTable GetData(string query){
DataTable dt = new DataTable();
using (SqlConnection conn = new
SqlConnection(conn1)){
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
}
public void ExecuteQuery(string query){
using (SqlConnection conn = new
SqlConnection(conn1)) {
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
}
}

public void Register(string tk, string mk){


string query = "INSERT INTO KhachHang (TaiKhoan,
MatKhau) VALUES (@TaiKhoan, @MatKhau)";
using (SqlConnection conn = new
SqlConnection(conn1)){
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@TaiKhoan", tk);
cmd.Parameters.AddWithValue("@MatKhau", mk);
conn.Open();
cmd.ExecuteNonQuery();
}
}

public bool Login(string tk, string mk){


string query = "SELECT COUNT(*) FROM KhachHang
WHERE TaiKhoan = @TaiKhoan AND MatKhau = @MatKhau";
using (SqlConnection conn = new
SqlConnection(conn1)){
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@TaiKhoan", tk);
cmd.Parameters.AddWithValue("@MatKhau", mk);
conn.Open();
int count = (int)cmd.ExecuteScalar();
return count > 0;
}
}
}

namespaces: new[] {
"WebApplication2.Areas.Admin.Controllers" }

You might also like