public List<Picture> GetListPicture() { List<Picture> list = new List<Picture>(); Picture ins; //...Database Connection... DataBaseConnection dbConn = new DataBaseConnection(); SqlConnection con = dbConn.SqlConn(); SqlCommand cmdI; //...SQL Commands... cmdI = new SqlCommand("SELECT * FROM Picture", con); cmdI.Connection.Open(); SqlDataReader drI = cmdI.ExecuteReader(); //...Retrieve Data... if (drI.HasRows) { while (drI.Read()) { ins = new Picture(); ins.PictureId = Convert.ToInt32(drI["PictureId"]); ins.PicUrl = drI["PicUrl"].ToString(); ins.ClientId = Convert.ToInt32(drI["ClientId"]); list.Add(ins); } } drI.Close(); con.Close(); return list; }
public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments) { PictureRepository picRep = new PictureRepository(); Functions functions = new Functions(); // The Name of the Upload component is "attachments" int NewPicID = picRep.GetLastPictureId(Convert.ToInt32(HttpContext.Session["ClientId"])); NewPicID++; Picture ins = new Picture(); foreach (var file in attachments) { // Some browsers send file names with full path. This needs to be stripped. var fileName = Path.GetFileName(file.FileName); var extention = fileName.Substring(fileName.IndexOf('.')); var newfilename = NewPicID.ToString() + extention; // Check if Path exsits string serverpath = Server.MapPath("~/Images/Client/" + HttpContext.Session["ClientId"].ToString() + "/Ads"); if (!Directory.Exists(serverpath)) { Directory.CreateDirectory(serverpath); } var physicalPath = Path.Combine(Server.MapPath("~/Images/Client/" + HttpContext.Session["ClientId"].ToString() + "/Ads"), newfilename); //...Resize.. Image original = Image.FromStream(file.InputStream, true, true); Image resized = functions.ResizeImage(original, new Size(500, 500)); resized.Save(physicalPath, ImageFormat.Jpeg); string finalpath = physicalPath.ToString(); finalpath = finalpath.Substring(finalpath.IndexOf("Images")); finalpath = finalpath.Replace('\\', '/'); //...Save In DB... ins.PicUrl = Constants.HTTPPath + finalpath; ins.ClientId = Convert.ToInt32(HttpContext.Session["ClientId"]); ins = picRep.InsertPicture(ins); } // Return an empty string to signify success if (ins.PictureId != 0) return Json(new { status = ins.PictureId.ToString() }, "text/plain"); else return Json(new { status = "0" }, "text/plain"); }
public Picture UpdatePicture(Picture ins) { //...Database Connection... DataBaseConnection dbConn = new DataBaseConnection(); SqlConnection con = dbConn.SqlConn(); con.Open(); SqlCommand cmdI = con.CreateCommand(); cmdI.Connection = con; //...Update Record... cmdI.Parameters.Clear(); cmdI.CommandText = "f_Admin_Update_Picture"; cmdI.CommandType = System.Data.CommandType.StoredProcedure; cmdI.Parameters.AddWithValue("@PictureId", ins.PictureId); cmdI.Parameters.AddWithValue("@PicUrl", ins.PicUrl); cmdI.ExecuteNonQuery(); cmdI.Connection.Close(); return ins; }
public Picture InsertPicture(Picture ins) { //...Get User and Date Data... string strTrx = "Insert_Picture"; //...Database Connection... DataBaseConnection dbConn = new DataBaseConnection(); SqlConnection con = dbConn.SqlConn(); con.Open(); //...Command Interface... SqlCommand cmdI = con.CreateCommand(); SqlTransaction trx; trx = con.BeginTransaction(strTrx); cmdI.Connection = con; cmdI.Transaction = trx; try { //...Insert Record... cmdI.Parameters.Clear(); cmdI.CommandText = "f_Admin_Insert_Picture"; //cmdI.Connection.Open(); cmdI.CommandType = System.Data.CommandType.StoredProcedure; cmdI.Parameters.AddWithValue("@PicUrl", ins.PicUrl); cmdI.Parameters.AddWithValue("@ClientId", ins.ClientId); //...Return new ID... ins.PictureId = (int)cmdI.ExecuteScalar(); //...Commit Transaction... trx.Commit(); cmdI.Connection.Close(); } catch (SqlException ex) { if (trx != null) trx.Rollback(); //...Save Error to Log... Functions func = new Functions(); func.LogError(ex.ToString()); } finally { //...Check for close and respond accordingly.. if (con.State != ConnectionState.Closed) { con.Close(); } //...Clean up... con.Dispose(); cmdI.Dispose(); trx.Dispose(); } return ins; }
public Picture GetPicture(int PictureId) { Picture ins = new Picture(); //...Database Connection... DataBaseConnection dbConn = new DataBaseConnection(); SqlConnection con = dbConn.SqlConn(); SqlCommand cmdI; //...SQL Commands... cmdI = new SqlCommand("SELECT * FROM Picture WHERE PictureId =" + PictureId, con); cmdI.Connection.Open(); SqlDataReader drI = cmdI.ExecuteReader(); //...Retrieve Data... if (drI.HasRows) { while (drI.Read()) { ins.PictureId = Convert.ToInt32(drI["PictureId"]); ins.PicUrl = drI["PicUrl"].ToString(); ins.ClientId = Convert.ToInt32(drI["ClientId"]); } } drI.Close(); con.Close(); return ins; }