Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to use services?

$
0
0

I'm to MVC core, I have a controller that capturers user name.

 // If they don't exist, create them
                if (person == null)
                {
                    person.CreatedByName = person.FirstName + " " + person.LastName;
                    person.FirstName = cac.FirstName;
                    person.LastName = cac.LastName;
                    person.EmailAddress = cac.Email;
                    person.Edi = cac.Edi;
                    person.ThemeId = Theme.Default;
                    _repository.Insert(person);
                    _repository.Save();
                }

I have a service that inserts data into a log table ex. table xyz.  How do I use the service...MyInsertService.cs, the method is called Insert, to save to my database:

public int InsertPersonActivityLog(int logId, DateTime activityDateTime, string activityTypeDescriptor, string additionalDetails, string edi, string fname, string mi, string lname, string notes, string reasonMessage, string titleOrRank)
        {
            StringBuilder errorMessages = new StringBuilder();
            try
            {
                // Get the connection string from the appsettings.json.
                conn.ConnectionString = myConn.DefaultConnectionString(configuration);  //@"Server=BRENTHUMBERDEVE;Database=NextGeneration;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                                                                                        // Open the connection.
                conn.Open();
                cmd.Connection = conn;

                // Set the command type of the command object.
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                // Assign values as "parameters to avoid 'SQL Injections.'"
                cmd.Parameters.AddWithValue("@date", activityDateTime);
                cmd.Parameters.AddWithValue("@descriptor", activityTypeDescriptor);
                cmd.Parameters.AddWithValue("@details", additionalDetails);
                cmd.Parameters.AddWithValue("@edi", edi);
                cmd.Parameters.AddWithValue("@lName", lname);
                cmd.Parameters.AddWithValue("@fname", fname);
                cmd.Parameters.AddWithValue("@mi", mi);
                cmd.Parameters.AddWithValue("@notes", notes);
                cmd.Parameters.AddWithValue("@reason", reasonMessage);
                cmd.Parameters.AddWithValue("@titleOrRank", titleOrRank);

                return cmd.ExecuteNonQuery();
            }
            catch(SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +"Message: " + ex.Errors[i].Message + "\n" +"LineNumber: " + ex.Errors[i].LineNumber + "\n" +"Source: " + ex.Errors[i].Source + "\n" +"Procedure: " + ex.Errors[i].Procedure + "\n");
                }
                Console.WriteLine(errorMessages.ToString());
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return 0;
        }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>