Ramco Ramco

Ramco Ramco

  • 469
  • 2.8k
  • 393.9k

Generic Function

Jul 8 2021 4:42 PM

Hi

  Is there any Generic way that only a sibgle function be created instead of 3 separate methods to be created 

public string Add(Location objLoc)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Common.Con))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("sp_Location", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Id", objLoc.Id);
                    cmd.Parameters.AddWithValue("@Description", objLoc.Description);
                    cmd.Parameters.AddWithValue("@Action", "C");
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                ExceptionLogging.SendExcepToDB(ex);
                output = ex.Message;
                //ViewBag.ErrorMessage = ex.Message.ToString();
                //return -1;
            }
            return output;
        }

        public string Update(Location objLoc , string hfId)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Common.Con))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("sp_Location", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Id", objLoc.Id);
                    cmd.Parameters.AddWithValue("@Description", objLoc.Description);
                    cmd.Parameters.AddWithValue("@Action", "U");
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                ExceptionLogging.SendExcepToDB(ex);
                output = ex.Message;
            }
            return output;
        }

        [HttpPost]
        public string Delete(string Id)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Common.Con))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("sp_Location", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Id", Id);
                    cmd.Parameters.AddWithValue("@Action", "D");
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                ExceptionLogging.SendExcepToDB(ex);
                output = ex.Message;
            }
            return output;
        }

Thanks


Answers (2)