As Ak

As Ak

  • NA
  • 65
  • 7.6k

Access a list in a class then insert into database

Feb 3 2022 1:52 AM

Hi, Im having a problem on accessing the list in the class. Im still new to c#. So I need help to solve this issue. 

Below is the classes i've created. In class Datum, there's a list called location. 

public class Location  
    {  
        public string location { get; set; }  
        public string remarks { get; set; }  
        public string deliverydate { get; set; }  
        public string pickupdate { get; set; }  
    }  
  
    public class Datum  
    {  
        public string tracking_number { get; set; }  
        public string pickup_place { get; set; }  
        public string delivery_place { get; set; }  
        public string tel_no { get; set; }  
        public string status { get; set; }  
        public DateTime lastupdate_time { get; set; }  
        public List<Location> Location { get; set; }  
    }  
  
    public class Root  
    {  
        public List<Datum> data { get; set; }  
    } 

 

I want to use the List<location> to insert the values into database. But below code gives an error :

'type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?). at line
 

foreach ( var p in data.Root)
{
  string tracking_number = p.tracking_number;
  string pickup_place = p.pickup_place; 
  string delivery_place = p.delivery_place;
  string tel_no = p.tel_no ; 
  string status = p.status ;
  datetime lastupdate_time = p.lastupdate_time ; 
  
  // start from this line, gives CS1061 error
  string location = p.Location.location ;
  string remarks = p.Location.remarks ; 
  string deliverydate  = p.Location.deliverydate;
  string pickupdate = p.Location.pickupdate; 
  
  query = "INSERT INTO Tracking (tracking_number, pickup_place, delivery_place, tel_no, status, 
  lastupdate_time, location, remarks, deliverydate , pickupdate) VALUES (tracking_number, 
  pickup_place, delivery_place, tel_no, status, lastupdate_time, location, remarks, deliverydate , 
  pickupdate); 


}

 

How can I solve this problem? I've tried to look for solutions, but nothing works. 

 

 


Answers (6)