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.IO; public partial class controlportal_Default : System.Web.UI.Page { DataSet ds; string connString = System.Configuration.ConfigurationManager.AppSettings["ConString"]; Publishers publisher = new Publishers(); protected void Page_Load(object sender, EventArgs e) { if (Session["isPermuted"] == null) Response.Redirect("~/controlportal/Login.aspx"); FillGrid(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { if (ds != null) { if (ds.Tables[0].Rows[GridView1.SelectedIndex] != null) { txt_name.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_Publisher"].ToString(); txtenName.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["Publisher"].ToString(); txt_logo.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["logo"].ToString(); } } } protected string chngchars(string strText) { string UpdatedText; UpdatedText = strText.Replace("'", "''"); return UpdatedText; } protected void btnSave_Click(object sender, ImageClickEventArgs e) { Label2.Text = ""; publisher.Publisher = chngchars(txtenName.Text); publisher.ar_Publisher = chngchars(txt_name.Text); publisher.logo = txt_logo.Text; try { OleDbCommand command; string query; if (Session["isNew"] != null) { query = "INSERT INTO publishers ([Publisher],ar_Publisher,logo) VALUES('" + publisher.Publisher + "','" + publisher.ar_Publisher + "','" + publisher.logo + "')"; Session.Remove("isNew"); clrBoxs(); } else { publisher.ID = (int)ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"]; query = "UPDATE Publishers SET [Publisher]='" + publisher.Publisher + "', ar_Publisher='" + publisher.ar_Publisher + "',logo='" + publisher.logo + "'" + " WHERE id=" + publisher.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 Label2.Text = ex.Message; } } protected void btnNew_Click(object sender, ImageClickEventArgs e) { Label2.Text = ""; clrBoxs(); Session.Add("isNew", "true"); ds.Tables.Clear(); FillGrid(); fillImgList(); } private void FillGrid() { ds = new DataSet(); string Query = "SELECT * FROM Publishers"; OleDbConnection con = new OleDbConnection(connString); con.Open(); OleDbDataAdapter adap = new OleDbDataAdapter(Query, con); adap.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void btnDel_Click(object sender, ImageClickEventArgs e) { Label2.Text = ""; OleDbCommand command; string query; string strID = ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"].ToString(); string strImg = ds.Tables[0].Rows[GridView1.SelectedIndex]["Logo"].ToString(); if (strImg != null && CheckBox1.Checked) Delete_File(strImg, "AppImages"); ds.Tables.Clear(); query = "DELETE FROM Publishers 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() { txt_name.Text = string.Empty; txtenName.Text = string.Empty; txt_logo.Text = string.Empty; ddl_logo.SelectedIndex = -1; CheckBox1.Checked = false; } protected void btnCancel_Click(object sender, ImageClickEventArgs e) { ViewState.Remove("isNew"); GridView1.SelectedIndex = -1; clrBoxs(); } private void fillImgList() { ddl_logo.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); ddl_logo.Items.Add(new ListItem(strFiles[i].Remove(0, index + 1), strFiles[i].Remove(0, index + 1))); } } protected void ddl_logo_SelectedIndexChanged1(object sender, EventArgs e) { txt_logo.Text = ddl_logo.SelectedValue; } protected void btnfuImg_Click(object sender, EventArgs e) { if (fuImg.HasFile) { fuImg.SaveAs(MapPath("~/AppImages/") + fuImg.FileName); txt_logo.Text = fuImg.FileName; fillImgList(); } } 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; } } } }