Open OracleConnection ONGet and Close it OnPost

Butachan 21 Reputation points
2023-02-08T15:07:54.8633333+00:00

Hi have a problem

I ve been struggling with stateless http request for days on my razpr .NET6 webapp

My razor page has a button that opens a modal, this triggers a method (with ajax)



 public OracleConnection connection { get; set; }

 public OracleTransaction transaction { get; set; }
public JsonResult OnPostGetOrder(string order)
        {
            connection = ConnectionManager.GetConnection();//
            transaction = connection.BeginTransaction();
....}

The connection should remain opened until the modal is closed or the form inside it is cancelled.

So I need to get connection and transaction values in the following method

 public async Task<IActionResult> OnPostCloseModal()
        {
            connection.close()
            transaction.Rollback();
...

}

Of course it won't work because of http being stateless, so connection being null

Here are all my attempt and none of it worked:

  1. [tempdata] public oracleconnection connection
    doesn't work because razor cannot store oracleconn inside tempdata
  2. [bindproperty] public oracleconnection connection with @Html.HiddenFor(model=>model.connection) or <input asp-for="connection" readonly/> don't work: the OnpostClose doesn't just trigger
  3. _httpContextAccessor.HttpContext.Items["Connection"] = connection; I have no idea what should I use to retrieve its value because VS2022 refuse the following: HttpContext.Session.Get("Connection"); HttpContext.Session["connection"]; HttpContext.session.Get<OracleConnection>("Connection") ; HttpContext.Session.TryGetValue("connection",OracleConnection);
  4. store connection and transaction in javascript then sending it back to server but Ajax s not able to pass oracleconnection to C# parameter (closeModal function doesn't trigger)

Please tell me there is something I can use to do this trivial task

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,148 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2023-02-09T06:38:31.7966667+00:00

    Hi @Butachan

    You can try to use Dependency injection and create a Singleton service.

    Like this: since I didn't install the oracle package, I just use the sql server package to test. You can change it to yours.

        //using Microsoft.Data.SqlClient;
        public interface IDataRepository
        {
            SqlConnection connection { get; set; }
            SqlConnection transaction { get; set; }
            string GetConnection();
        }
        public class DataRepository : IDataRepository
        {
            public SqlConnection connection { get; set; }
            public SqlConnection transaction { get; set; }
    
            public string GetConnection()
            {
                return connection.ConnectionString + "******** status: " + connection.State;
            }
        }
    

    Then, register the services as Singleton in the program.cs file:

    builder.Services.AddSingleton<IDataRepository, DataRepository>();
    

    After that, inject the service in the razor page .cshtml file:

        public class CreateEmployeeModel : PageModel
        {
            private readonly IDataRepository _datarepo;
            private readonly IConfiguration _config; 
            public CreateEmployeeModel(IDataRepository dataRepository, IConfiguration config)
            {
                _datarepo = dataRepository;
                _config=config; 
            }
    
            [BindProperty]
            public Employee NewEmployee { get; set; }
            public void OnGet()
            { 
                SqlConnection connection = new SqlConnection(_config.GetConnectionString("DefaultConnection2"));
                connection.Open();
                
                _datarepo.connection = connection;
    
                var state = _datarepo.GetConnection();
            }
            public IActionResult OnPostAsync()
            {
    
                var state = _datarepo.GetConnection();
     
                #region
                var newemp = NewEmployee;
    
                var showerror = true; //assume there has an error and we want to show the error.
                if (showerror)
                {
                    ViewData["showerror"]="true"; //used to set the display attribute.
                    ViewData["customerror"]="the custom error message"; //show the custom error message.
                    return Page();
                }
                #endregion
                return RedirectToPage("./Index");
            }
        }
    

    The result as below: after submitting the form, the connection status is Open in the Post method.

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful