You are on page 1of 7

ĐĂNG KÝ THÀNH VIÊN

ASP.NET Code
<table align="center" cellpadding="5" cellspacing="0">
<tr>
<td>
Mã thành viên</td>
<td>
:</td>
<td>
<asp:TextBox runat="server" id="txtMaKH" Width="196px" />
</td>
</tr>
<tr>
<td>
Mật khẩu</td>
<td>
:</td>
<td>
<asp:TextBox runat="server" id="txtMatKhau" Width="280px" />
</td>
</tr>
<tr>
<td>
Họ và tên</td>
<td>
:</td>
<td>
<asp:TextBox Text='<%# Bind("HoTen") %>' runat="server"
id="txtHoTen"
Width="279px" />
</td>
</tr>
<tr>
<td>
Giới tính</td>
<td>
:</td>
<td>
<asp:CheckBox runat="server"
id="chkGioiTinh" Text="Nam ?" /></td>
</tr>
<tr>
<td>
Ngày sinh</td>
<td>
:</td>
<td>
<asp:TextBox runat="server" id="txtNgaySinh" />
</td>
</tr>
<tr>
<td>
Địa chỉ</td>
<td>
:</td>
<td>
<asp:TextBox runat="server" id="txtDiaChi" Width="279px" />
</td>
</tr>
<tr>
<td>
Điện thoại</td>
<td>
:</td>
<td>
<asp:TextBox runat="server" id="txtDienThoai" Width="276px" />
</td>
</tr>
<tr>
<td>
Email</td>
<td>
:</td>
<td>
<asp:TextBox runat="server" id="txtEmail" Width="277px" />
</td>
</tr>
<tr>
<td>
Hình ảnh</td>
<td>
:</td>
<td>
<asp:FileUpload ID="fulHinh" runat="server" />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
<asp:Button ID="btnDangKy" runat="server" Text="Đăng ký"
onclick="btnDangKy_Click" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblThongBao" runat="server" ForeColor="Red"
Text="Vui lòng điền thông tin vào form"></asp:Label>
</td>
</tr>
</table>

Code behind
Nhấp đúp vào nút Đăng ký để viết code cho btnDangKy_Click

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net.Mail;

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


{
protected void Page_Load(object sender, EventArgs e)
{

protected void btnDangKy_Click(object sender, EventArgs e)


{
if (fulHinh.HasFile) // upload hình
{
String Path = Server.MapPath("Hinh/KhachHang/" + fulHinh.FileName);
fulHinh.SaveAs(Path);
}

// Insert vào bảng KhachHang


try
{
String connectionString = "Data Source=.;Initial
Catalog=eStore20;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
String sql = "INSERT INTO KhachHang(MaKH, MatKhau, HoTen, GioiTinh,
NgaySinh, DiaChi, DienThoai, Email, Hinh, HieuLuc, VaiTro) VALUES
(@MaKH,@MatKhau,@HoTen,@GioiTinh,@NgaySinh,@DiaChi,@DienThoai,@Email,@Hinh,@HieuLuc
,@VaiTro)";
SqlCommand command = new SqlCommand(sql, connection);

command.Parameters.AddWithValue("@MaKH", txtMaKH.Text);
command.Parameters.AddWithValue("@MatKhau", txtMatKhau.Text);
command.Parameters.AddWithValue("@HoTen", txtHoTen.Text);
command.Parameters.AddWithValue("@GioiTinh", chkGioiTinh.Checked);
command.Parameters.AddWithValue("@NgaySinh", Calendar1.SelectedDate);
command.Parameters.AddWithValue("@DiaChi", txtDiaChi.Text);
command.Parameters.AddWithValue("@DienThoai", txtDienThoai.Text);
command.Parameters.AddWithValue("@Email", txtEmail.Text);
command.Parameters.AddWithValue("@Hinh", fulHinh.FileName);
command.Parameters.AddWithValue("@HieuLuc", false);
command.Parameters.AddWithValue("@VaiTro", 0);

command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
}

You might also like