Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to reference a variable declared in Main() method in SSIS script task in a different method.

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {      
        public void Main()
        {
            try
            {
                var user = Dts.Variables["User::usr"].Value.ToString(); 
		var code = Dts.Variables["User::pwd"].Value.ToString();              
            }

            catch (Exception e)
            {
                // catch exception
            }
        }

        private class getItem
        {
            public string url = "http://localhost:1234/api/item";
			
			using (var client = new System.Net.Http.HttpClient())
            {
                // HTTP POST
                client.BaseAddress = new Uri("baseUrl");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var response =  client.GetAsync("/item/GetAllItems").Result;
                string res = "";
                using (HttpContent content = response.Content)
                {
                    // Read string.
                    Task<string> result =  content.ReadAsStringAsync();
                    res = result.Result;
                }
            }	

            private httpRequest cr(string uri, string webMethod = "GET", byte[] data = null, string contentType = "application/json", bool isAuth = false)
            {
                var reqid = httpRequest.Create(uri);

                if (isAuth)
                {
                    var hruser = user; // trying to pass user from Main() method
                    var hrcode = code; // trying to pass code from Main() method
                    var authString = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", user, code)));
                    reqid.Headers.Add("Authorization", "Basic " + authString);
                }

                return reqid;
            }
		}
    }


What I have tried:

I tried using a constructor but still run to error "object reference not set to an instance of an object.".

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {  
     string user;
     string code;
   
        public void Main()
        {
            try
            {
                user = Dts.Variables["User::usr"].Value.ToString(); 
		code = Dts.Variables["User::pwd"].Value.ToString();              
            }

            catch (Exception e)
            {
                // catch exception
            }
        }

        private class getItem
        {
            public string url = "http://localhost:1234/api/item";

            string User;
            string Code;

            public getItem(string user, string code)
            {
               User = user;
               Code = code;
            }
			
			using (var client = new System.Net.Http.HttpClient())
            {
                // HTTP POST
                client.BaseAddress = new Uri("baseUrl");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var response =  client.GetAsync("/item/GetAllItems").Result;
                string res = "";
                using (HttpContent content = response.Content)
                {
                    // Read string.
                    Task<string> result =  content.ReadAsStringAsync();
                    res = result.Result;
                }
            }	

            private httpRequest cr(string uri, string webMethod = "GET", byte[] data = null, string contentType = "application/json", bool isAuth = false)
            {
                var reqid = httpRequest.Create(uri);

                if (isAuth)
                {
                    var hruser = User; // trying to pass user from Main() method
                    var hrcode = Code; // trying to pass code from Main() method
                    var authString = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", user, code)));
                    reqid.Headers.Add("Authorization", "Basic " + authString);
                }

                return reqid;
            }
		}

      public string getItemDesc()
        {
            getItem gi = new getItem(user, code); //pass user, code?
            string uri = gi.BaseUrl;

            HttpStatusCode statuscode;

            string idesc = gi.httpRequest(uri, out statuscode, isSecured: true);
            if (statuscode != HttpStatusCode.OK) throw new ApplicationException("error");
            return idesc;
        }
    }
Posted
Updated 1-Jul-19 12:25pm
v2
Comments
[no name] 30-Jun-19 14:32pm    
Do you know the difference between PRIVATE and PUBLIC fields / properties?

1 solution

Hi Gerry

Thanks for your response. Private or Public access specifier is based on how it could be accessed in the program.

I apologize if my answer is vague as I am still trying to get my feet wet understanding C# program nuances.

Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900