private Byte[] RetrieveProductImage(int countryId) { ContinentDBEntities db = new ContinentDBEntities(); var country = db.Countries.Find(countryId); if (country != null) { return country.Flag; } return null; }
protected void grdCountries_RowDeleting(object sender, GridViewDeleteEventArgs e) { ContinentDBEntities db = new ContinentDBEntities(); using (db) { var countryId = int.Parse(grdCountries.DataKeys[e.RowIndex].Value.ToString()); var towns = from t in db.Towns where t.Country_Id == countryId select t; db.Towns.RemoveRange(towns); db.SaveChanges(); } }
protected void grdCountries_RowCommand(object sender, GridViewCommandEventArgs e) //protected void upload_Click(object sender, EventArgs e) { //int index = Convert.ToInt32(e.CommandArgument); //GridViewRow row = grdCountries.Rows[index]; if (e.CommandName.ToLower() != "upload") { return; } //get index of clicked Item Button btn = (Button)e.CommandSource; GridViewRow gvRow = (GridViewRow)btn.NamingContainer; var itemIndex = int.Parse(grdCountries.DataKeys[gvRow.RowIndex].Value.ToString()); Button bts = e.CommandSource as Button; Response.Write(bts.Parent.Parent.GetType().ToString()); FileUpload fu = bts.FindControl("FileUpload") as FileUpload;//here if (fu.HasFile) { bool upload = true; string fleUpload = Path.GetExtension(fu.FileName.ToString()); if (fleUpload.Trim().ToLower() == ".png") { ContinentDBEntities db = new ContinentDBEntities(); using (db) { //fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString())); //string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString())); byte[] buffer = new byte[fu.FileContent.Length]; Stream s = fu.FileContent; s.Read(buffer, 0, buffer.Length); var currCountry = db.Countries.Where(c => c.Id == itemIndex).FirstOrDefault(); currCountry.Flag = buffer; db.SaveChanges(); //Someting to do?... } } else { upload = false; // Something to do?... } if (upload) { // somthing to do?... } } else { //Something to do?... } }