locked
Looping into Class Property array RRS feed

  • Question

  • User1180806401 posted

    Hi Guys,

    I'm creating a REST webservice that return a query result set and for that I've created class property with public properties, and then ran a query in my websevice, store the result in an array with the properties and return a JSON respose, but the problem is that I'm unable to define a While loop inside the class property array with and error (Invalid Expression term 'while'), and help pls?

    public class Employees

    {

    public Employees()

    {

    }

        public string EmpNumber { get; set; }

        public string Surname { get; set; }

        public string Firstname { get; set; }

        

    }

            Employees[] Emps = new Employees[]{
               while (reader.Read())    {

              new Employees()

              {

               EmpNumber =  reader[1].ToString(),

               Surname =  reader[2].ToString(),

              Firstname =  reader[3].ToString()

            }

            }

     return new JavaScriptSerializer().Serialize(Emps);

    thanks

    Tuesday, November 21, 2017 3:37 AM

Answers

  • User-707554951 posted

    Hi rbadis

    For your problem, I suggest you could use List instead of array.

    using System.Configuration;
    using System.Data.SqlClient;
    using System.Web.Script.Serialization;
     protected void Page_Load(object sender, EventArgs e)
            {
                List<Employees> emp = new List<Employees>();
    
                string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT top 3 * FROM Cities"))
                    {
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                                     
                                    emp.Add(new Employees { EmpNumber = reader[0].ToString(),
                                     Surname= reader[1].ToString(),
                                    Firstname = reader[2].ToString()});                        
                            }
                        }
    
                    }
                    con.Close();
                }
                var jsonSerialiser = new JavaScriptSerializer();
                var ss = jsonSerialiser.Serialize(emp);
                Response.Write(ss);
            }
    
            public class Employees
    
            {
    
                public Employees()
    
                {
    
                }
                public string EmpNumber { get; set; }
    
                public string Surname { get; set; }
    
                public string Firstname { get; set; }
            }

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 22, 2017 6:44 AM

All replies

  • User1180806401 posted

    I also tried the below but it tells me "Object reference not set to an instance of an object" when it comes to  Emps[index].EmpNumber = reader[1].ToString(); line

           int index = 0;

       int count = 20;

        Employees[] Emps = new Employees[count];

            while (reader.Read())

             {

                 Emps[index].EmpNumber = reader[1].ToString();

                 Emps[index].Surname = reader[2].ToString();

                 Emps[index].Firstname = reader[3].ToString();

                    index=index+1;

            

    Tuesday, November 21, 2017 4:00 AM
  • User-707554951 posted

    Hi rbadis

    For your problem, I suggest you could use List instead of array.

    using System.Configuration;
    using System.Data.SqlClient;
    using System.Web.Script.Serialization;
     protected void Page_Load(object sender, EventArgs e)
            {
                List<Employees> emp = new List<Employees>();
    
                string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT top 3 * FROM Cities"))
                    {
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                                     
                                    emp.Add(new Employees { EmpNumber = reader[0].ToString(),
                                     Surname= reader[1].ToString(),
                                    Firstname = reader[2].ToString()});                        
                            }
                        }
    
                    }
                    con.Close();
                }
                var jsonSerialiser = new JavaScriptSerializer();
                var ss = jsonSerialiser.Serialize(emp);
                Response.Write(ss);
            }
    
            public class Employees
    
            {
    
                public Employees()
    
                {
    
                }
                public string EmpNumber { get; set; }
    
                public string Surname { get; set; }
    
                public string Firstname { get; set; }
            }

    Output:

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 22, 2017 6:44 AM
  • User1180806401 posted

    Thanks Cathy, I actually did this and I'm guessing that would work also.

    Thanks you very much for your help.

    [WebMethod]

        public DataTable GetEmployees()

        {

         CacheConnection cnCache1 = cvCacheConnection.OpenCvCacheConnection();

         string SQLText = " Select top 10 EmployeeNumber, GivenNames from cvEmployee.Employee  ";

         CacheCommand command = new CacheCommand(SQLText, cnCache1);

         CacheDataReader reader = command.ExecuteReader();

         DataTable dt = new DataTable();

         dt.Load(reader);

         command.Dispose();

         reader.Close();

         reader.Dispose(true);

              return dt;

        }

    Monday, November 27, 2017 3:52 AM