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.Data.OleDb; using System.Xml; using System.IO; public partial class controlportal_Default : System.Web.UI.Page { DataSet ds; string connString = DALUtilities.getConnectionString(); Press press = new Press(); protected void Page_Load(object sender, EventArgs e) { if (Session["isPermuted"] == null) Response.Redirect("~/controlportal/Login.aspx"); FillGrid(); if (!IsPostBack) { fillImgList(); fillDocList(); } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { if (ds != null) { if (ds.Tables[0].Rows[GridView1.SelectedIndex] != null) { txtDate.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["Date"].ToString(); txtenName.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_title"].ToString(); txt_name.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_title"].ToString(); txtBreif.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_abstract"].ToString(); txtAr_brief.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_abstarct"].ToString(); txtImg.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["img"].ToString(); txten_path.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_path"].ToString(); txtar_path.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_path"].ToString(); } } } protected string chngchars(string strText) { string UpdatedText; UpdatedText = strText.Replace("'", "''"); return UpdatedText; } protected void btnSave_Click(object sender, ImageClickEventArgs e) { press.date = txtDate.Text; press.Ar_abstract = chngchars(txtAr_brief.Text); press.En_abstract = chngchars(txtBreif.Text); press.Ar_title = chngchars(txt_name.Text); press.En_title = chngchars(txtenName.Text); press.En_path = txten_path.Text; press.Ar_path = txtar_path.Text; try { OleDbCommand command; string query; if (Session["isNew"] != null) { query = "INSERT INTO press ([Date], en_title,ar_title, en_abstract, ar_abstarct, pressTypeID, en_path, img, ar_path) VALUES('" + press.date + "','" + press.En_title + "','" + press.Ar_title + "', '" + press.En_abstract + "'," + " '" + press.Ar_abstract + "', 0,'" + press.En_path + "','" + txtImg.Text + "','" + press.Ar_path + "')"; Session.Remove("isNew"); clrBoxs(); } else { press.ID = (int)ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"]; query = "UPDATE press SET en_title='" + press.En_title + "', [Date]='" + press.date + "', ar_title='" + press.Ar_title + "', en_abstract='" + press.En_abstract + "', pressTypeID=0," + " ar_abstarct='" + press.Ar_abstract + "', en_path='" + press.En_path + "', ar_path='" + press.Ar_path + "', img='" + txtImg.Text + "' WHERE ID=" + press.ID.ToString(); } OleDbConnection connection = new OleDbConnection(connString); command = new OleDbCommand(query, connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); FillGrid(); } catch (OleDbException ex) { //loging code Response.Write(ex.Message); } } protected void btnNew_Click(object sender, ImageClickEventArgs e) { clrBoxs(); Session.Add("isNew", "true"); ds.Tables.Clear(); FillGrid(); } private void FillGrid() { ds = new DataSet(); string Query = "SELECT * FROM press "; OleDbDataAdapter adap = new OleDbDataAdapter(Query, new OleDbConnection(connString)); adap.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void btnDel_Click(object sender, ImageClickEventArgs e) { OleDbCommand command; string query; string strID = ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"].ToString(); string strImg = ds.Tables[0].Rows[GridView1.SelectedIndex]["img"].ToString(); string strEnPDF = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_path"].ToString(); string strArPDF = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_path"].ToString(); if (strImg != null && CheckBox1.Checked) Delete_File(strImg, "AppImages"); if (strEnPDF != null && CheckBox1.Checked) Delete_File(strEnPDF, "Articles"); if (strArPDF != null && strArPDF != strEnPDF && CheckBox1.Checked) Delete_File(strArPDF, "Articles"); ds.Tables.Clear(); query = "DELETE FROM press WHERE id=" + strID; OleDbConnection connection = new OleDbConnection(connString); command = new OleDbCommand(query, connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); FillGrid(); fillDocList(); fillImgList(); clrBoxs(); } private void clrBoxs() { txtDate.Text = string.Empty; txten_path.Text = string.Empty; txtar_path.Text = string.Empty; txt_name.Text = string.Empty; txtAr_brief.Text = string.Empty; txtBreif.Text = string.Empty; txtenName.Text = string.Empty; txtImg.Text = string.Empty; ddlar_path.SelectedIndex = -1; ddlen_path.SelectedIndex = -1; ddlmgs.SelectedIndex = -1; } protected void btnCancel_Click(object sender, ImageClickEventArgs e) { if (Session["isNew"] != null) Session.Remove("isNew"); clrBoxs(); } private void fillImgList() { ddlmgs.Items.Clear(); string[] strFiles = Directory.GetFiles(HttpContext.Current.Server.MapPath("../AppImages")); for (int i = 0; i < strFiles.Length; i++) { int index = strFiles[i].LastIndexOf('\\'); string strFile = strFiles[i].Remove(0, index + 1); ddlmgs.Items.Add(new ListItem(strFiles[i].Remove(0, index + 1), strFiles[i].Remove(0, index + 1))); } } private void fillDocList() { ddlen_path.Items.Clear(); ddlar_path.Items.Clear(); string[] strFiles = Directory.GetFiles(HttpContext.Current.Server.MapPath("../Articles")); for (int i = 0; i < strFiles.Length; i++) { int index = strFiles[i].LastIndexOf('\\'); string strFile = strFiles[i].Remove(0, index + 1); ddlen_path.Items.Add(new ListItem(strFiles[i].Remove(0, index + 1), strFiles[i].Remove(0, index + 1))); ddlar_path.Items.Add(new ListItem(strFiles[i].Remove(0, index + 1), strFiles[i].Remove(0, index + 1))); } } protected void ddlmgs_SelectedIndexChanged(object sender, EventArgs e) { txtImg.Text = ddlmgs.SelectedValue; } protected void ddlen_path_SelectedIndexChanged(object sender, EventArgs e) { txten_path.Text = ddlen_path.SelectedValue; } protected void ddlar_path_SelectedIndexChanged(object sender, EventArgs e) { txtar_path.Text = ddlar_path.SelectedValue; } protected void Delete_File(string file_name, string folder_name) { OleDbCommand command1, command2, command3, command4, command5, command6, command7, command8, command9; string query1, query2, query3, query4, query5, query6, query7, query8, query9; int i, i1, i2, i3, i4, i5, i6, i7, i8, i9; query1 = "SELECT COUNT(ID) FROM casestudies WHERE img1='" + file_name + "' OR img2='" + file_name + "' OR en_path='" + file_name + "' OR ar_path='" + file_name + "';"; query2 = "SELECT COUNT(ID) FROM press WHERE img='" + file_name + "' OR en_path='" + file_name + "' OR ar_path='" + file_name + "';"; query3 = "SELECT COUNT(ID) FROM publications WHERE img='" + file_name + "' OR en_path='" + file_name + "' OR ar_path='" + file_name + "';"; query4 = "SELECT COUNT(ID) FROM clients WHERE logo='" + file_name + "';"; query5 = "SELECT COUNT(ID) FROM ecopartners WHERE logo='" + file_name + "';"; query6 = "SELECT COUNT(ID) FROM news WHERE img='" + file_name + "';"; query7 = "SELECT COUNT(ID) FROM publishers WHERE logo='" + file_name + "';"; query8 = "SELECT COUNT(ID) FROM usefullinks WHERE img='" + file_name + "';"; query9 = "SELECT COUNT(ID) FROM events WHERE img='" + file_name + "' OR big_image='" + file_name + "';"; OleDbConnection connection = new OleDbConnection(connString); command1 = new OleDbCommand(query1, connection); command2 = new OleDbCommand(query2, connection); command3 = new OleDbCommand(query3, connection); command4 = new OleDbCommand(query4, connection); command5 = new OleDbCommand(query5, connection); command6 = new OleDbCommand(query6, connection); command7 = new OleDbCommand(query7, connection); command8 = new OleDbCommand(query8, connection); command9 = new OleDbCommand(query9, connection); connection.Open(); i1 = (int)command1.ExecuteScalar(); i2 = (int)command2.ExecuteScalar(); i3 = (int)command3.ExecuteScalar(); i4 = (int)command4.ExecuteScalar(); i5 = (int)command5.ExecuteScalar(); i6 = (int)command6.ExecuteScalar(); i7 = (int)command7.ExecuteScalar(); i8 = (int)command8.ExecuteScalar(); i9 = (int)command9.ExecuteScalar(); connection.Close(); i = i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9; if (i < 2) { try { FileInfo TheFile = new FileInfo(MapPath("../") + folder_name + "/" + file_name); if (TheFile.Exists) { File.Delete(MapPath("../") + folder_name + "/" + file_name); } else { throw new FileNotFoundException(); } } catch (FileNotFoundException ex) { Label2.Text = ex.Message; } catch (Exception ex) { Label2.Text = ex.Message; } } } protected void btnfuImg_Click(object sender, EventArgs e) { if (fuImg.HasFile) { fuImg.SaveAs(MapPath("~/AppImages/") + fuImg.FileName); txtImg.Text = fuImg.FileName; fillImgList(); } } protected void btnfuEnPath_Click(object sender, EventArgs e) { if (fuEnPath.HasFile) { fuEnPath.SaveAs(MapPath("~/Articles/") + fuEnPath.FileName); txten_path.Text = fuEnPath.FileName; fillDocList(); } } protected void btnfuArPath_Click(object sender, EventArgs e) { if (fuArPath.HasFile) { fuArPath.SaveAs(MapPath("~/Articles/") + fuArPath.FileName); txtar_path.Text = fuArPath.FileName; fillDocList(); } } }