when I try to upload multiple entries to my CSV reader I get this error:
Exception Details: System.InvalidOperationException: Sequence contains no elements
on this line:
if (item.contactname != null)
{
ViewBag.name = item.contactname.First().ToString().ToUpper() +
item.contactname.Substring(1);
}
oddly when I only put one or two entries it seems to work its just a lot of entries, even manually one by one? what could be breaking this? I've checked the CSV through and the formatting seems fine.
This is the CSV
domain,contact,contactname,price,type,MJTopicsID,,,,,,,,,,,,,,,,,,
goodmenproject.com,[email protected],Melissa Drake,60,0,15,,,,,,,,,,,,,,,,,,
oneworld365.org,[email protected],Paul,60,0,26,,,,,,,,,,,,,,,,,,
thefutureofthings.com/,[email protected],Kristijan,70,0,23,,,,,,,,,,,,,,,,,,
broowaha.com,[email protected],,60,0,3,,,,,,,,,,,,,,,,,,
easytravelgroup.co.uk,[email protected],Emily,30,0,26,,,,,,,,,,,,,,,,,,
bbmlive.com,[email protected],,30,0,8,,,,,,,,,,,,,,,,,,
fluxmagazine.com,[email protected],Lee,30,0,8,,,,,,,,,,,,,,,,,,
irishdogs.ie,[email protected],Peter Banks,15,0,5,,,,,,,,,,,,,,,,,,
roberthorne.co.uk,[email protected],Emily,30,0,12,,,,,,,,,,,,,,,,,,
aspiringgentleman.com,[email protected],Jack,30,0,8,,,,,,,,,,,,,,,,,,
puretravel.com,[email protected],Julie,22,0,26,,,,,,,,,,,,,,,,,,
domstechblog.com,[email protected],Dom,45,0,23,,,,,,,,,,,,,,,,,,
nerdly.co.uk,[email protected],Phil,52,0,23,,,,,,,,,,,,,,,,,,
jcount.com,[email protected],Jony,35,0,3,,,,,,,,,,,,,,,,,,
marquetteturner.com/,[email protected],Simon,20,0,12,,,,,,,,,,,,,,,,,,
This is my reader:
[HttpPost]
[ActionName("CreateBulk")]
public ActionResult CreateBulkUpload(Identifier model)
{
var file = Request.Files["attachmentcsv"];
using (var csv = new CsvReader(new StreamReader(file.InputStream), true))
{
csv.Configuration.HeaderValidated = null;
csv.Configuration.MissingFieldFound = null;
var records = csv.GetRecords<Identifier>().ToList();
foreach (var item in records)
{
var strip = item.domain.Replace("https://www.", "").Replace("http://www.", "")
.Replace("https://", "").Replace("http://", "").Replace("www.", "");
string[] URLtests =
{"http://www." + strip, "https://www." + strip, "https://" + strip, "http://" + strip};
string[] Metric = MajesticFunctions.MajesticChecker(URLtests);
if (!(from c in db.Identifiers
select c.domain).Contains(Metric[0]))
{
if (item.contactname != null) { ViewBag.name = item.contactname.First().ToString().ToUpper() + item.contactname.Substring(1); }
var userId = User.Identity.GetUserId();
var UserTableID = db.UserTables.Where(c => c.ApplicationUserId == userId).First().ID;
var identifier = new Identifier
{
domain = Metric[0],
contact = item.contact,
contactname = ViewBag.name,
price = item.price,
type = item.type,
TrustFlow = Int32.Parse(Metric[1]),
CitationFlow = Int32.Parse(Metric[2]),
RI = Int32.Parse(Metric[3]),
MJTopicsID = item.MJTopicsID,
UserTableID = UserTableID
};
ViewBag.identifier = identifier;
db.Identifiers.Add(identifier);
db.SaveChanges();
}
else { continue; }
}
return RedirectToAction("Index");
}
}
Stack Trace:
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.First(IEnumerable`1 source) +301
Linkofy.Controllers.IdentifiersController.CreateBulkUpload(Identifier model) in C:\Users\liam\Documents\Visual Studio 2017\Linkofy4\Linkofy\Controllers\IdentifiersController.cs:229
lambda_method(Closure , ControllerBase , Object[] ) +104
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +228
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9748493
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159