-2

I want to return multiple string from the below function. How to do that

public static string GETSTATE_SAP(string SAP)
    {
        string strState = "";
        string strSiteAdd = "";
        string strLatitude = "";
        string strLongtiude = "";
        try
        {
            OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
            Oraconn.Open();
            OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
            OracleDataAdapter da = new OracleDataAdapter(cmd);
            DataTable dtSap = new DataTable();
            da.Fill(dtSap);
            strState = Convert.ToString(dtSap.Rows[0][0]);
            strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
            strLatitude = Convert.ToString(dtSap.Rows[0][0]);
            strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
        }
        catch (Exception)
        {
        }
        return strState, strSiteAdd, strLatitude, strLongtiude;
    }
Nad
  • 4,605
  • 11
  • 71
  • 160

5 Answers5

4

There are several ways to return info, I advise you to return a class. This is because the info is related but does not have the same meaning. You use an array or list when the elements are representing the same type of data. For example a list of menu items, a list of state info's etc.

public class StateInfo
{
    public string State {get;set;}
    public string SiteAdd {get;set;}
    public string Latitude {get;set;}
    public string Longtiude {get;set;}
}


public static StateInfo GETSTATE_SAP(string SAP)
{
    var result = new StateInfo();

    try
    {
        OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
        Oraconn.Open();
        OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
        OracleDataAdapter da = new OracleDataAdapter(cmd);
        DataTable dtSap = new DataTable();
        da.Fill(dtSap);
        result.State = Convert.ToString(dtSap.Rows[0][0]);
        result.SiteAdd = Convert.ToString(dtSap.Rows[0][0]);
        result.Latitude = Convert.ToString(dtSap.Rows[0][0]);
        result.Longtiude = Convert.ToString(dtSap.Rows[0][0]);
    }
    catch (Exception)   // <--- I'll explain below..
    {
        return null;
    }

    return result;
}

This way you're not depending on the order of returning the info. It's making a lot easier to handle.

Usage:

var stateInfo = GETSTATE_SAP(sap);

if(stateInfo != null)
    Debug.WriteLine(stateInfo.Longtiude);

try/catch in your method:

I marked your try/catch as 'explain below'.Imagine you call this method to get your information, and you can't 'see' what went wrong, but the information is empty. The called cannot verify the information is right of wrong. Either return null or do not catch the exception here:

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0

You can create a custom class with those fields and return it,

 public MyClass string GETSTATE_SAP(string SAP)
    {
        MyClass myObj = new MyClass();
        try
        {
            OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
            Oraconn.Open();
            OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
            OracleDataAdapter da = new OracleDataAdapter(cmd);
            DataTable dtSap = new DataTable();
            da.Fill(dtSap);

            myObj.strState = Convert.ToString(dtSap.Rows[0][0]);
            myObj.strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
            myObj.strLatitude = Convert.ToString(dtSap.Rows[0][0]);
            myObj.strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
        }
        catch (Exception)
        {
        }
        return myObj;
    }
    public class MyClass
    {
        public string strState { get; set; }
        public string strSiteAdd { get; set; }
        public string strLatitude { get; set; }
        public string strLongtiude { get; set; }


    }
Florian K
  • 602
  • 9
  • 30
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

Your method returns a string and thus return strState, strSiteAdd, strLatitude, strLongtiude; statement is illegal.

You can consider changing your method signature to return a DataTable like below and return the DataTable you created

 public static DataTable GETSTATE_SAP(string SAP)
    {

(OR)

Change it to return a array of string

 public static IEnumerable<string> GETSTATE_SAP(string SAP)
    {
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

You can choose any one from following

  1. Create a new class say location with these members and populate this in your function and your function will return it.

public class location{ public string strState = ""; public string strSiteAdd = ""; public string strLatitude = ""; public string strLongtiude = ""; }

public static location GETSTATE_SAP(string SAP)
{
    _location = new location() ;
    try
    {

     _location.strState = Convert.ToString(dtSap.Rows[0][0]);
        _location.strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
        _location.strLatitude = Convert.ToString(dtSap.Rows[0][0]);
        _location.strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
}

return _location;
  1. another option is dictionary

    public static Dictionary<string,string> GETSTATE_SAP(string SAP) {
    
     Dictionary<string,string> _location = new dictionary<string,string>
    _location.add("strState", Convert.ToString(dtSap.Rows[0][0]));`
    
  2. third one is datatable datatable _location = new datatable(); _location.add("strState"); // add all columns _location.rows.add(Convert.ToString(dtSap.Rows[0][0]), , , ,..);

  3. Return dynamic object

public object MyDynamicObject() { dynamic obj = new ExpandoObject(); return obj; }

public static object GETSTATE_SAP(string SAP)
{
 object _location = MyDynamicObject();
try
        {

         _location.strState = Convert.ToString(dtSap.Rows[0][0]);
            _location.strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
            _location.strLatitude = Convert.ToString(dtSap.Rows[0][0]);
            _location.strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
    }

    return _location;`
Anil
  • 3,722
  • 2
  • 24
  • 49
0

You are using Public method. Personally I don't like using out parameter for public method.

But If you don't want to create new class, then you can try below method.

    public static bool GETSTATE_SAP(string SAP, out string strState, out string strSiteAdd, out string strLatitude, out string strLongtiude)
        {
            strState = string.Empty;
            strSiteAdd = string.Empty;
            strLatitude = string.Empty;
            strLongtiude = string.Empty;
            try
            {
                OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
                Oraconn.Open();
                OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
                OracleDataAdapter da = new OracleDataAdapter(cmd);
                DataTable dtSap = new DataTable();
                da.Fill(dtSap);
if(dtSap.Rows.Count > 0){
                strState = Convert.ToString(dtSap.Rows[0][0]);
                strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
                strLatitude = Convert.ToString(dtSap.Rows[0][0]);
                strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
return true;
}
            }
            catch (Exception)
            {
            }
            return false;
        }
Ashish Sapkale
  • 540
  • 2
  • 13