public int InsertOther(clsNews obj) { try { string str = "insert into tblnews(tieude,noidung,ghichu,hinhanh,loaitinid,hot,typeid) values(@tieude,@noidung,@ghichu,@hinhanh,@loaitinid,@hot,@typeid)"; SqlCommand com = new SqlCommand(str, con.getCon()); com.Parameters.AddWithValue("@tieude", obj.TieuDe); com.Parameters.AddWithValue("@noidung", obj.NoiDung); com.Parameters.AddWithValue("@ghichu", obj.GhiChu); com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh); com.Parameters.AddWithValue("@loaitinid", obj.CateNew.Id); com.Parameters.AddWithValue("@hot", obj.Hot); com.Parameters.AddWithValue("@typeid", obj.TypeID); con.openCon(); com.ExecuteNonQuery(); con.closeCon(); return clsDAStaticMethod.getIDIdentity("tblnews"); } catch (Exception) { throw; } finally { con.closeCon(); } }
public void Update(clsNews obj) { try { string str = "Update tblnews set tieude=@tieude,noidung=@noidung,ghichu=@ghichu,hinhanh=@hinhanh,loaitinid=@loaitinid,hot=@hot where id=@id"; if (obj.TypeID > 0) str = "Update tblnews set tieude=@tieude,noidung=@noidung,ghichu=@ghichu,hinhanh=@hinhanh,loaitinid=@loaitinid,hot=@hot,typeid=@typeid where id=@id"; SqlCommand com = new SqlCommand(str, con.getCon()); com.Parameters.AddWithValue("@tieude", obj.TieuDe); com.Parameters.AddWithValue("@noidung", obj.NoiDung); com.Parameters.AddWithValue("@ghichu", obj.GhiChu); com.Parameters.AddWithValue("@hinhanh", obj.HinhAnh); com.Parameters.AddWithValue("@loaitinid", obj.CateNew.Id); com.Parameters.AddWithValue("@hot", obj.Hot); com.Parameters.AddWithValue("@id", obj.Id); if (obj.TypeID > 0) com.Parameters.AddWithValue("@typeid", obj.TypeID); con.openCon(); com.ExecuteNonQuery(); con.closeCon(); } catch (Exception) { throw; } finally { con.closeCon(); } }
public clsNews GetById(int id) { try { clsNews obj = new clsNews(); obj.CateNew = new clsCateNew(); string str = "select * from tblnews where id=@id"; SqlCommand com = new SqlCommand(str, con.getCon()); com.Parameters.AddWithValue("@id", id); con.openCon(); SqlDataReader dr = com.ExecuteReader(); if (dr.Read()) { obj.Id = id; if (dr["tieude"] != DBNull.Value) { obj.TieuDe = dr["tieude"].ToString(); } if (dr["noidung"] != DBNull.Value) { obj.NoiDung = dr["noidung"].ToString(); } if (dr["ghichu"] != DBNull.Value) { obj.GhiChu = dr["ghichu"].ToString(); } if (String.IsNullOrEmpty(dr["hinhanh"].ToString())) { obj.HinhAnh = "~/Styles/Images/no-photo.gif"; } else { obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString(); } if (dr["hot"] != DBNull.Value) { obj.Hot = Convert.ToBoolean(dr["hot"].ToString()); } if (dr["ngaydang"] != DBNull.Value) { obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString()); } if (dr["loaitinid"] != DBNull.Value) { obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString()); obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString()); } if (dr["TypeID"] != DBNull.Value) { obj.TypeID = Convert.ToInt32(dr["TypeID"].ToString()); } } return obj; } catch (Exception) { throw; } finally { con.closeCon(); } }
public List<clsNews> GetAll() { try { List<clsNews> list = new List<clsNews>(); string str = "select * from tblnews where typeid is null order by id DESC"; SqlCommand com = new SqlCommand(str, con.getCon()); con.openCon(); SqlDataReader dr = com.ExecuteReader(); while (dr.Read()) { clsNews obj = new clsNews(); obj.CateNew = new clsCateNew(); if (dr["tieude"] != DBNull.Value) { obj.TieuDe = dr["tieude"].ToString(); } if (dr["noidung"] != DBNull.Value) { obj.NoiDung = dr["noidung"].ToString(); } if (dr["ghichu"] != DBNull.Value) { obj.GhiChu = dr["ghichu"].ToString(); } if (String.IsNullOrEmpty(dr["hinhanh"].ToString())) { obj.HinhAnh = "~/Styles/Images/no-photo.gif"; } else { obj.HinhAnh = "~/Images/HinhAnh/" + dr["hinhanh"].ToString(); } if (dr["hot"] != DBNull.Value) { obj.Hot = Convert.ToBoolean(dr["hot"].ToString()); } if (dr["ngaydang"] != DBNull.Value) { obj.NgayDang = Convert.ToDateTime(dr["ngaydang"].ToString()); } if (dr["loaitinid"] != DBNull.Value) { obj.CateNew.Id = Convert.ToInt32(dr["loaitinid"].ToString()); obj.CateNewId = Convert.ToInt32(dr["loaitinid"].ToString()); } if (dr["id"] != DBNull.Value) { obj.Id = Convert.ToInt32(dr["id"].ToString()); } list.Add(obj); } return list; } catch (Exception) { throw; } finally { con.closeCon(); } }
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { try { if (e.CommandName.Equals("Xoa")) { clsNews news = new clsNews(); news = obj.GetById(Convert.ToInt32(e.CommandArgument)); clsBSFileUpload.DeleteFile(Server.MapPath("~/Images/HinhAnh/") + news.HinhAnh); obj.Delete(Convert.ToInt32(e.CommandArgument)); this.GridView1.DataBind(); } } catch (Exception) { throw; } }
protected void btnSave_Click(object sender, EventArgs e) { try { if (Request.QueryString["action"] != null) { clsNews news = new clsNews(); news.TieuDe = this.txtTieuDe.Text; news.HinhAnh = hfHinhAnh.Value; news.CateNew = new clsCateNew(); news.CateNew.Id = 1; news.GhiChu = this.txtGhiChu.Text; news.NoiDung = this.txtNoiDung.Value; news.Hot = this.chbTinHot.Checked; news.TypeID = Convert.ToInt32(this.ddlType.SelectedValue); string filename = ""; if (Request.QueryString["action"].ToString().Equals("new")) { if (this.fuFile.HasFile) { filename = clsBSFileUpload.SaveFile(this.fuFile, Server.MapPath("~/Images/HinhAnh/")); news.HinhAnh = filename; } obj.InsertByTypeId(news); this.txtTieuDe.Text = ""; this.imgHinhAnh.ImageUrl = ""; this.txtGhiChu.Text = ""; this.txtNoiDung.Value = ""; this.chbTinHot.Checked = false; this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "show", "<script type='text/javascript'>alert('Thêm mới thành công')</script>"); } else { if (Request.QueryString["action"].ToString().Equals("edit")) { if (Request.QueryString["ID"] != null) { int id = Convert.ToInt32(Request.QueryString["ID"]); news.Id = id; if (this.fuFile.HasFile) { clsBSFileUpload.DeleteFile(Server.MapPath("~/Images/HinhAnh/") + this.hfHinhAnh.Value); filename = clsBSFileUpload.SaveFile(this.fuFile, Server.MapPath("~/Images/HinhAnh/")); news.HinhAnh = filename; if (Request.QueryString["typeid"] != null) { news.TypeID=Convert.ToInt32((Request.QueryString["typeid"])); } } obj.Update(news); this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "show", "<script type='text/javascript'>alert('Cập nhật thành công')</script>"); news = obj.GetById(id); this.txtTieuDe.Text = news.TieuDe; this.txtGhiChu.Text = news.GhiChu; this.txtNoiDung.Value = news.NoiDung; this.chbTinHot.Checked = news.Hot; this.imgHinhAnh.ImageUrl = "~/Images/HinhAnh/" + news.HinhAnh; this.hfHinhAnh.Value = news.HinhAnh; this.ddlType.SelectedValue = news.TypeID.ToString(); } } } } this.odsNews.DataBind(); this.GridView1.DataBind(); } catch (Exception) { throw; } }
protected void Page_Load(object sender, EventArgs e) { try { if (Request.QueryString["action"] != null) { if (this.pnInfo.Visible == false) { if (Request.QueryString["action"].ToString().Equals("new")) { this.pnInfo.Visible = true; this.txtTieuDe.Text = ""; this.imgHinhAnh.ImageUrl = ""; this.txtGhiChu.Text = ""; this.txtNoiDung.Value = ""; this.chbTinHot.Checked = false; } else { if (Request.QueryString["action"].ToString().Equals("edit")) { if (Request.QueryString["ID"] != null) { this.pnInfo.Visible = true; int id = Convert.ToInt32(Request.QueryString["ID"]); clsNews news = new clsNews(); news = obj.GetById(id); this.txtTieuDe.Text = news.TieuDe; this.txtGhiChu.Text = news.GhiChu; this.txtNoiDung.Value = news.NoiDung; this.chbTinHot.Checked = news.Hot; this.ddlType.SelectedValue = news.TypeID.ToString(); this.imgHinhAnh.ImageUrl = "~/Images/HinhAnh/" + news.HinhAnh; this.hfHinhAnh.Value = news.HinhAnh; this.ddlCateNew.SelectedValue = news.CateNew.Id.ToString(); } } } } } else { this.pnInfo.Visible = false; } } catch (Exception) { throw; } }
public void Update(clsNews obj) { try { this.obj.Update(obj); } catch (Exception) { throw; } }
public int InsertByTypeId(clsNews obj) { try { return this.obj.InsertByType (obj); } catch (Exception) { throw; } }