Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

VS C# Creating a library of functions

I am trying to improve my code by putting functions inside libraries. This way I do not need to keep repeating the same code over and over.  Listed below are two functions: GetConnection and DisplayARSummary. The first returns a connection string and the second return a Boolean. Something changes when I drop them into a library. private SqlConnection GetConnection is no longer correct and private void DisplayARSummary had to be changed to public static void DisplayARSummary. What is the concept behind this and what is the correct way to now call these functions now that they have been put into a library?

namespace PowmatGPCustomizations.Library
{


    public class Controller
    {
        Int32 resp;
        string GP_User_ID;

        /* Set up SQL Connection Handler */
        public System.Data.SqlClient.SqlConnection DataConnection;

        /* Define the GP connection object */
        GPConnection GPConnObj;

        private SqlConnection GetConnection()
        {
            SqlConnection DataConnection = new SqlConnection();

            /* Call Startup */
            resp = GPConnection.Startup();

            /* Create the connection object */
            GPConnObj = new GPConnection();

            /* Initialize the connection object */
            GPConnObj.Init("MyCompany", "MyKey");

            /* Make the connection */
            DataConnection.ConnectionString = "Database=" + Dynamics.Globals.IntercompanyId.Value;
            GPConnObj.Connect(DataConnection, Dynamics.Globals.SqlDataSourceName.Value, Dynamics.Globals.UserId.Value, Dynamics.Globals.SqlPassword.Value);

            GP_User_ID = Dynamics.Globals.UserId.Value;

            /* Check the return code */
            if ((GPConnObj.ReturnCode & (int)GPConnection.ReturnCodeFlags.SuccessfulLogin) !=
                (int)GPConnection.ReturnCodeFlags.SuccessfulLogin)
            {
                MessageBox.Show("Login failed");
            }
        }

        public static void DisplayARSummary(string CustomerNumber)
        {
            string CUSTNMBR;
            CUSTNMBR = CustomerNumber;

            try
            {
                SqlCommand cmd = new SqlCommand("rbs_PerformARCreditCheck", DataConnection);

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@I_CUSTNMBR", CUSTNMBR));

                cmd.Parameters.Add(new SqlParameter("@O_CURRBLNC", SqlDbType.Float)).Direction = ParameterDirection.Output;
                cmd.Parameters.Add(new SqlParameter("@O_CRLMTTYP", SqlDbType.Int)).Direction = ParameterDirection.Output;
                cmd.Parameters.Add(new SqlParameter("@O_CRLMTAMT", SqlDbType.Float)).Direction = ParameterDirection.Output;

                cmd.Connection = DataConnection;

                cmd.ExecuteNonQuery();

                CURRBLNC = Convert.ToDecimal(cmd.Parameters["@O_CURRBLNC"].Value);
                CRLMTTYP = Convert.ToInt32(cmd.Parameters["@O_CRLMTTYP"].Value);
                CRLMTAMT = Convert.ToDecimal(cmd.Parameters["@O_CRLMTAMT"].Value);

                /* MessageBox.Show("Credit Limit Type: " + CRLMTTYP.ToString()); */

                if (CURRBLNC < CRLMTAMT)
                {
                    this.Hide();
                    this.Dispose();
                    resp = GPConnection.Shutdown();
                    DataConnection.Close();
                }
            }
            catch (Exception ex)
            {
                string eMsg = "003: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                if (stackTraceWanted) MessageBox.Show(eMsg);
            }

            txtCurrentBalance.Text = CURRBLNC.ToString("c");

            txtCreditLimitAmount.Text = CRLMTAMT.ToString("c");

            switch (CRLMTTYP)
            {
                case 0:
                    txtCreditLimitType.Text = "No Credit";
                    break;
                case 1:
                    txtCreditLimitType.Text = "Unlimited";
                    CRLMTAMT = 9999999.99M;
                    break;
                case 2:
                    txtCreditLimitType.Text = "Amount";
                    break;
                default:
                    txtCreditLimitType.Text = "Unknown";
                    break;
            }
        }

    }

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwheeler23

ASKER

mY APLOGIES I THOUGHT I HAD CLOSED THIS LAST WEEK. tHANKS.