Aniket Narvankar

Aniket Narvankar

  • 536
  • 2.1k
  • 580.7k

Dapper Related Issue,return string from method

Dec 28 2021 4:53 AM

Here is the c sharp code,which returns amsNotId from sql. Insted of getting it data to datatable and then assigning value to amsNotId,how to directly return string from this method?

public string GetAnotIdByAgencyIdPropertyStateSourceCode(string agencyId, string source, string propertyState, string sourceType)
        {
            string amsNotId = string.Empty;
            DataTable dt = new DataTable();
            try
            {
                using (IDbConnection conn = objDataConnection.dataConnection)
                {
                    var param = new DynamicParameters();
                    param.Add("@agencyId",agencyId);
                    param.Add("@sourceCode",source);
                    param.Add("@propertyState",propertyState);
                    param.Add("@sourceType",sourceType);
                    StringBuilder sb = new StringBuilder("Select AMSNotationCode from ATESSourceMapping where ");
                    sb.Append("AgencyId=@agencyId and SourceCode=@sourceCode and PropertyState=@propertyState and (SourceType is null or isnull(SourceType,'') != @sourceType) ");
                    var result = conn.ExecuteReader(sb.ToString(), param, null);
                    dt.Load(result);
                    if(dt != null && dt.Rows.Count > 0)
                    {
                        amsNotId = Convert.ToString(dt.Rows[0]["AMSNotationCode"]);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return amsNotId;
        }


Answers (2)