public HttpResponseMessage Register() { DbContextTransaction transaction = entities.Database.BeginTransaction(); string imageName = null; string imageName1 = null; string imageName2 = null; var httpRequest = HttpContext.Current.Request; //Upload Image try { var postedFile = httpRequest.Files["Image"]; var postedFile1 = httpRequest.Files["Pan"]; var postedFile2 = httpRequest.Files["TraderLicense"]; //Create custom filename imageName = new String(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Image/" + imageName); postedFile.SaveAs(filePath); imageName1 = new String(Path.GetFileNameWithoutExtension(postedFile1.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName1 = imageName1 + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile1.FileName); var filePath1 = HttpContext.Current.Server.MapPath("~/Image/" + imageName1); postedFile1.SaveAs(filePath1); imageName2 = new String(Path.GetFileNameWithoutExtension(postedFile2.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName2 = imageName2 + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile2.FileName); var filePath2 = HttpContext.Current.Server.MapPath("~/Image/" + imageName2); postedFile2.SaveAs(filePath2); tblBidder br = new tblBidder(); //Save to db using (dbFarmerScheme3Entities db = new dbFarmerScheme3Entities()) { string email = httpRequest["Email"]; var e = db.tblBidders.Where(x => x.bEmailId == email).FirstOrDefault(); if (e == null) { string acc = httpRequest["AccountNo"]; var a = db.tblBidders.Where(x => x.bAccountNo == acc).FirstOrDefault(); if (a == null) { try { br.bAadhar = filePath; br.bUserName = httpRequest["Username"]; br.bContactNo = httpRequest["Contact"]; br.bEmailId = httpRequest["Email"]; br.bAddress = httpRequest["Address"]; br.bCity = httpRequest["City"]; br.bState = httpRequest["State"]; br.bPincode = httpRequest["Pincode"]; br.bAccountNo = httpRequest["AccountNo"]; br.bIFSCcode = httpRequest["ifsccode"]; br.bPan = filePath1; br.bTraderLicense = filePath2; br.bPassword = httpRequest["Password"]; br.StatusOfBidderDocx = "pending"; entities.tblBidders.Add(br); entities.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Account No already exist")); } } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Email already exist")); } } } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "File size should be less than 2MB")); } return(Request.CreateResponse(HttpStatusCode.Created, "Successfully Registered")); }