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 admin_Default : System.Web.UI.Page { DataSet ds; //string connString = ConfigurationSettings.AppSettings["ConString"]; string connString = DALUtilities.getConnectionString(); Clients client = new Clients(); protected void Page_Load(object sender, EventArgs e) { if (Session["isPermuted"] == null) { Response.Redirect("~/controlportal/Login.aspx"); } FillGrid(); if (!IsPostBack) { FillddlTypes(); fillImgList(); } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { if (ds != null) { if (GridView1.SelectedIndex != -1) { txtenName.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_name"].ToString(); txt_name.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_name"].ToString(); txtBreif.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["en_Brief"].ToString(); txtAr_brief.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["ar_Brief"].ToString(); txtLogo.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["logo"].ToString(); txtLink.Text = ds.Tables[0].Rows[GridView1.SelectedIndex]["link"].ToString(); ddlTypes.SelectedValue = ds.Tables[0].Rows[GridView1.SelectedIndex]["typeID"].ToString(); } } } protected void btnSave_Click(object sender, ImageClickEventArgs e) { client.Ar_Brief = chngchars(txtAr_brief.Text); client.En_Brief = chngchars(txtBreif.Text); client.Ar_name = chngchars(txt_name.Text); client.En_name = chngchars(txtenName.Text); client.Logo = txtLogo.Text; client.Link = txtLink.Text; try { OleDbCommand command; string query; if (Session["isNew"] != null) { query = "INSERT INTO clients (en_name,ar_name, en_Brief, ar_Brief, logo, typeID, link) VALUES('" + client.En_name + "','" + client.Ar_name + "', '" + client.En_Brief + "'," + " '" + client.Ar_Brief + "','" + client.Logo + "','" + ddlTypes.SelectedValue+"','" + client.Link + "')"; Session.Remove("isNew"); } else { client.ID = (int)ds.Tables[0].Rows[GridView1.SelectedIndex]["ID"]; query = "UPDATE clients SET en_name='" + client.En_name + "', ar_name='" + client.Ar_name + "', en_Brief='" + client.En_Brief + "'," + " ar_Brief='" + client.Ar_Brief + "', logo='" + client.Logo + "', typeID='"+ ddlTypes.SelectedValue + "', link='" + client.Link + "' WHERE ID=" + client.ID.ToString(); } OleDbConnection connection = new OleDbConnection(connString); command = new OleDbCommand(query, connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); FillGrid(); fillImgList(); clrBoxs(); } catch (OleDbException ex) { //loging code Response.Write(ex.Message); } } protected string chngchars(string strText) { string UpdatedText; UpdatedText = strText.Replace("'", "''"); return UpdatedText; } protected void btnNew_Click(object sender, ImageClickEventArgs e) { clrBoxs(); Session.Add("isNew", "true"); ds.Tables.Clear(); FillGrid(); fillImgList(); } private void FillGrid() { ds = new DataSet(); string Query = "SELECT * FROM clients "; 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) { 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 clients 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; txtAr_brief.Text = string.Empty; txtBreif.Text = string.Empty; txtenName.Text = string.Empty; txtLogo.Text = string.Empty; txtLink.Text = string.Empty; ddlTypes.SelectedIndex = 0; GridView1.SelectedIndex = -1; Panel1.Visible = false; } private void FillddlTypes() { ddlSource.DataBind(); ddlSource.DataBind(); } 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))); } } protected void ddlmgs_SelectedIndexChanged(object sender, EventArgs e) { txtLogo.Text = ddlmgs.SelectedValue; } protected void btnfuImg_Click(object sender, EventArgs e) { if (fuImg.HasFile) { fuImg.SaveAs(MapPath("~/AppImages/") + fuImg.FileName); txtLogo.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; } } } protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { Panel1.Visible = true; Label4.Text = ddlTypes.SelectedItem.ToString(); Label3.Text = txtenName.Text; Label5.Text = txtBreif.Text; ImageMap1.ImageUrl = "../AppImages/" + txtLogo.Text; client.Link = txtLink.Text; } }