DapperLib / DapperLib/Dapper.Contrib

Dapper.insert

Open
#72 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
293
Forks
109
PR merge metrics
No merged PRs in 30d

Description

public string AddItem(BASE_ITEMS Obj)
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ERPDEMO"].ConnectionString))
{
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
DynamicParameters ObjParm = new DynamicParameters();
try
{

                ObjParm.Add("@ItemCode", Obj.ItemCode);
                ObjParm.Add("@AdditionalCode", Obj.AdditionalCode);
                ObjParm.Add("@PartNumber", Obj.PartNumber);
                ObjParm.Add("@Name", Obj.Name);
                ObjParm.Add("@Description", Obj.Description);
                ObjParm.Add("@ItemTypeId", Obj.ItemTypeId);
                ObjParm.Add("@CategoryId", Obj.CategoryId);
                ObjParm.Add("@UserId", Obj.EntryUserId);
                ObjParm.Add("@newId", 0, DbType.Int32, direction: ParameterDirection.Output);
                ObjParm.Add("@errorId", 0, DbType.Int32, ParameterDirection.Output);
                connection.Execute("BASE_Item_Advanced_ADDYYY", ObjParm, commandType: CommandType.StoredProcedure);
                transaction.Commit();
               

            }
            catch (Exception e)
            {
                transaction.Rollback();
            }
            finally
            {
                connection.Close();
            }

System.InvalidOperationException was unhandled
Message=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.
Source=System.Data
StackTrace:
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction, String sql, Action2 paramReader, Object obj, Nullable1 commandTimeout, Nullable1 commandType) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Dapper\SqlMapper.cs:line 836 at CallSite.Target(Closure , CallSite , Type , IDbConnection , IDbTransaction , String , Object , Object , Nullable1 , Nullable1 ) at System.Dynamic.UpdateDelegates.UpdateAndExecute8[T0,T1,T2,T3,T4,T5,T6,T7,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) at CallSite.Target(Closure , CallSite , Type , IDbConnection , IDbTransaction , String , Object , Object , Nullable1 , Nullable1 ) at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable`1 commandType) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Dapper\SqlMapper.cs:line 176
at Dapper.Contrib.Extensions.SqlMapperExtensions.Insert[T](IDbConnection connection, T entityToInsert) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Dapper\Extensions\SqlMapperExtensions.cs:line 175
at CodeSlice.Data.Profilers.ProfileInsert.b__2() in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ProfileInsert.cs:line 51
at CodeSlice.Data.Profilers.ActionProfiler.Profile(String description, Action actionToProfile) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ActionProfiler.cs:line 16
at CodeSlice.Data.Profilers.ProfileInsert.Run() in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ProfileInsert.cs:line 39
at CodeSlice.Data.Profilers.ProfileInsert.Exec() in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Profilers\ProfileInsert.cs:line 18
at CodeSlice.Data.Program.Main(String[] args) in C:\Projects\microorm-comparison\CodeSlice.Data\CodeSlice.Data\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing insert call chain in Dapper/SqlMapper.cs, Dapper/Extensions/SqlMapperExtensions.cs, and Profilers/ProfileInsert.cs, using the stack trace and transaction error as the reproduction context. Trace how the connection and transaction reach Execute, then verify the insert path behaves correctly when a local transaction is pending.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.