c# - An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code -
i have console application generates set of fake data/information on database testing.
public list<lcs_exchangedatastaging> generatenewdata(int count) { var newdata = new list<lcs_exchangedatastaging>(); var random = new random(); var branches = new lcsbranchcontext().getallbranches(); var schemeids = new lcsschemeportfoliocontext().schemeportfolios(); var branchcount = branches.count - 1; var schemecount = schemeids.count - 1; parallel.for(1, count + 1, => { int gender = random.next(0, 1); var data = new lcs_exchangedatastaging { accountnumber = (3000000000 + random.next(999999999)).tostring(), accountstatement = random.next(), address = faker.address.streetaddress(), addressofkeycontact = faker.address.streetaddress(), age = randomdate(datetime.now.addyears(-70), datetime.now.addyears(-18), random), amountdisbursed = faker.randomnumber.next(20000000), amountoverdue = faker.randomnumber.next(1000000), branchname = branches[random.next(branchcount)].name, clientid = $"r{(faker.randomnumber.next(99999999) + 200000000).tostring()}", clientname = gender == 0 ? namegenerator.generate(gender.male) : namegenerator.generate(gender.female), companiesmandate = "", dateofincorporation = randomdate(datetime.now.addyears(-70), datetime.now, random), dayspastdue = 1, disbursmentdate = randomdate(datetime.now.addyears(-3), datetime.now, random), email = faker.internet.email(), emergencycontact = faker.name.fullname(), employeecontact = faker.name.fullname(), employeecontacttelephone = faker.phone.number(), employeraddress = faker.address.streetaddress(), employername = faker.company.name(), employertelephone = faker.phone.number(), expirydate = randomdate(datetime.now.addyears(1), datetime.now.addyears(7), random), feesoverdue = faker.randomnumber.next(2000), gender = gender == 0 ? "m" : "f", guarantoraddress = faker.address.streetaddress(), guarantorname = faker.name.fullname(), guarantortelephone = faker.phone.number(), income = random.next(50000, 1000000), insurancecompany = faker.company.name(), insuredamount = faker.randomnumber.next(2000), interestrate = random.next(3, 24), intrduceraddress = faker.address.streetaddress(), intrestoverdue = faker.randomnumber.next(2000), introducername = faker.name.fullname(), introducertelephone = faker.phone.number(), keycontactperson = faker.name.fullname(), lastcramount = faker.randomnumber.next(20000), lastcrdate = randomdate(datetime.now.addyears(-1), datetime.now, random), lienamount = faker.randomnumber.next(2000), loanrestructured = "no", missedpayin24monthcount = random.next(0, 15), monthlyinflow = faker.randomnumber.next(1000000), operativeaccountnumber = $"01{random.next(99999999)}", outstandingprincipal = faker.randomnumber.next(200000), penalchargedue = faker.randomnumber.next(2000), principaloverdue = faker.randomnumber.next(2000), productcategory = "", producttype = "laa", relationshipyears = random.next(0, 40), repaymentamount = faker.randomnumber.next(2000), returnedchequein6monthcount = random.next(0, 3), schemeid = schemeids[random.next(schemecount)].schemecode, telephone1 = faker.phone.number(), telephone2 = faker.phone.number(), telephoneofkeycontact = faker.phone.number(), tenor = random.next(3, 124).tostring(), }; newdata.add(data); }); return newdata; }
however, when method generatenewdata runs, exception
an exception of type 'system.argumentoutofrangeexception' occurred in mscorlib.dll not handled in user code
additional information: 'maxvalue' must greater zero.
system.argumentoutofrangeexception unhandled user code hresult=-2146233086 message='maxvalue' must greater zero. parameter name: maxvalue paramname=maxvalue source=mscorlib stacktrace: @ system.random.next(int32 maxvalue) @ rlcs.tasks.data.datagenerator.<>c__displayclass1_0.<generatenewdata>b__0(int32 i) in c:\users\solution architect\documents\work\lcs\lcs\rlcs.tasks\data\datagenerator.cs:line 50 @ system.threading.tasks.parallel.<>c__displayclass17_0`1.<forworker>b__1() innerexception:
please suggestions might have on this. thanks
it's hard tell without running or seeing of code, problem 1 of calls random.next(int32 maxvalue)
providing int32 maxvalue
of 0 or less. narrows down 1 of following lines -
branchname = branches[random.next(branchcount)].name,
schemeid = schemeids[random.next(schemecount)].schemecode,
so important variables seem branchcount
, schemecount
. these set count - 1
of respective context method results.
i suspect either lcsbranchcontext().getallbranches()
or lcsschemeportfoliocontext().schemeportfolios()
returning 1 or 0 results, can "fix" making them return more results, better solution use faker.random.listitem()
, c# 6 safe navigation operator (?
) -
branchname = faker.random.listitem(branches)?.name,
schemeid = faker.random.listitem(schemeids)?.schemecode,
which set value null
if there aren't entries in provided list.
wiki
Comments
Post a Comment