I'm trying a booking system, I want to put controls on the booking aspect. I want to use If and then cases. I want to control in such a way that if number of booking is 4, then it will throw an exception and stop inserting in the database.
public ApiResult<TimeModelExtended> SaveBooking(Booking booking)
{
AchimotaGCDb repo = new AchimotaGCDb();
var result = new ApiResult<TimeModelExtended>();
try
{
booking.PlayDate = getPlayDate(booking.RefCode);
Int16 nb = getNbBooked(booking.RefCode);
if (nb == 4)
{
Exception ex = new Exception();
result.Successfull = 0;
result.InternalError = ex.Message;
result.Error = "Booking slot is full";
}
else if (nb == 0)
{
booking.BookingStatus = 1;//Booked already
}
else
{
booking.BookingStatus = 0;//Reservation already
}
repo.Insert(booking);
result.Successfull = 1;
result = GetOneteeTime(booking.RefCode);
}
catch (Exception ex)
{
result.Successfull = 0;
result.InternalError = ex.Message;
result.Error = "Error from server";
}
finally
{
repo.Dispose();
}
return result;
}
help to solve that.
If you want to throw an exception, you need to really throw it:
if (nb == 4)
{
throw new Exception("Booking slot is full.");
}
But I don't think throwing an exception is a good idea. Throwing an exception and validation is a different thing.
Here is my suggestion:
if (nb == 4)
{
return result = new ApiResult<TimeModelExtended>()
{
Successfull = 0,
InternalError = "Other messages",
Error = ""Booking slot is full."
};
}
This will return as result message that nothing will continue unless you satisfy that nb != 4
Related
I'm fairly new to EF transactions, this is the code that is used for saving
public bool Save(TbArea area, bool isNew, out string errMsg)
{
try
{
errMsg = string.Empty;
using (var oScope = new System.Transactions.TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(120)))
{
try
{
TbArea oEntity = oContext.TbArea.Where(a => a.AreaCode == area.AreaCode && a.CompanyId == MainClass.SystemCompanyId).FirstOrDefault();
if (oEntity != null)
{
if (isNew) { errMsg = Resources.ResSales.MsgRecordCodeDublicated; return false; }
oContext.TbArea.Attach(oEntity);
oContext.Entry(oEntity).CurrentValues.SetValues(area);
}
else
{
if (!isNew) { errMsg = Resources.ResSales.MsgRecordNotFoundInDB; return false; }
oContext.TbArea.Add(area);
}
oContext.SaveChangesAsync();
oScope.Complete();
return true;
}
catch (Exception ex)
{
oScope.Dispose();
errMsg = ex.Message;
return false;
}
}
I'm overriding SaveChangesAsync
so that I can save the ChangeTracker.Entries into the database.
this is a portion of the code:
dbContext.AcceptAllChanges();
logsSet.AddRange(audits);
int result = 0;
try
{
result = await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
//scope.Complete(); there was a transaction that I commented out, I thought it might overlaps the original transaction!
return result;
}
catch (Exception ex)
{
var m = ex.Message;
return result;
}
When I save an item, I get the error :
the transaction has aborted
when I remove the transaction scope the saving happens normally!
Your code is marking the transaction complete before the changes have finished saving:
oContext.SaveChangesAsync();
oScope.Complete();
You need to use await:
await oContext.SaveChangesAsync();
oScope.Complete();
If you're in a context where await can resume on a different thread, you'll probably also need to specify TransactionScopeAsyncFlowOption.Enabled.
I have an web application where I parse a csv file that can have over 200k records in it. I parse each line for information, verify that the key does not exist in the database and then add it to the context. When the count reaches 10,000 records it calls SaveChanges routine. The problem is that there can be duplicates in the context and it errors out. This is running on a Azure VM communicating to an Azure SQL server.
Two questions, how do I handle the duplicate issue and is there any way I can improve the speed as it takes several hours to run?
using (LoanFileEntities db = new LoanFileEntities())
{
db.Configuration.AutoDetectChangesEnabled = false; // 1. this is a huge time saver
db.Configuration.ValidateOnSaveEnabled = false; // 2. this can also save time
while (parser.Read())
{
counter++;
int loan_code = 0;
string loan_code_string = parser["LoanId"];
string dateToParse = parser["PullDate"].Trim();
DateTime date_pulled;
try
{
date_pulled = DateTime.Parse(dateToParse, CultureInfo.InvariantCulture);
}
catch (Exception)
{
throw new Exception("No Pull Date for line " + counter);
}
string originationdate = parser["OriginationDate"].Trim();
DateTime date_originated;
try
{
date_originated = DateTime.Parse(originationdate, CultureInfo.InvariantCulture);
}
catch (Exception)
{
throw new Exception("No Origination Date for line " + counter);
}
dateToParse = parser["DueDate"].Trim();
DateTime date_due;
try
{
date_due = DateTime.Parse(dateToParse, CultureInfo.InvariantCulture);
}
catch (Exception)
{
throw new Exception("No Due Date for line " + counter);
}
string region = parser["Region"].Trim();
string source = parser["Channel"].Trim();
string password = parser["FilePass"].Trim();
decimal principalAmt = Convert.ToDecimal(parser["Principal"].Trim());
decimal totalDue = Convert.ToDecimal(parser["TotalDue"].Trim());
string vitaLoanId = parser["VitaLoanId"];
var toAdd =
db.dfc_LoanRecords.Any(
x => x.loan_code_string == loan_code_string);
if (!toAdd)
{
dfc_LoanRecords loan = new dfc_LoanRecords();
loan.loan_code = loan_code;
loan.loan_code_string = loan_code_string;
loan.loan_principal_amt = principalAmt;
loan.loan_due_date = date_due;
loan.date_pulled = date_pulled;
loan.date_originated = date_originated;
loan.region = region;
loan.source = source;
loan.password = password;
loan.loan_amt_due = totalDue;
loan.vitaLoanId = vitaLoanId;
loan.load_file = fileName;
loan.load_date = DateTime.Now;
switch (loan.region)
{
case "UK":
if (location.Equals("UK"))
{
//db.dfc_LoanRecords.Add(loan);
if (loan.source == "Online")
{
counter_new_uk_online++;
}
else
{
counter_new_uk_retail++;
}
}
break;
case "US":
if (location.Equals("US"))
{
db.dfc_LoanRecords.Add(loan);
if (loan.source == "Online")
{
counter_new_us_online++;
}
else
{
counter_new_us_retail++;
}
}
break;
case "Canada":
if (location.Equals("US"))
{
db.dfc_LoanRecords.Add(loan);
if (loan.source == "Online")
{
counter_new_cn_online++;
}
else
{
counter_new_cn_retail++;
}
}
break;
}
// delay save to speed up load. 3. also saves transactional time
if (counter % 10000 == 0)
{
db.SaveChanges();
}
}
} // end of parser read
db.SaveChanges();
}
}
}
I would suggest removing duplicates in the code before sending it over to .SaveChanges().
Instead of going into detail about duplicate removal, I've put together this list of links to existing questions and answers on StackOverflow that may help:
Delete duplicates using Lambda
Using DISTINCT on a subquery to remove duplicates in Entity Framework
Using LINQ to find / delete duplicates
Hope that helps!
I am trying to move the activities on some of our contacts/accounts to a new_private_contact_details entity. I have all of them but the phonecall working. The below code does seem to work, but it ends up showing the phonecall on the activity feed for both the new_private_contact_details entity as well as the existing Contact entity. Obviously this is a problem, since I'm trying to migrate those kinds of details to the private details, so having them still show up voids that process.
if (phoneCallsList != null && phoneCallsList.Count > 0)
{
foreach (PhoneCall pc in phoneCallsList)
{
if (pc.StatusCode.Value != 1)
{
int oldState = 0; //for the beginning statecode
int oldStatus = 0; //for the beginning statuscode
if (pc.StatusCode.Value == 3)
{
oldState = 2;
oldStatus = 3;
}
else
{
oldState = 1;
oldStatus = pc.StatusCode.Value;
}
//change status to open
SetStateRequest setStateRequest = new SetStateRequest()
{
EntityMoniker = new EntityReference
{
Id = pc.Id,
LogicalName = pc.LogicalName
},
State = new OptionSetValue(0),
Status = new OptionSetValue(1)
};
try
{
crm.Execute(setStateRequest);
}
catch (Exception ex)
{
throw new Exception("Error: " + ex);
}
pc.RegardingObjectId = pcd.ToEntityReference();
pc.StatusCode.Value = 1;
try
{
service.Update(pc);
}
catch (Exception ex)
{
throw new Exception("Error: " + ex);
}
//return status to closed
setStateRequest = new SetStateRequest()
{
EntityMoniker = new EntityReference
{
Id = pc.Id,
LogicalName = pc.LogicalName
},
State = new OptionSetValue(oldState),
Status = new OptionSetValue(oldStatus)
};
try
{
crm.Execute(setStateRequest);
}
catch (Exception ex)
{
throw new Exception("Error: " + ex);
}
}
else
{
pc.RegardingObjectId = pcd.ToEntityReference();
try
{
service.Update(pc);
}
catch (Exception ex)
{
throw new Exception("Error: " + ex);
}
}
}
}
I have already handled for when the phonecall is completed/closed. I'm updating the RegardingObjectId, but it doesn't remove it from the original entity, and deleting it in CRM from either entity, deletes it from both.
Again, I only seem to be having this issue with the phonecall entity. This code works perfectly for the others, i.e., appointments, tasks, letters, and emails
I figured out where the problem was occurring. It showed up on both entities, because for PhoneCalls there is also a PhoneCalls.To field that still referenced the old Contact entity. Unfortunately this field requires a PartyList, which, from what I've found, cannot be customized, so it must always point to a Contact, Lead, Opportunity or Account entity.
I have an application using the office interop libraries (Word and Excel 2007) library version 12.0 and I have a generic method for executing tasks with a timeout in case Office stops responding for some reason but when the method returns its value it throws an exception stating the Com object can't be used when separated from its rcw.
Method:
public static R ExecuteOfficeTimoutAction<R>(Func<R> ToExecute, TimeSpan TimeOut)
{
object returnVal = new OfficeEmptyReturn(); // Empty Class Used With Reflection To Tell If There Was Really A Value Returned
try
{
Exception threadError = null;
System.Threading.Thread timedThread = new System.Threading.Thread(() =>
{
try
{
returnVal = ToExecute();
}
catch (Exception ex)
{
threadError = ex;
}
});
timedThread.Priority = System.Threading.ThreadPriority.Normal;
timedThread.SetApartmentState(System.Threading.ApartmentState.STA);
timedThread.Start();
timedThread.Join(TimeOut);
if (threadError != null)
{
throw threadError;
}
if (timedThread.ThreadState != System.Threading.ThreadState.Stopped)
{
bool abortCalled = false;
bool processKilled = false;
int waitTime = 0;
do
{
if (!abortCalled)
{
timedThread.Abort();
abortCalled = true;
}
if (waitTime < 2500)
{
waitTime += 250;
System.Threading.Thread.Sleep(250);
}
else if (!processKilled)
{
processKilled = true;
foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses())
{
if (process.ProcessName.ToLower().Contains("winword.exe") || process.ProcessName.ToLower().Contains("excel.exe"))
{
process.Close();
}
}
}
else
{
throw new TimeoutException("The Timeout Action Could Not Be Terminated");
}
}
while (!(timedThread.ThreadState == System.Threading.ThreadState.Aborted || timedThread.ThreadState == System.Threading.ThreadState.Stopped));
}
}
catch (Exception)
{
throw;
}
return (R)returnVal; // Throws Exception Here
}
Method Where It Throws An Error:
public Document GetDocument(string FilePath)
{
Document newDoc = null;
if (!String.IsNullOrWhiteSpace(FilePath))
{
if (File.Exists(FilePath) && IsWordDoc(new FileInfo(FilePath)))
{
if (IsActive)
{
if (FilePath.Length < 255)
{
newDoc = ExecuteOfficeTimoutAction<Application, Document>((Application app) =>
{
Document tempDoc = app.Documents.Open(FilePath, Type.Missing, true);
return tempDoc;
}, App, new TimeSpan(0, 0, 10));
if (newDoc == null || newDoc is OfficeEmptyReturn)
{
newDoc = null;
}
}
else
{
throw new ArgumentOutOfRangeException("The Word Document's Full Path Must Be Shorter Than 255 Characters To Be Reached");
}
}
else
{
throw new InvalidOperationException("The Word Application Must Be Open To Process Word Data");
}
}
else
{
throw new FileNotFoundException("The Word File Did Not Exist At The Specified Path");
}
}
else
{
throw new ArgumentNullException("The File Path Was Null");
}
return newDoc;
}
The App object being passed in is a static Word.Application object stored in a field within the containing word class so that it is visible to all applicable methods and disposed of appropriately when no longer needed.
I don't see why it would be giving this exception when returning a value, even if it is a com object.
My desired outcome is for the method to return the value generated by the passed Func, throw the exception generated by said delegate or return OfficeEmptyReturn.
Any help or pointers with this would be greatly appreciated.
Side note, I do have fallback error handling for when the office application is terminated outside the application automating it, I would open the task manager and manually terminate word when it froze my code then it would continue but that is not an acceptable outcome.
I can't figure this out why I keep getting the compilation error: "not all code paths return a value". I am writing a simple class method that is supposed to return true if the account is available to use and false if the account is not available or is null/empty. The code for the method is below:
public static bool AccountAvailable(int AccountId)
{
try
{
bool accountavailable;
string queryTransaction = "Select Count(AccountID) FROM Accounts WHERE AccountID = " + AccountId.ToString() + " AND AccountUsed = 0";
//grab a connection to the database
Database database = DatabaseFactory.CreateDatabase();
//create an instance of the command
DbCommand command = database.GetSqlStringCommand(queryTransaction);
object dataobject = command.ExecuteScalar();
if (dataobject == null || string.IsNullOrEmpty(Convert.ToString(dataobject)))
{
accountavailable = false;
}
else if (Convert.ToInt32(dataobject) == 0)
{
accountavailable = false;
}
else if (Convert.ToInt32(dataobject) > 0)
{
accountavailable = true;
}
else
{
accountavailable = true;
}
return accountavailable;
}
catch
{
}
}
Any help or advice on this would be appreciated. Thanks!!
If an exception is thrown in your code before you return a value then control moves to the catch block. It then reaches the end of the method without returning anything.
Either return something within, or after, the catch block.
In your catch block, add a return:
catch (Exception ex)
{
// your code
return null;
}
The suggest to try this code
public static bool AccountAvailable(int AccountId)
{
bool accountavailable = false;
try
{
string queryTransaction = "Select Count(AccountID) FROM Accounts WHERE AccountID = " + AccountId.ToString() + " AND AccountUsed = 0";
//grab a connection to the database
Database database = DatabaseFactory.CreateDatabase();
//create an instance of the command
DbCommand command = database.GetSqlStringCommand(queryTransaction);
object dataobject = command.ExecuteScalar();
if (dataobject == null || string.IsNullOrEmpty(Convert.ToString(dataobject)))
{
accountavailable = false;
}
else if (Convert.ToInt32(dataobject) == 0)
{
accountavailable = false;
}
else if (Convert.ToInt32(dataobject) > 0)
{
accountavailable = true;
}
else
{
accountavailable = true;
}
}
catch
{
}
return accountavailable;
}