Friday 25 November 2016

download file in C#

[HttpGet]
        public ActionResult DownloadDocument(string fileName)
        {
            try
            {
                var filePath = GetImportsPath();
                var newFileFullPath = Path.Combine(filePath, fileName);
                var fileExtension = Path.GetExtension(fileName);
                var reConstructedFileName = "ConstituentId" + "_" + DateTime.UtcNow.ToShortDateString() + "_" + "OriginalFileName" + fileExtension;
                if (fileExtension == null)
                    throw new PersistenceValidationException("Export Error", new List<BrokenRule>
                {
                    new BrokenRule("InvalidDownload", "The file you are looking for download does not exist.")
                });
                if (fileExtension.ToLower() == ".zip" || fileExtension.ToLower() == ".rar")
                {
                    return File(newFileFullPath, "application/zip", reConstructedFileName);
                }
                return File(newFileFullPath, "application/octet-stream", reConstructedFileName);
            }
            catch (PersistenceValidationException ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json(ex.BrokenRules, JsonRequestBehavior.AllowGet);
            }
        }

_________________________________________

 protected static string GetImportsPath()
        {
            var fileUploadLocation = ConfigurationManager.AppSettings["importsPath"];

            if (!Directory.Exists(fileUploadLocation))
                Directory.CreateDirectory(fileUploadLocation);

            return fileUploadLocation;
        }

___________________________
 write below code in your webconfig file under appsetting

<appSettings>
 
    <add key="importsPath" value="C:\LocalFiles\" />
   
  </appSettings




Monday 21 November 2016

join between two table in linq C#

var paymentMethos = from p in DbContext.StagingPaymentMethods
                                join c in DbContext.StagingContributions on p.ContributionGuid equals c.StagingContributionGuid
                                where c.ImportMappingGuid == importMappingGuid
                                select p;
            var creditCards = from cr in DbContext.StagingCreditCards
                              join c in DbContext.StagingContributions on cr.ContributionGuid equals c.StagingContributionGuid
                              where c.ImportMappingGuid == importMappingGuid
                              select cr;

Thursday 3 November 2016

entity framework validation

if (tran != null)



{

tran.Commit();

tran.Dispose();

}

}



catch (DbEntityValidationException dbEx)



{

foreach (var validationErrors in dbEx.EntityValidationErrors)



{

foreach (var validationError in validationErrors.ValidationErrors)



{

var xyz = string.Format("Property: {0} Error: {1}", validationError.PropertyName,validationError.ErrorMessage);

abc = !string.IsNullOrEmpty(abc) ? xyz + " HHH " : xyz;

// Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);



}

}

}