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(); Events events = new Events(); protected void Page_Load(object sender, EventArgs e) { if (Session["isPermuted"] == null) Response.Redirect("~/controlportal/Login.aspx"); FillGrid(); if(!IsPostBack) fillImgList(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { Label3.Text = ""; 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(); txten_venue.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_venue"].ToString(); txtar_venue.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_venue"].ToString(); txtBreif.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_abstarct"].ToString(); txtAr_brief.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_abstarct"].ToString(); txten_body.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_body"].ToString(); txtar_body.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_body"].ToString(); txtImg.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["img"].ToString(); txtbig_image.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["big_image"].ToString(); txten_comment.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_img_comment"].ToString(); txtar_comment.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_img_comment"].ToString(); txten_organizer.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_organizer"].ToString(); txtar_organizer.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_organizer"].ToString(); txt_AttFile.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["AttFiles"].ToString(); chbIsNew.Checked = (bool)ds.Tables[0].Rows[GridView1.SelectedIndex]["isNew"]; chkIsHome.Checked = (bool)ds.Tables[0].Rows[GridView1.SelectedIndex]["inHome"]; } } } protected void btnSave_Click(object sender, ImageClickEventArgs e) { Label3.Text = ""; events.Date = txtdate.Text; events.En_title = chngchars(txtenName.Text); events.Ar_title = chngchars(txt_name.Text); events.en_venue = chngchars(txten_venue.Text); events.ar_venue = chngchars(txtar_venue.Text); events.En_abstract = chngchars(txtBreif.Text); events.Ar_abstract = chngchars(txtAr_brief.Text); events.en_body = chngchars(txten_body.Text); events.ar_body = chngchars(txtar_body.Text); events.img1 = txtImg.Text; events.big_image = txtbig_image.Text; events.en_img_comment = chngchars(txten_comment.Text); events.ar_img_comment = chngchars(txtar_comment.Text); events.TypeID = 1; events.isNew = chbIsNew.Checked; events.en_organizer = chngchars(txten_organizer.Text); events.ar_organizer = chngchars(txtar_organizer.Text); events.attfiles = chngchars(txt_AttFile.Text); events.inHome = chkIsHome.Checked; try { OleDbCommand command; string query; if (Session["isNew"] != null) { string enPath = " "; string arPath = " "; query = "INSERT INTO events ([date], en_title, ar_title, en_venue, ar_venue, en_abstarct, ar_abstarct, en_body, ar_body, [img], big_image, en_img_comment, ar_img_comment, eventTypeID, isNew, en_path, ar_path, en_organizer, ar_organizer, attfiles, inHome) VALUES('" + events.Date + "','" + events.En_title + "','" + events.Ar_title + "','" + events.en_venue + "','" + events.ar_venue + "','" + events.En_abstract + "','" + events.Ar_abstract + "','" + events.en_body + "','" + events.ar_body + "','" + events.img1 + "','" + events.big_image + "','" + events.en_img_comment + "','" + events.ar_img_comment + "',1," + events.isNew + ",'','','" + events.en_organizer + "','" + events.ar_organizer + "','" + events.attfiles + "'," + events.inHome + ")"; Session.Remove("isNew"); clrBoxs(); } else { string enPath = ""; string arPath = ""; events.ID = (int)ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"]; query = "UPDATE events SET en_title='" + events.En_title + "', ar_title='" + events.Ar_title + "', en_abstarct='" + events.En_abstract + "'," + " ar_abstarct='" + events.Ar_abstract + "', isNew= " + events.isNew + ", eventTypeID=" + events.TypeID + ", inHome=" + chkIsHome.Checked + ", [Date]='" + events.Date + "', en_venue='" + events.en_venue + "', ar_venue='" + events.ar_venue + "', en_body='" + events.en_body + "', ar_body='" + events.ar_body + "', [img]='" + events.img1 + "', big_image='" + events.big_image + "', en_path='" + enPath + "', ar_path='" + arPath + "', en_organizer='" + events.en_organizer + "', ar_organizer='" + events.ar_organizer + "', attfiles='" + events.attfiles + "'" + " WHERE ID=" + events.ID.ToString(); } OleDbConnection connection = new OleDbConnection(connString); command = new OleDbCommand(query, connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); FillGrid(); fillImgList(); } catch (OleDbException ex) { //loging code Label3.Text= ex.Message; } } protected void btnNew_Click(object sender, ImageClickEventArgs e) { Label3.Text = ""; clrBoxs(); Session.Add("isNew", "true"); ds.Tables.Clear(); FillGrid(); fillImgList(); } private void FillGrid() { ds = new DataSet(); string Query = "SELECT * FROM events "; OleDbDataAdapter adap = new OleDbDataAdapter(Query, new OleDbConnection(connString)); adap.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void btnDel_Click(object sender, ImageClickEventArgs e) { Label3.Text = ""; 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 strbig_Img = ds.Tables[0].Rows[GridView1.SelectedIndex]["big_image"].ToString(); if (strImg != null && CheckBox1.Checked) Delete_File(strImg, "AppImages"); if (strbig_Img != null && strbig_Img!= strImg && CheckBox1.Checked) Delete_File(strImg, "AppImages"); ds.Tables.Clear(); query = "DELETE FROM events WHERE id=" + strID; OleDbConnection connection = new OleDbConnection(connString); command = new OleDbCommand(query, connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); FillGrid(); fillImgList(); clrBoxs(); } private void clrBoxs() { txtdate.Text = string.Empty; txten_organizer.Text = string.Empty; txtar_organizer.Text = string.Empty; txtenName.Text = string.Empty; txt_name.Text = string.Empty; txtBreif.Text = string.Empty; txtAr_brief.Text = string.Empty; txten_venue.Text = string.Empty; txtar_venue.Text = string.Empty; txtImg.Text = string.Empty; txtbig_image.Text = string.Empty; txten_comment.Text = string.Empty; txtar_comment.Text = string.Empty; txten_body.Text = string.Empty; txtar_body.Text = string.Empty; txt_AttFile.Text = string.Empty; chbIsNew.Checked = false; chkIsHome.Checked = false; GridView1.SelectedIndex = -1; ddlbig_image.SelectedIndex = -1; ddlmgs.SelectedIndex = -1; ddl_AttFile.SelectedIndex = -1; } protected void btnCancel_Click(object sender, ImageClickEventArgs e) { Label3.Text = ""; if (Session["isNew"] != null) Session.Remove("isNew"); clrBoxs(); } private void fillImgList() { ddlmgs.Items.Clear(); ddlbig_image.Items.Clear(); ddl_AttFile.Items.Clear(); string[] strFiles = Directory.GetFiles(HttpContext.Current.Server.MapPath("../AppImages")); string[] strFiles2 = Directory.GetFiles(HttpContext.Current.Server.MapPath("../App_Files")); 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))); ddlbig_image.Items.Add(new ListItem(strFiles[i].Remove(0, index + 1), strFiles[i].Remove(0, index + 1))); } for (int i = 0; i < strFiles2.Length; i++) { int index = strFiles2[i].LastIndexOf('\\'); string strFile2 = strFiles2[i].Remove(0, index + 1); ddl_AttFile.Items.Add(new ListItem(strFiles2[i].Remove(0, index + 1), strFiles2[i].Remove(0, index + 1))); } } protected void ddlmgs_SelectedIndexChanged(object sender, EventArgs e) { txtImg.Text = ddlmgs.SelectedValue; } protected void ddlbig_image_SelectedIndexChanged(object sender, EventArgs e) { txtbig_image.Text = ddlbig_image.SelectedValue; } protected void ddl_AttFile_SelectedIndexChanged(object sender, EventArgs e) { txt_AttFile.Text = ddl_AttFile.SelectedValue; } protected void btnfuImg_Click(object sender, EventArgs e) { if (fuImg.HasFile) { fuImg.SaveAs(MapPath("~/AppImages/") + fuImg.FileName); txtImg.Text = fuImg.FileName; fillImgList(); } } protected void btn_AttFile_Click(object sender, EventArgs e) { if (fup_AttFile.HasFile) { fup_AttFile.SaveAs(MapPath("~/App_Files/") + fup_AttFile.FileName); txt_AttFile.Text = fup_AttFile.FileName; fillImgList(); } } protected string chngchars(string strText) { string UpdatedText; UpdatedText = strText.Replace("'", "''"); return UpdatedText; } 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) { Label3.Text = ex.Message; } catch (Exception ex) { Label3.Text = ex.Message; } } } protected void btnfubig_image_Click(object sender, EventArgs e) { if (fubig_image.HasFile) { fubig_image.SaveAs(MapPath("~/AppImages/") + fubig_image.FileName); txtbig_image.Text = fubig_image.FileName; fillImgList(); } } }