Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am very new to C#. I am writing a program using visual studio c# where it will first ask the user to enter an employee name. Next, it will pass that name through an API and will retrieve and display the employee signature. I have completed this portion.

Next, the program will ask the user to enter a designated "to" and "from" date. Next, the program should pass the date information as well as the signature obtained previously through a second API and retrieve and display information on to a grid data table accordingly.

For the Grid view data table, I understand that I should be connected to a SQL data server, which I am.

My problem is that

1) I am not sure how to write a code which will pass three parameters to an API (the "to" and "from" date, and the employee signature). I have tried the code below, however, I receive an error when I try to link the corresponding button to the JSON code to retrieve data. The error states that "There is no argument given that corresponds to the required formal parameter 'toDate' of 'WebAPI.GetTime(double, double, string).'

2) I am not sure how to pass the signature previously obtained from a different API through the new API.

Any help would be much appreciated.

What I have tried:

Code for defining the variables:

namespace TimeSheet_Try11_Models
{
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class OracleHour
    {
        public string orderNumber { get; set; }
        public DateTime dateOfWork { get; set; }
        public string description { get; set; }
        public string surveyor { get; set; }
        public string hourType { get; set; }
        public double hours { get; set; }
        public int status { get; set; }
        public string savedInOlsonTimezone { get; set; }
        public double invoicelinevalue { get; set; }
        public string articleType { get; set; }
        public DateTime dateOfWorkInSavedTimezone { get; set; }
    }

    public class MyArray
    {
        public string orderNumber { get; set; }
        public string projectnumber { get; set; }
        public string noteToInvoicer { get; set; }
        public List<object> oracleCosts { get; set; }
        public List<OracleHour> oracleHours { get; set; }
    }

    public class Root1
    {
        public List<MyArray> MyArray { get; set; }
    }



}


Code calling out the JSON

public string[] GetTime(double fromDate, double toDate, string username)
      {


          ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

          var cookies = FullWebBrowserCookie.GetCookieInternal(new Uri(StaticStrings.UrlNcert), false);
          WebClient wc = new WebClient();
          wc.Encoding = System.Text.Encoding.UTF8;
          wc.Headers.Add("Cookie:" + cookies);
          wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
          wc.UseDefaultCredentials = true;
          string url = "";

          url = $"{StaticStrings.UrlNcert}?user={username}&fromDate={fromDate:yyyy-MM-dd}&toDate={toDate:yyyy-MM-dd}";
          var respons = wc.DownloadString(url);
          OracleHour ndata = JsonConvert.DeserializeObject<OracleHour>(respons);
          var Get_Odnum = ndata.orderNumber;
          var Dt_Work = ndata.dateOfWork;
          var hrType = ndata.hourType;
          var hr = ndata.hours;
          var des = ndata.description;
          var surname = ndata.surveyor;

          string[] myncertdata = { Get_Odnum, Dt_Work.ToString(), hrType, hr.ToString(), des, surname };

          return myncertdata;


      }


Partial code attempting to connect the corresponding button to retrieve data (the error appears at the very last line):

namespace TimeSheets_Try_11
{
    public partial class Form3 : Form
    {
        WebAPI WA = new WebAPI();
        public Form3()
        {
            InitializeComponent();
            webBrowser2.Url = new Uri(StaticStrings.UrlNcert);
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'timesDataSet.NCert_Data' table. You can move, or remove it, as needed.
            this.nCert_DataTableAdapter.Fill(this.timesDataSet.NCert_Data);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GtData_Click(object sender, EventArgs e)
        {
            var connetionString = ConfigurationManager.ConnectionStrings["Times"].ConnectionString;
            try
            {
                using (SqlConnection conn = new SqlConnection(connetionString))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        conn.Open();
                        using (SqlCommand Sqlcmd = new SqlCommand("NCert_Data", conn))
                        {
                            Sqlcmd.CommandType = CommandType.StoredProcedure;

                            int counter; string projectnumber; double hrs; string respname; string describe; string[] prjstat; DateTime dates;

                            for (counter = 0; counter < (dataGridView1.RowCount) - 1; counter++)
                            {
                                hrs = 0;
                                projectnumber = dataGridView1.Rows[counter].Cells[1].Value.ToString();

                                prjstat = WA.GetTime(projectnumber);


                            }
                        }
                    }
                }
            }
        }
    }
}
Posted
Comments
RickZeeland 20-Oct-20 13:33pm    
That's hard to say, there are lots of API's each expecting parameters in their own format. So look for information on the website or ask more information from the API manufacturer.

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