Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Using myReader to Reference a Class That References a List From Another Class

Hi,
I'm stuck on how to reference a List value from one class that is referenced in another class while reading data with "myReader". Though I'm relatively new to C#, I can usually figure things out, but this stumps me. The first snippet is the "Physician" class and in it has an item called, "public List<PhysicianTraining> Trainings { get; set; }", which is defined in the second snippet, the "PhysicianTraining" class. The third snippet has the main class that runs a query that has all the necessary fields and I employ the myReader to populate the new object list I have setup:
                var physicianList = new List<Physician>();
                myReader = SqlCommand.ExecuteReader();
                while (myReader.Read())
                {
                    obj = new Physician();
                    obj.ID = myReader["Physician_Info_ID"].ToString();
                    obj.FirstName = myReader["First_Name"].ToString();
                    obj.LastName = myReader["Last_Name"].ToString();
                    obj.FullName = obj.Name;
                    //obj.Trainings = myReader["Product_Name"];                    
                    physicianList.Add(obj);
                }

So, the question is how to I take the myReader["Product_Name"] and assign it to obj.Trainings ? The errors I get with a few different tries at it refer to casting issues, and/or object not instantiated.

Cheers,
Ty
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Medtronic.Mobile.EndoPhysician.BusinessObjects
{
    public class Physician
    {
        public string ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Email { get; set; }
		public bool EFormCompleted { get; set; }
        public List<PhysicianTraining> Trainings { get; set; }
		
		public string Name { get { return FirstName + " " + LastName; } }
        public string FullName { get; set; }
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Medtronic.Mobile.EndoPhysician.BusinessObjects
{
    public class PhysicianTraining
    {
        public DateTime Date { get; set; }
        public Product Product { get; set; }
        public Physician Physician { get; set; }
        public string SignatureImageBytes { get; set; } 
        public Account AffiliatedAccount { get; set; }
        public User CCE { get; set; }
        public bool Fellow { get; set; }
        public string FellowGradYear { get; set; }
        public bool VS { get; set; }
        public bool IR { get; set; }
        public bool CT { get; set; }
        public bool IC { get; set; }

    }
}

Open in new window

public List<Physician> GetPhysicianTrainingList(string searchCriteria)
        {

            //Make sure something was passed
            if (String.IsNullOrEmpty(searchCriteria))
            {
                searchCriteria = "0";
            }

            var physicianList = new List<Physician>();
            var obj = new Physician();

            //Open the connection
            try
            {
                SqlServer.Open();
            }
            catch (Exception e)
            {
                ErrorMessageOpening = e.ToString();
            }


            //Run the sql query
            try
            {
                SqlDataReader myReader = null;

                string sqlStatement = "SELECT " +
                    " Physician_Lookup.Physician_Info_ID, Physician_Lookup.First_Name, Physician_Lookup.Last_Name, Physician_Lookup.Email_Address,  " +
                    " Physician_Training_Lookup.Date_Trained, Physician_Training_Lookup.Product_Name, CCE_Lookup.Employee_number AS CCE_Employe_Number, " +
                    " CCE_Lookup.First_Name AS CCE_FirstName, CCE_Lookup.Last_Name AS CCE_LastName " +
                    " FROM  " +
                    " CCE_Lookup LEFT OUTER JOIN Physician_Training_Lookup ON CCE_Lookup.Clinical_Reps_id = Physician_Training_Lookup.Clinical_Reps_ID RIGHT OUTER JOIN" +
                    " Physician_Lookup ON Physician_Training_Lookup.Physician_Info_ID = Physician_Lookup.Physician_Info_ID " +
                    " WHERE Physician_Lookup.Physician_Info_ID = " + @searchCriteria + 
                    " ORDER BY Physician_Training_Lookup.Product_Name ";

                SqlCommand SqlCommand = new SqlCommand(sqlStatement, SqlServer);
                SqlCommand.Parameters.Add(new SqlParameter("@searchCriteria", searchCriteria));

                myReader = SqlCommand.ExecuteReader();
                while (myReader.Read())
                {
                    obj = new Physician();
                    obj.ID = myReader["Physician_Info_ID"].ToString();
                    obj.FirstName = myReader["First_Name"].ToString();
                    obj.LastName = myReader["Last_Name"].ToString();
                    obj.FullName = obj.Name;
                    //obj.Trainings = myReader["Product_Name"];
                    physicianList.Add(obj);
                }
            }
            catch (Exception e)
            {
                ErrorMessageSQLSelect = e.ToString();
            }



            //Close the connection
            try
            {
                SqlServer.Close();
            }
            catch (Exception e)
            {
                ErrorMessageClosing = e.ToString();
            }


            return physicianList;
        }

Open in new window

0
MedtronicVascSR
Asked:
MedtronicVascSR
  • 3
1 Solution
 
DhaestCommented:
It seems to be a list of PhysicianTraining, so you need to do something like this:
It seems also that Product is another class, so you need to add this also

while (myReader.Read())
                {
                    obj = new Physician();
                    obj.ID = myReader["Physician_Info_ID"].ToString();
                    obj.FirstName = myReader["First_Name"].ToString();
                    obj.LastName = myReader["Last_Name"].ToString();
                    obj.FullName = obj.Name;

                    obj.Trainings = new List<PhysicianTraining>();
                      PhysicianTraining training = new PhysicianTraining();
                    training.Product = new Product(Name = myReader["Product_Name"]);
                    obj.Trainings.add(training);

                    physicianList.Add(obj);
                }
0
 
MedtronicVascSRAuthor Commented:
Hi,
Thanks for the reply. Your suggestion certainly pushes it in the right direction. However, the "New Product" line needs some tweaking (see attached file). I tried a few different things, but with no success. You are correct that the "Product" has its own class (see fourth snippet).

Cheers,
Ty Error Message
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Medtronic.Mobile.EndoPhysician.BusinessObjects
{
    public class Product
    {
        public string Name { get; set; }
    }
}

Open in new window

0
 
MedtronicVascSRAuthor Commented:
Hi,
I got figured out.
                    obj.Trainings = new List<PhysicianTraining>();
                    PhysicianTraining Training = new PhysicianTraining();
                   Training.Product = new Product();
                    Product Prod = new Product();
                    Training.Product.Name = myReader["Product_Name"].ToString();
                   obj.Trainings.Add(Training);
0
 
MedtronicVascSRAuthor Commented:
Thanks for your help.

Cheers,
Ty
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now