public bool AddFromMobile(object data) { List <SqlParameter> parameterList = new List <SqlParameter>(); string procName = "sp_T_Planing_Action_SitePreparation_Insert_Mobile"; string TPID = ""; SqlConnection conn = OpenConnection(); SqlTransaction transaction = conn.BeginTransaction(); isCan = true; try { T_PlaningActionSitePreparationMobileDTO obj = (T_PlaningActionSitePreparationMobileDTO)data; command = new SqlCommand(procName, conn, transaction); command.CommandType = CommandType.StoredProcedure; if (obj != null) { parameterList.AddRange(GetParametersExactly(procName, obj, transaction).ToArray()); command.Parameters.AddRange(parameterList.ToArray()); command.ExecuteNonQuery(); TPID = obj.TPID; procName = "sp_T_Planing_Action_SitePreparation_UnderGround_Insert_Mobile"; if (obj.underGroundList != null && obj.underGroundList.Count > 0) { foreach (M_UndergroundMobileDTO dto in obj.underGroundList) { dto.TPID = TPID; command = new SqlCommand(procName, conn, transaction); command.CommandType = CommandType.StoredProcedure; if (dto != null) { parameterList = new List <SqlParameter>(); parameterList.AddRange(GetParametersExactly(procName, dto, transaction).ToArray()); command.Parameters.AddRange(parameterList.ToArray()); // command.Parameters[0].Value = ""; } command.ExecuteNonQuery(); } } if (obj.DeleteFiles != null && obj.DeleteFiles.Length > 0) { procName = "sp_T_Planing_Action_SitePreparation_Files_Delete_Mobile"; foreach (var fileNo in obj.DeleteFiles.Split(',')) { if (fileNo != "") { command = new SqlCommand(procName, conn, transaction); command.CommandType = CommandType.StoredProcedure; if (fileNo != null) { T_PlaningFileMobileDTO file = new T_PlaningFileMobileDTO(); file.TPID = TPID; file.No = fileNo; parameterList = new List <SqlParameter>(); parameterList.AddRange(GetParametersExactly(procName, file, transaction).ToArray()); command.Parameters.AddRange(parameterList.ToArray()); // command.Parameters[0].Value = ""; } command.ExecuteNonQuery(); } } } procName = "sp_T_Planing_Action_SitePreparation_Files_Insert_Mobile"; if (obj.UploadFileList != null && obj.UploadFileList.Count > 0) { foreach (T_PlaningFileMobileDTO file in obj.UploadFileList) { file.TPID = TPID; command = new SqlCommand(procName, conn, transaction); command.CommandType = CommandType.StoredProcedure; if (file != null) { parameterList = new List <SqlParameter>(); parameterList.AddRange(GetParametersExactly(procName, file, transaction).ToArray()); command.Parameters.AddRange(parameterList.ToArray()); // command.Parameters[0].Value = ""; } command.ExecuteNonQuery(); } } } transaction.Commit(); } catch (Exception ex) { isCan = false; transaction.Rollback(); throw new Exception(ex.Message); } finally { CloseConnection(); } return(isCan); }
public HttpResponseMessage Add() { var deserializer = new JavaScriptSerializer(); mobileBal = new T_Planing_Action_SitePreparationMobileBAL(); ResposeType response = new ResposeType(); HttpResponseMessage mapMessage = null; T_PlaningActionSitePreparationMobileDTO dto = null; try { var context = HttpContext.Current; context.Response.ContentType = "multipart/form-data"; dto = ConvertX.GetReqeustFormExactly <T_PlaningActionSitePreparationMobileDTO>(); M_UndergroundMobileDTO[] undergroundDTOList = new JavaScriptSerializer().Deserialize <M_UndergroundMobileDTO[]>(context.Request.Form["objList"]); dto.underGroundList = new List <M_UndergroundMobileDTO>(); dto.underGroundList.AddRange(undergroundDTOList); dto.UploadFileList = deserializer.Deserialize <List <T_PlaningFileMobileDTO> >(context.Request.Form["fileList"]); if (dto.UploadFileList != null && dto.UploadFileList.Count > 0) { foreach (T_PlaningFileMobileDTO file in dto.UploadFileList) { file.DesPath = context.Server.MapPath(planPath) + @"\" + file.PID + @"\" + file.UploadType; file.FullPath = file.DesPath + @"\" + file.FileName; logger.debug("Add file:" + file.ToString()); } } logger.debug("Add dto :" + dto.ToString()); if (dto.underGroundList != null) { foreach (M_UndergroundMobileDTO ud in dto.underGroundList) { logger.debug("Add underground :" + ud.ToString()); } } response.statusCode = mobileBal.AddFromMobile(dto); if (response.statusCode) { response.statusText = "Success"; try { // For new upload { foreach (T_PlaningFileMobileDTO f in dto.UploadFileList) { if (DTO.PTT.Util.FileMng.HaveDirectory(f.DesPath)) { logger.debug("Save file to :" + f.FullPath); Utility.saveBase64File(f.FullPath, f.Base64File); } } } // For delete old file if (dto.DeleteFileNames != null && dto.DeleteFileNames.Length > 0) { foreach (var fileName in dto.DeleteFileNames.Split(',')) { if (fileName != "") { var realDesFile = context.Server.MapPath(planPath) + @"\" + dto.PID + @"\" + fileName; DTO.PTT.Util.FileMng.DeleteFile(realDesFile); } } } }catch (Exception e) { response.statusText = "Success but upload error:" + e.ToString(); } } } catch (Exception ex) { logger.error("Add error:" + ex.ToString()); response.statusText = ex.ToString(); } mapMessage = Request.CreateResponse(HttpStatusCode.OK, response); return(mapMessage); }