0

I'm trying to add some data from a local machine to my Azure SQL using my web API written in .net core 3.1 which is hosted on Azure App Service. But, Im getting an error on the deployed version of my web API on the app service linux. It shows that "Object reference not set to an instance of an object", but this issue is not showing while I run my API locally.
The error is showing on Log which is streaming on App Service Dashboard. code works fine when it is running on our local machine. I checked all over the code there is no issue that shows "Object reference not set to an instance of an object". Please help me out. Thanks in advance.

App Service Log

2022-07-05T04:33:34.904824178Z [41m[30mfail[39m[22m[49m: Zeus.API.Controllers.v1.Accounts.CustomerController[0] 2022-07-05T04:33:34.904862979Z Object reference not set to an instance of an object. 2022-07-05T04:33:34.904869079Z System.NullReferenceException: Object reference not set to an instance of an object. 2022-07-05T04:33:34.904873679Z at System.Data.SqlClient.SqlInternalConnectionTds.get_IsLockedForBulkCopy() 2022-07-05T04:33:34.904888480Z at System.Data.SqlClient.SqlInternalTransaction.Rollback() 2022-07-05T04:33:34.904892680Z at System.Data.SqlClient.SqlTransaction.Dispose(Boolean disposing) 2022-07-05T04:33:34.904896580Z at ZeusAPI.DataAccess.Dapper.SQLServer.DbHelper.SqlDbHelper.ExecuteAsync(String sql, DynamicParameters dp, CommandType commandType, String database) in /src/ZeusAPI.DataAccess.Dapper.SQLServer/DbHelper/SqlDbHelper.cs:line 105 2022-07-05T04:33:34.904900780Z at ZeusAPI.DataAccess.Dapper.SQLServer.Repository.Accounts.CustomerRepo.AddCustomer(IList1 customerList) in /src/ZeusAPI.DataAccess.Dapper.SQLServer/Repository/Accounts/CustomerRepo.cs:line 67 2022-07-05T04:33:34.904904880Z at ZeusAPI.Domain.Entities.Accounts.CustomerEntity.AddCustomer(IList1 customerList) in /src/ZeusAPI.Domain/Entities/Accounts/CustomerEntity.cs:line 51 2022-07-05T04:33:34.904908980Z at Zeus.API.Controllers.v1.Accounts.CustomerController.AddCustomer(IList`1 customerList) in /src/Zeus.API/Controllers/v1/Accounts/CustomerController.cs:line 59

Code on Repo

private readonly ISqlDbHelper _sqlDbHelper;

public CustomerRepo(ISqlDbHelper sqlDbHelper)
{
    _sqlDbHelper = sqlDbHelper;
}

public async Task<bool> AddCustomer(IList<Customer> customerList)
{
    DataTable dtCustomer = new DataTable();
    dtCustomer.Columns.Add("OutletId", typeof(int));
    dtCustomer.Columns.Add("Code", typeof(string));
    dtCustomer.Columns.Add("Name", typeof(string));
    dtCustomer.Columns.Add("Address", typeof(string));
    dtCustomer.Columns.Add("Location", typeof(string));
    dtCustomer.Columns.Add("City", typeof(string));
    dtCustomer.Columns.Add("State", typeof(string));
    dtCustomer.Columns.Add("Phone", typeof(string));
    dtCustomer.Columns.Add("Phone2", typeof(string));
    dtCustomer.Columns.Add("GstIn", typeof(string));
    dtCustomer.Columns.Add("Credit", typeof(float));
    dtCustomer.Columns.Add("Debit", typeof(float));
    dtCustomer.Columns.Add("Point", typeof(float));
    if (customerList != null && customerList.Count > 0)
    {
        foreach (var customer in customerList)
        {
            dtCustomer.Rows.Add(
                customer.Outlet.Id,
                customer.Code,
                customer.Name,
                customer.Address,
                customer.Location,
                customer.City,
                customer.State,
                customer.Phone,
                customer.Phone2,
                customer.GstIn,
                customer.Credit,
                customer.Debit,
                customer.Point);
        }
    }
    var param = new DynamicParameters();
    param.Add("Customer", dtCustomer.AsTableValuedParameter("[Accounts].[UDT_Customer]"));
    var result = await _sqlDbHelper.ExecuteAsync("[Accounts].[AddCustomer]", param, CommandType.StoredProcedure, customerList[0].Outlet.HostDb.Name);
    return result > 0;
}

Code on SQL Connection Class

private readonly IConfiguration _configuration;
public string ConnectionString { get; set; }
public string CustomConnectionString { get; set; }
public string ConnectionDatabase { get; set; }
public SqlDbHelper(IConfiguration configuration, IConnectionStrings connectionStrings)
{
    ConnectionString = connectionStrings.DefaultConnection;
    CustomConnectionString = connectionStrings.CustomConnection;
    ConnectionDatabase = connectionStrings.DefaultDatabase;
    _configuration = configuration;    
}
public IDbConnection CreateConnection(string database)
{
    var connectionPath = CustomConnectionString + database + ";";
    return new SqlConnection(connectionPath);
}

public async Task<int> ExecuteAsync(string sql, DynamicParameters dp, CommandType commandType = CommandType.Text,string database="")
{
    int result;
    try
    {
        using (var _connection = CreateConnection(database))
        {
            if (_connection.State == ConnectionState.Closed)
                _connection.Open();
            using (var transaction = _connection.BeginTransaction())
            {
                try
                {
                    result = await _connection.ExecuteAsync(sql, dp, transaction, commandType: commandType);
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
    return result;
}
ThomasArdal
  • 4,999
  • 4
  • 33
  • 73
Ajay AV
  • 21
  • 4

0 Answers0