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 = ConfigurationSettings.AppSettings["ConString"]; string connString = DALUtilities.getConnectionString(); CaseStudy caseStudy = new CaseStudy(); protected void Page_Load(object sender, EventArgs e) { if (Session["isPermuted"] == null) Response.Redirect("~/controlportal/Login.aspx"); FillGrid(); if (!IsPostBack) { fillImgList(); fillPDFList(); } //fillImgList(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { if (ds != null) { if (ds.Tables[0].Rows[GridView1.SelectedIndex] != null) { 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(); if (ds.Tables[0].Rows[GridView1.SelectedIndex]["caseStudyTypeID"].ToString() != "0") ddlTypes.SelectedValue = ds.Tables[0].Rows[GridView1.SelectedIndex]["caseStudyTypeID"].ToString(); txtImg.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["img1"].ToString(); txtImg2.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["img2"].ToString(); txtEnPDF.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_path"].ToString(); txtArPDF.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_path"].ToString(); } } } protected void btnSave_Click(object sender, ImageClickEventArgs e) { caseStudy.Ar_abstract = chngchars(txtAr_brief.Text); caseStudy.En_abstract = chngchars(txtBreif.Text); caseStudy.Ar_title = chngchars(txt_name.Text); caseStudy.En_title = chngchars(txtenName.Text); caseStudy.img1 = txtImg.Text; caseStudy.img2 = txtImg2.Text; caseStudy.Ar_path = txtArPDF.Text; caseStudy.En_path = txtEnPDF.Text; caseStudy.caseStudyTypeID = Int32.Parse(ddlTypes.SelectedValue); try { OleDbCommand command; string query; int i; if (Session["isNew"] != null) { query = "INSERT INTO casestudies (en_title, ar_title, en_abstract, ar_abstarct, caseStudyTypeID, en_path, img1, img2, ar_path) VALUES('" + caseStudy.En_title + "', '" + caseStudy.Ar_title + "', '" + caseStudy.En_abstract + "'," + " '" + caseStudy.Ar_abstract + "'," + caseStudy.caseStudyTypeID + ", '" + caseStudy.En_path + "', '" + caseStudy.img1 + "', '" + caseStudy.img2 + "', '" + caseStudy.Ar_path + "'" + ")"; Session.Remove("isNew"); clrBoxs(); } else { caseStudy.ID = (int)ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"]; query = "UPDATE casestudies SET en_title='" + caseStudy.En_title + "', ar_title='" + caseStudy.Ar_title + "', en_abstract='" + caseStudy.En_abstract + "'," + " ar_abstarct='" + caseStudy.Ar_abstract + "'," + " en_path='" + caseStudy.En_path + "'," + " ar_path='" + caseStudy.Ar_path + "'," + " img1='" + txtImg.Text + "', img2='" + txtImg2.Text + "', caseStudyTypeID=" + caseStudy.caseStudyTypeID + " WHERE ID=" + caseStudy.ID.ToString(); } OleDbConnection connection = new OleDbConnection(connString); command = new OleDbCommand(query, connection); connection.Open(); i= command.ExecuteNonQuery(); connection.Close(); FillGrid(); } catch (OleDbException ex) { //loging code Label2.Text= "Message: " + ex.Message; } } protected void btnNew_Click(object sender, ImageClickEventArgs e) { clrBoxs(); Session.Add("isNew", "true"); ds.Tables.Clear(); FillGrid(); fillImgList(); fillPDFList(); clrBoxs(); } private void FillGrid() { ds = new DataSet(); string Query = "SELECT * FROM casestudies "; OleDbDataAdapter adap = new OleDbDataAdapter(Query, new OleDbConnection(connString)); adap.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected string chngchars(string strText) { string UpdatedText; UpdatedText = strText.Replace("'", "''"); return UpdatedText; } protected void btnDel_Click(object sender, ImageClickEventArgs e) { if (GridView1.SelectedIndex != -1) { OleDbCommand command; string query; string strID = ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"].ToString(); string strImg = ds.Tables[0].Rows[GridView1.SelectedIndex]["img1"].ToString(); string strImg2 = ds.Tables[0].Rows[GridView1.SelectedIndex]["img2"].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 (strImg2 != null && strImg2 != strImg && CheckBox1.Checked) Delete_File(strImg2, "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 casestudies WHERE id=" + strID; OleDbConnection connection = new OleDbConnection(connString); command = new OleDbCommand(query, connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); FillGrid(); fillImgList(); fillPDFList(); clrBoxs(); } } private void clrBoxs() { txt_name.Text = string.Empty; txtAr_brief.Text = string.Empty; txtBreif.Text = string.Empty; txtenName.Text = string.Empty; txtImg.Text = string.Empty; txtImg2.Text = string.Empty; txtEnPDF.Text = string.Empty; txtArPDF.Text = string.Empty; GridView1.SelectedIndex = -1; ddlTypes.SelectedIndex = -1; ddlmgs.SelectedIndex = -1; ddlmgs2.SelectedIndex = -1; ddlEnPDF.SelectedIndex = -1; ddlArPDF.SelectedIndex = -1; CheckBox1.Checked = false; Label2.Text = string.Empty; } protected void btnCancel_Click(object sender, ImageClickEventArgs e) { if (Session["isNew"] != null) Session.Remove("isNew"); clrBoxs(); } private void fillImgList() { ddlmgs.Items.Clear(); ddlmgs2.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))); ddlmgs2.Items.Add(new ListItem(strFiles[i].Remove(0, index + 1), strFiles[i].Remove(0, index + 1))); } } private void fillPDFList() { ddlEnPDF.Items.Clear(); ddlArPDF.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); ddlEnPDF.Items.Add(new ListItem(strFiles[i].Remove(0, index + 1), strFiles[i].Remove(0, index + 1))); ddlArPDF.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 ddlmgs2_SelectedIndexChanged(object sender, EventArgs e) { txtImg2.Text = ddlmgs2.SelectedValue; } protected void ddlEnPDF_SelectedIndexChanged(object sender, EventArgs e) { txtEnPDF.Text = ddlEnPDF.SelectedValue; } protected void ddlArPDF_SelectedIndexChanged(object sender, EventArgs e) { txtArPDF.Text = ddlArPDF.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 btnfuImg2_Click(object sender, EventArgs e) { if (fuImg2.HasFile) { fuImg2.SaveAs(MapPath("~/AppImages/") + fuImg2.FileName); txtImg2.Text = fuImg2.FileName; fillImgList(); } } protected void btnfuEnPDF_Click(object sender, EventArgs e) { if (fuEnPDF.HasFile) { fuEnPDF.SaveAs(MapPath("~/Articles/") + fuEnPDF.FileName); txtEnPDF.Text = fuEnPDF.FileName; fillPDFList(); } } protected void btnfuArPDF_Click(object sender, EventArgs e) { if (fuArPDF.HasFile) { fuArPDF.SaveAs(MapPath("~/Articles/") + fuArPDF.FileName); txtArPDF.Text = fuArPDF.FileName; fillPDFList(); } } }