1. Một số hàm hệ thống
a) Hàm kết nối cớ sở dữ liệu using System;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public class connectDB {public connectDB() {
}
// Mở kết nối CSDL
public void openConnection(ref SqlConnection Cnn) {
Cnn = null;
String n_ConnectionString =
ConfigurationManager.ConnectionStrings["database"].To String();
Cnn = new SqlConnection(n_ConnectionString);
Cnn.Open();
} }
b) Các hàm kiểm tra điều kiện using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class function {
connectDB connect = new connectDB();
public function() {
}
public Int32 TotalRow(ref SqlConnection Cnn, String sql){
Int32 row = 0;
connect.openConnection(ref Cnn);
using( SqlCommand cmd =Cnn.CreateCommand()) {
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql ;
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read()) {
row = Int32.Parse(dr["dem"].ToString());
} }
Cnn.Close();
return row;
}
public bool checkString(String s) {
String [] split = s.Split(new Char []
{' ', ',', '.', ':','-'});
if (split.Length > 1) {
return false;
}
return true;
}public bool testImage(String s) {
bool bl=false;
String[] allowedExtensions =
{ "gif", "png", "jpeg", "jpg" };
if (s.IndexOf('.') == -1) {
bl = false;
} else {
String[] split = s.Split(new Char[] { '.' });
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (split[split.Length-1] == allowedExtensions[i]) bl = true;{
} }
} return bl;
}
public bool checkExtension(FileUpload fileupload) {String fileExtension = System.IO.Path.GetExtension
(fileupload.FileName).ToLower();
String[] allowedExtensions =
{".gif",".png",".jpeg", ".jpg"};
for (int i = 0; i < allowedExtensions.Length; i++) {
if (fileExtension == allowedExtensions[i]) {
return true;
} }
return false;
} }
2. Modul đăng ký tài khoản
using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Dangky : System.Web.UI.Page {
function fun = new function();
protected void Page_Load(object sender, EventArgs e) {
lbLoi.Text = "";
lblThongbao.Text = "";
btnDangky.Enabled = false;
CheckBox1.AutoPostBack = true;
txtUserName.AutoPostBack = true;
}
Private string n_ConnectionString =
ConfigurationManager.ConnectionStrings["database"].ToString();
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
using (SqlConnection Cnn = new
SqlConnection(n_ConnectionString)) {
using (SqlCommand cmdUser = new SqlCommand("spUsers_GetByPK", Cnn))
{
cmdUser.CommandType = CommandType.StoredProcedure;
cmdUser.Parameters.Add("@PK_iUserID",SqlDbType.Int).Value = 0;
using (SqlDataAdapter daUser = new SqlDataAdapter(cmdUser)) {
using (DataSet dsUser = new DataSet()) {
daUser.Fill(dsUser, "Users");
string sUserName = txtUserName.Text;
string sFilter = " sUserName=" + "'" + sUserName + "'";
DataTable dtRole = dsUser.Tables["Users"];
DataRow[] rows = dtRole.Select(sFilter);
if (rows.Length > 0)
args.IsValid = false;
else
args.IsValid = true;
}//??DataAdapter tu mo*
} } } }
protected void btnDangky_Click(object sender, EventArgs e) {
{
lbLoi.Text = "Tên đăng nhập không được có ký tự đặc biệt";
} else {
if (txtUserName.Text.Trim().Length < 6)
lbLoi.Text = "Tên đăng nhập phải nhiều hơn 5 ký tự!";
else {
try {
if (!Page.IsValid) return;
using(SqlConnection Cnn=new SqlConnection(n_ConnectionString)) {
int iRet = 0;
using (SqlCommand Cmd = Cnn.CreateCommand()) {
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.CommandText = "spUsers_Insert ";
Cmd.Parameters.AddWithValue("@sUserName", txtUserName.Text);
Cmd.Parameters.AddWithValue("@sPassword", txtPassword.Text);
Cmd.Parameters.AddWithValue("@sEmail", txtEmail.Text);
Cnn.Open();
iRet = Cmd.ExecuteNonQuery();
if (iRet != 0)
lblThongbao.Text = "Bạn đã đăng ký thành công!";
Reset();
Cnn.Close();
} } }
catch (Exception ex) {
Response.Write(ex.Message);
} } } }
protected void Reset() {
txtUserName.Text = "";
txtPassword.Text = "";
txtEmail.Text = "";
CheckBox1.Checked = false;
}
protected void Check(object sender, EventArgs e) {
if (CheckBox1.Checked) {
btnDangky.Enabled = true;
} else {
btnDangky.Enabled = false;
} } }
3. Modul tạo Album
public partial class Taoalbum : System.Web.UI.Page {
connectDB connect = new connectDB();
RoleProviders role = new RoleProviders();
SqlConnection Cnn = null;
private String sUserName;
private String sPassword;
private Int32 ID_Alb;
protected void Page_Load(object sender, EventArgs e) {
txtCreate.AutoPostBack = true;
if (Session["sUserName"] == null) {
Response.Redirect(".//Default.aspx");
} else { try {
ID_Alb=Convert.ToInt32(Request.QueryString["AID"].ToString());
}
catch (Exception ex) { } }
}
protected void btnTao_Click(object sender, EventArgs e)
{ try {
this.ID_Alb =
Convert.ToInt32(Request.QueryString["AID"].ToString());
}
catch (Exception ex) {
Response.Write(ex.ToString());
}
connect.openConnection(ref Cnn);
if (role.IdentifyForAlbum(ref Cnn, txtCreate.Text.Trim())) {
lbloi.Text = "Tên Album đã được dùng";
} else {
using (SqlCommand Cmd = Cnn.CreateCommand()) {
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.CommandText = "spAlbum_Insert";
Cmd.Parameters.AddWithValue("@sAlbTitle",
txtCreate.Text.Trim());
Cmd.Parameters.AddWithValue("@sAlbContent",
txtContent.Text.Trim());
Cmd.Parameters.AddWithValue("@sCreateAlbum",
DateTime.Now.ToString("dd/MM/yyyy"));
if(ID_Alb >0 && checkIDAlbum(ref Cnn,this.ID_Alb)) {
Cmd.Parameters.AddWithValue("@SK_iAlbum",ID_Alb);
}
else Cmd.Parameters.AddWithValue("@SK_iAlbum",DBNull.Value);
Cmd.ExecuteNonQuery();
}
int UserID = role.getUserId(ref Cnn,
(String)Session["sUsername"], (String)Session["sPassword"]);
int iLastAlbumID = getLastAlbumId();
insert(UserID, iLastAlbumID);
Cnn.Close();
txtCreate.Text = "";
txtContent.Text = "";
lbloi.Text = "Tạo thành công";
Response.Redirect(".//chitiet.aspx");
} }
protected void btnHuy_Click(object sender, EventArgs e) {
Response.Redirect(".//chitiet.aspx");
}
protected void txtCreate_TextChanged(object sender,EventArgse) {
if (txtCreate.Text.Trim().Length == 0) {
lbloi.Text = "Tên album không được trống";
} }
public bool insert(int userid, int albumid) {
connect.openConnection(ref Cnn);
using (SqlCommand cmd = Cnn.CreateCommand()) {
cmd.CommandType = CommandType.Text;
cmd.CommandText = " insert into
tblUser_Album(FK_iUserID,FK_iAlbumID) values(" + userid + ","
+ albumid + ")";
int i = cmd.ExecuteNonQuery();
Cnn.Close();
if (i > 0) return true;
else return false;
} }
public int getLastAlbumId() {
int iLastAlbumId = 1;
connect.openConnection(ref Cnn);
using (SqlCommand cmd = Cnn.CreateCommand()) {
cmd.CommandType = CommandType.Text;
cmd.CommandText = " select PK_iAlbumID from tblAlbum ";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
iLastAlbumId = Convert.ToInt32(dr["PK_iAlbumID"].ToString());
}
dr.Close();
}
return iLastAlbumId;
}
private bool checkIDAlbum(ref SqlConnection Cnn, int IDAlbum) {
bool bl;
String sql = "select * from tblAlbum where PK_iAlbumID =" + this.ID_Alb + "";
using (SqlCommand cmd = Cnn.CreateCommand()) {
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows) bl = true;
else bl = false;
dr.Close();
}
return bl;
} }
4. Modul cập nhật ảnh using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
public partial class Nhapanh : System.Web.UI.Page {
connectDB connect = new connectDB();
RoleProviders role = new RoleProviders();
function fun = new function();
SqlConnection Cnn = null;
private int ID_Alb;
protected void Page_Load(object sender, EventArgs e) {
if (Session["sUserName"] == null) {
Response.Redirect(".//Default.aspx");
} else {
ID_Alb =
Convert.ToInt32(Request.QueryString["AID"].ToString());
} }
protected void Up_Click(object sender, EventArgs e) {
if (txtTitle.Text.Trim().Length == 0) {
txtTitle.Text = "";
lbloi.Text = "Tên ảnh không được trống";
} else {
if (FileUploadImage.FileName != "") {
if (!fun.checkExtension(FileUploadImage)) {
lbloi.Text = "File nhập vào không là file ảnh!";
} else {
String sLocationPath="Upload/"+ FileUploadImage.FileName;
FileUploadImage.SaveAs(Server.MapPath("Upload/") +
FileUploadImage.FileName);
System.Drawing.Image img =
System.Drawing.Image.FromFile(Server.MapPath
("Upload/")+ FileUploadImage.FileName);
connect.openConnection(ref Cnn);
using (SqlCommand Cmd = Cnn.CreateCommand()) {
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@FK_iAlbumID", this.ID_Alb);
Cmd.Parameters.AddWithValue("@sImgTitle", txtTitle.Text.ToString());
Cmd.Parameters.AddWithValue("@sImgDate",
DateTime.Now.ToString("dd/MM/yyyy"));
Cmd.Parameters.AddWithValue("@sImgImage",
FileUploadImage.FileName.ToString());
Cmd.Parameters.AddWithValue("@sCreateImage",
txtDateCreate.Text.ToString()+"/"+
txtMonthCreate.Text.ToString()+"/"+
txtYearCreate.Text.ToString());
Cmd.Parameters.AddWithValue("@iHeight",
img.Size.Height.ToString());
Cmd.Parameters.AddWithValue("@iWidth",
img.Size.Width.ToString());
Cmd.Parameters.AddWithValue("@iSize",
FileUploadImage.PostedFile.ContentLength / 1024);
Cmd.ExecuteNonQuery();
lbloi.Text = "Thành công";
Reset();
Cnn.Close();
} } } } }
protected void Reset() {
txtTitle.Text = "";
txtDateCreate.Text = "";
txtMonthCreate.Text = "";
txtYearCreate.Text = "";
}
protected void Huy_Click(object sender, EventArgs e) {
txtTitle.Text = "";
Response.Redirect(".//Hienthianh.aspx");
}