protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { news_categories n = (news_categories)Session["nn"]; txtid.Text = Convert.ToString(n.news_cat_id); txtname.Text = n.news_cat_name; txtdescription.Text = n.news_cat_description; DataBind(); } }
protected void Sua_Click(object sender, CommandEventArgs e) { if (e.CommandName == "sua") { int m = Convert.ToInt16(e.CommandArgument); news_categories n = data.Layra1danhmuc(m); Session["nn"] = n; Response.Redirect("news_categoriesEdit.aspx"); } }
//Thêm danh mục tin tức public void ThemNewCategories(news_categories n) { con.Open(); String strSql = "insert into news_categories values(@name,@description)"; SqlCommand cmd = new SqlCommand(strSql, con); cmd.Parameters.AddWithValue("name", n.news_cat_name); cmd.Parameters.AddWithValue("description", n.news_cat_description); cmd.ExecuteNonQuery(); con.Close(); }
//Cập nhật danh mục tin tức public void Capnhatdanhmuc(news_categories n) { con.Open(); string strSql = "update news_categories set news_cat_name=@name,news_cat_description=@description where news_cat_id=@news_cat_id"; SqlCommand cmd = new SqlCommand(strSql, con); cmd.Parameters.AddWithValue("name", n.news_cat_name); cmd.Parameters.AddWithValue("description", n.news_cat_description); cmd.Parameters.AddWithValue("news_cat_id", n.news_cat_id); cmd.ExecuteNonQuery(); con.Close(); }
protected void btnThem_Click(object sender, EventArgs e) { try { news_categories n = new news_categories(); n.news_cat_name = txtname.Text; n.news_cat_description = txtdescription.Text; data.ThemNewCategories(n); msg.ForeColor = System.Drawing.Color.Red; msg.Text = "Thêm thành công"; } catch (Exception ex) { msg.Text = "Không thêm được,có lỗi:" + ex.Message; } }
protected void btnSua_Click(object sender, EventArgs e) { try { news_categories n = new news_categories(); n.news_cat_id = int.Parse(txtid.Text); n.news_cat_name = txtname.Text; n.news_cat_description = txtdescription.Text; data.Capnhatdanhmuc(n); msg.Text = "Bạn cập nhật thành công."; } catch (Exception ex) { msg.Text = "Có lỗi:" + ex; } }
//Phần danh mục tin tức ----------------------------------------------------------------------------------------- // Lấy ra danh sách danh mục tin tức public List <news_categories> getNewsCategories() { List <news_categories> li = new List <news_categories>(); string strSql = "select*from news_categories"; con.Open(); SqlCommand cmd = new SqlCommand(strSql, con); SqlDataReader rd = cmd.ExecuteReader(); while (rd.Read()) { news_categories n = new news_categories(); n.news_cat_id = (int)rd["news_cat_id"]; n.news_cat_name = (string)rd["news_cat_name"]; n.news_cat_description = (string)rd["news_cat_description"]; li.Add(n); } con.Close(); return(li); }
//Lấy ra 1 danh mục tin tức public news_categories Layra1danhmuc(int news_cat_id) { List <news_categories> li = new List <news_categories>(); string strSql = "select*from news_categories where news_cat_id=@news_cat_id"; con.Open(); SqlCommand cmd = new SqlCommand(strSql, con); cmd.Parameters.AddWithValue("news_cat_id", news_cat_id); news_categories n = null; SqlDataReader rd = cmd.ExecuteReader(); while (rd.Read()) { n = new news_categories(); n.news_cat_id = (int)rd["news_cat_id"]; n.news_cat_name = (string)rd["news_cat_name"]; n.news_cat_description = (string)rd["news_cat_description"]; } con.Close(); return(n); }