Currently playing around with Dapper I'm trying to insert values into the db as follows
using (var sqlCon = new SqlConnection(Context.ReturnDatabaseConnection()))
{
sqlCon.Open();
try
{
var emailExists = sqlCon.Query<UserProfile>(#"SELECT UserId FROM User_Profile WHERE EmailAddress = #EmailAddress",
new { EmailAddress = userRegister.EmailAddress.Trim() }).FirstOrDefault();
if (emailExists == null) // No profile exists with the email passed in, so insert the new user.
{
userProfile.UniqueId = Guid.NewGuid();
userProfile.Firstname = userRegister.Firstname;
userProfile.Surname = userRegister.Surname;
userProfile.EmailAddress = userRegister.EmailAddress;
userProfile.Username = CreateUsername(userRegister.Firstname);
userProfile.Password = EncryptPassword(userRegister.Password);
userProfile.AcceptedTerms = true;
userProfile.AcceptedTermsDate = System.DateTime.Now;
userProfile.AccountActive = true;
userProfile.CurrentlyOnline = true;
userProfile.ClosedAccountDate = null;
userProfile.JoinedDate = System.DateTime.Now;
userProfile.UserId = SqlMapperExtensions.Insert(sqlCon, userProfile); // Error on this line
Registration.SendWelcomeEmail(userRegister.EmailAddress, userRegister.Firstname); // Send welcome email to new user.
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
sqlCon.Close();
}
}
The error I get is
ExecuteNonQuery requires the command to have a transaction when the connection
assigned to the command is in a pending local transaction. The Transaction
property of the command has not been initialized.
I have googled this error, but I misunderstood the answers provided.
From the error message I assume that you have started a transaction that was neither committed nor rolled back. The real cause for this error message is elsewhere.
I suggest you to log requests in Context.ReturnDatabaseConnection() and trace what requests precede this error.
Also I advice you to look in your code for all transactions and check if they are correctly completed (commit/rollback).
Related
This seems to be a problem either with my computer or the azure sql servers. The problem I have was not a problem before but the code all of a sudden acting strange. Here is my code that is used to work. Connection opens successfully, my executenonquery works fine but reader acts strange.
public static List<ClientDto> GetClientInformation()
{
List<ClientDto> results = new List<ClientDto>();
try
{
using (var sqlConnection =
new SqlConnection(ConfigurationManager.ConnectionStrings["ClientData"].ConnectionString))
{
using (var command = new SqlCommand(null, sqlConnection))
{
sqlConnection.Open();
command.CommandType = CommandType.StoredProcedure;
//running the GetClientInformation stored procedure in DB
command.CommandText = Constants.GetClientInformation;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
results.Add(new ClientDto()
{
Id = Convert.ToInt32(reader["Id"].ToString()),
DealerName = reader["DealerName"].ToString(),
GroupID = reader["GroupID"].ToString(),
DealerId = reader["DealerId"].ToString(),
DealerFolderName = reader["DealerFolder"].ToString(),
DMSType = reader["DMSType"].ToString(),
MAIsActive = Convert.ToBoolean(Convert.ToInt32(reader["MAIsActive"])),
SalesIsActive = Convert.ToBoolean(Convert.ToInt32(reader["SalesIsActive"])),
SalesSource = reader["SalesSource"].ToString(),
InventoryIsActive = Convert.ToBoolean(Convert.ToInt32(reader["InventoryIsActive"])),
InventorySource = reader["InventorySource"].ToString(),
AppointmentsIsActive =
Convert.ToBoolean(Convert.ToInt32(reader["AppointmentsIsActive"])),
AppointmentsSource = reader["AppointmentsSource"].ToString(),
LeadsIsActive = Convert.ToBoolean(Convert.ToInt32(reader["LeadsIsActive"])),
LeadsSource = reader["LeadsSource"].ToString(),
ServiceIsActive = Convert.ToBoolean(Convert.ToInt32(reader["ServiceIsActive"])),
ServiceSource = reader["ServiceSource"].ToString(),
SalesIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["SalesIsDelete"])),
SalesDeleteRange = reader["SalesDeleteRange"].ToString(),
InventoryIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["InventoryIsDelete"])),
InventoryDeleteRange = reader["InventoryDeleteRange"].ToString(),
AppointmentsIsDelete =
Convert.ToBoolean(Convert.ToInt32(reader["AppointmentsIsDelete"])),
AppointmentsDeleteRange = reader["AppointmentsDeleteRange"].ToString(),
LeadsIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["LeadsIsDelete"])),
LeadsDeleteRange = reader["LeadsDeleteRange"].ToString(),
ServiceIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["ServiceIsDelete"])),
ServiceDeleteRange = reader["ServiceDeleteRange"].ToString(),
IsActive = Convert.ToBoolean(Convert.ToInt32(reader["IsActive"])),
UserDefinedName = reader["UserDefinedName"].ToString()
});
}
}
}
return results;
}
catch (Exception ex)
{
//If an exception happens it will insert the error to errorlog table and to the log file
Logger.WriteError(new ErrorDto()
{
Source = "GetClientInformation",
Message = ex.Message,
StackTrace = ex.StackTrace
});
Console.WriteLine(string.Format("Error - {0} - {1} ", "GetClientInformation", ex.Message));
return results;
}
}
There is data in the sql, the executable of my application works fine on our virtual machine (Windows Server).
Only strange thing I see in the code is that SQLConnection have this innerexception (it is not stopping the code to run though)
ServerVersionNormalized = 'sqlConnection.InnerConnection.ServerVersionNormalized' threw an exception of type 'System.NotSupportedException'
I am not sure if this was an issue before or not.
This picture shows that Enamration yielded no result
This picture shows that reader has rows
I am reinstalling my visual studio now to see if it will fix the issue.
This issue is happening in both Visual Studio 2015 and 2017 for me.
This is my connection string format:
Server=.database.windows.net,1433;Database=; User ID=;Password=;Trusted_Connection=False; Encrypt=True;
It looks like it is working fine now. To me it sounds like since I had breakpoint in the code, it might have caused the issue because of the longer time between connecting and running the query.
I'm trying to propagate an error back to the user if their input is wrong.
Here's the code that should throw the error:
var sql2 = "select COD_id from COD_Codes where COD_name = #Value and COD_COT_id = #codeId";
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandText = sql2;
cmd2.Parameters.AddWithValue("#Value", value);
cmd2.Parameters.AddWithValue("#codeId", result);
cmd2.Connection = connection;
cmd2.Transaction = transaction;
var code = Convert.ToInt32(cmd2.ExecuteScalar());
if (code == 0)
{
throw new ApplicationException(string.Format(#"({0}) is not a valid data for this credit report. Please check with your sys admin if this is a new data value", code));
}
return code;
This code is a few levels below my controller which looks like this:
[HttpPost]
public HttpResponseMessage SaveCreditReport(GrowData growData)
{
using (SqlConnection connection = CreateSqlConnection())
{
using (var transaction = connection.BeginTransaction())
{
try
{
var service = CreateCreditService(connection, transaction);
var user = GetUser(service);
if (log.IsInfoEnabled)
log.InfoFormat("Save credit report called by user '{0}' ",
user.UserName);
var growValues = growData.GrowRows.Select(growRow => new GrowValue() { Mnemonic = growRow.Mnemonic, Time = growRow.Time, Value = growRow.Value }).ToList();
var growCompValues = growData.GrowCompRows.Select(compRow => new GrowCompRowValue() { Mnemonic = compRow.Mnemonic, Value = compRow.Value }).ToList();
var reportId = service.SaveGrowCreditData(user.Id, growData.CompanyId, growValues, growCompValues, growData.Publish);
var message = this.Request.CreateResponse<int?>(HttpStatusCode.OK, reportId);
transaction.Commit();
return message;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}
I'm not sure how and what kind of exception I need to throw to propagate the error back to user level. I don't want the standard "500 internal server error" message but instead the one that I've formatted in the code.
Can I just throw a new httpresponseexception instead of ApplicationException directly from the code? Will it then propagate back to controller level and unto the user?
In your catch method use CreateErrorResponse to create a response with whatever HttpStatusCode is appropriate:
catch (ApplicationException ex)
{
transaction.Rollback();
return this.Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, ex);
}
A caveat from the documentation:
The request must be associated with an HttpConfiguration instance. An HttpResponseMessage whose content is a serialized representation of an HttpError instance [is returned]
I have a code that adds data to two EntityFramework 6 DataContexts, like this:
using(var scope = new TransactionScope())
{
using(var requestsCtx = new RequestsContext())
{
using(var logsCtx = new LogsContext())
{
var req = new Request { Id = 1, Value = 2 };
requestsCtx.Requests.Add(req);
var log = new LogEntry { RequestId = 1, State = "OK" };
logsCtx.Logs.Add(log);
try
{
requestsCtx.SaveChanges();
}
catch(Exception ex)
{
log.State = "Error: " + ex.Message;
}
logsCtx.SaveChanges();
}
}
}
There is an insert trigger in Requests table that rejects some values using RAISEERROR. This situation is normal and should be handled by the try-catch block where the SaveChanges method is invoked. If the second SaveChanges method fails, however, the changes to both DataContexts must be reverted entirely - hence the transaction scope.
Here goes the error: when requestsCtx.SaveChanges() throws a exception, the whole Transaction.Current has its state set to Aborted and the latter logsCtx.SaveChanges() fails with the following:
TransactionException:
The operation is not valid for the state of the transaction.
Why is this happening and how do tell EF that the first exception is not critical?
Really not sure if this will work, but it might be worth trying.
private void SaveChanges()
{
using(var scope = new TransactionScope())
{
var log = CreateRequest();
bool saveLogSuccess = CreateLogEntry(log);
if (saveLogSuccess)
{
scope.Complete();
}
}
}
private LogEntry CreateRequest()
{
var req = new Request { Id = 1, Value = 2 };
var log = new LogEntry { RequestId = 1, State = "OK" };
using(var requestsCtx = new RequestsContext())
{
requestsCtx.Requests.Add(req);
try
{
requestsCtx.SaveChanges();
}
catch(Exception ex)
{
log.State = "Error: " + ex.Message;
}
finally
{
return log;
}
}
}
private bool CreateLogEntry(LogEntry log)
{
using(var logsCtx = new LogsContext())
{
try
{
logsCtx.Logs.Add(log);
logsCtx.SaveChanges();
}
catch (Exception)
{
return false;
}
return true;
}
}
from the documentation on transactionscope: http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope%28v=vs.110%29.aspx
If no exception occurs within the transaction scope (that is, between
the initialization of the TransactionScope object and the calling of
its Dispose method), then the transaction in which the scope
participates is allowed to proceed. If an exception does occur within
the transaction scope, the transaction in which it participates will
be rolled back.
Basically as soon as an exception is encountered, the transaction is rolled back (as it seems you're aware) - I think this might work but am really not sure and can't test to confirm. It seems like this goes against the intended use of transaction scope, and I'm not familiar enough with exception handling/bubbling, but maybe it will help! :)
I think I finally figured it out. The trick was to use an isolated transaction for the first SaveChanges:
using(var requestsCtx = new RequestsContext())
using(var logsCtx = new LogsContext())
{
var req = new Request { Id = 1, Value = 2 };
requestsCtx.Requests.Add(req);
var log = new LogEntry { RequestId = 1, State = "OK" };
logsCtx.Logs.Add(log);
using(var outerScope = new TransactionScope())
{
using(var innerScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
requestsCtx.SaveChanges();
innerScope.Complete();
}
catch(Exception ex)
{
log.State = "Error: " + ex.Message;
}
}
logsCtx.SaveChanges();
outerScope.Complete();
}
}
Warning: most of the articles about RequiresNew mode discourage using it due to performance reasons. It works perfectly for my scenario, however if there are any side effects that I'm unaware of, please let me know.
I am using session.query,first time its return latest database records and we populate on grid after editing on fields we save on database and then we again execute this query then it is not getting the updated record from database. the fields has updated in database but not getting updated record.
My NHibernate query is given below
List<FiscalPeriod> lstPeriods = new List<FiscalPeriod>();
using (var tr = session.BeginTransaction())
{
try
{
List<TSCFiscalPeriod> lstFiscalPeriods = session.Query<TSCFiscalPeriod>().ToList();
if (lstFiscalPeriods != null && lstFiscalPeriods.Count > 0)
{
foreach (var period in lstFiscalPeriods)
{
FiscalPeriod objPeriod = new FiscalPeriod();
objPeriod.Period = period.Id.FPeriod;
objPeriod.Name = "Period " + objPeriod.Period;
objPeriod.CompanyID = period.Id.FCompanyID;
objPeriod.Year = period.Id.FFiscalYear;
objPeriod.Begin = period.FBeginDate;
objPeriod.End = period.FEndDate;
objPeriod.OpenAP = FOpenAP(period.FOpen);
objPeriod.OpenGL = FOpenGL(period.FOpen);
objPeriod.SCFiscalPeriod = period;
objPeriod.IsExisting = true;
lstPeriods.Add(objPeriod);
}
}
tr.Commit();
}
catch (Exception ex)
{
lstPeriods = null;
CusException cex = new CusException(ex);
cex.Write();
}
return lstPeriods;
}
Is there any refresh method or something i am doing wrong.
Any help will be appreciated.thanks
make sure you are using session.clear() or session.flush() after save the session.
Nhibernate session is a static initializer.
Sometimes session is not able to refresh values.
So You have to flush and clear the session
session.flush();
session.clear();
I am LINQ to input information from a Database. I have my try.catch block set up to catch these exceptions. However I believe I ran into a sore spot where I am attempting to see what the message is but it just bypass printing the message to me and goes directly to error page. Here is an example of the code I have so far. I would love to get some input on why this seems to be acting so strange.
private void CreateEntry()
{
var date = DateTime.Today;
var version = (from v in house.StayLateVersions
where v.Active
select v).FirstOrDefault();
if (version == null)
{
throw new NullReferenceException();
}
//Try to create an entry for the database. Upon failure, sends the exception to ThrowDbError();
try
{
ResidenceHallInspection rhi = new ResidenceHallInspection();
rhi.versionId = version.id;
rhi.submitDate = DateTime.Now;
rhi.CheckInOrOut = ddlCheck.SelectedItem.Text;
rhi.Id = txtId.Text;
rhi.FirstName = txtFirstName.Text;
rhi.MiddleName = txtMiddleName.Text;
rhi.LastName = txtLastName.Text;
rhi.Walls = chbxWalls.SelectedItem.Text;
rhi.Windows = chbxWindows.SelectedItem.Text;
rhi.Blinds = chbxBlinds.SelectedItem.Text;
rhi.Couch = chbxCouch.SelectedItem.Text;
rhi.CommonRoomCouch = chbxCRCouch.SelectedItem.Text;
rhi.CommonRoomChair = chbxCRChair.SelectedItem.Text;
rhi.Doors = chbxDoors.SelectedItem.Text;
rhi.Carpet = chbxCarpet.SelectedItem.Text;
rhi.Ceiling = chbxCeiling.SelectedItem.Text;
rhi.CommonRoomCounter = chbxCRCounter.SelectedItem.Text;
rhi.Cabinet = chbxCabinet.SelectedItem.Text;
rhi.Phone = chbxPhone.SelectedItem.Text;
rhi.Bed = chbxBed.SelectedItem.Text;
rhi.Desk = chbxDesk.SelectedItem.Text;
rhi.DeskChairs = chbxDeskChair.SelectedItem.Text;
rhi.Tub = chbxTub.SelectedItem.Text;
rhi.Vanity = chbxVanity.SelectedItem.Text;
rhi.Notes = txtNotes.Text;
rhi.Building = txtResHall.Text;
rhi.ApartmentNumber = txtSuitNo.Text;
rhi.BedSpace = txtBedSpace.Text;
house.AddToResidenceHallInspections(rhi);
house.SaveChanges();
}
catch (Exception oe)
{
ThrowDbError(oe);
Response.Write(oe.InnerException);
}
}
/*=================================================*/
/*Possible Errors */
/*=================================================*/
private void ThrowDbError(Exception oe)
{
Response.Write(oe.Source);
house.Dispose();
Session.Contents.Add("FormException", oe);
Response.Redirect("/Database-Error/", true);
}
The most likely reason for that to happen is that you are running the database version query outside the try/catch block. Any exception in this db access code will not be handled by the code you have shown above.
Try extending your try block to also include the db access code:
var version = (from v in house.StayLateVersions
where v.Active
select v).FirstOrDefault();
if (version == null)
{
throw new NullReferenceException();
}
and see if this time the error is caught.