Click here to Skip to main content
15,885,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am retrieving multiple records from database and storing it in list. I have created properties class and then i assign data being retrieved to variables in properties and then trying to return all collection of values to client who made function call but it doesn't work. Why ? please make it correct.

Code:

C#
public Properties FetchCoordinates(String FetchParam) 
        {
            String[] parts = FetchParam.Split(',');
            sqlCom.CommandText = "FetchCoordinates";
            sqlCom.CommandType = CommandType.StoredProcedure;
    
            sqlCom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value = parts[0].ToString();
            sqlCom.Parameters.Add("@DateTimeFrom", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[1].ToString());
            sqlCom.Parameters.Add("@DateTimeTo", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[2].ToString());
            SqlParameter sqlParam = new SqlParameter("@result", SqlDbType.Int);
            sqlCom.Parameters.Add(sqlParam);
            sqlCom.Parameters["@result"].Direction = ParameterDirection.Output;
            List<Properties> props = new List<Properties>();
            Properties p;
     
            try
            {
                sqlCon.Open();
                using (SqlDataReader reader = sqlCom.ExecuteReader())
    {
                while (reader.Read())
                {
                    p = new Properties()
                    {
                        Longitude = reader["Longitude"].ToString(),
                        Latitude  = reader["Latitude"].ToString()
                    };
                    props.Add(p);
                    return p;
                }
                
    }
               
    
    
            }
    
    
    
            catch (Exception ex)
            {
    
                
    
            }
    
            finally
            {
                sqlCon.Close();
            }
            
        }
    
    }


Properties.cs

C#
public class Properties
    {
        public Properties()
    	{
    	}
    
        public string Longitude { get; set; }
        public string Latitude { get; set; }
    
    }
Posted


  • The code in q. returns a single property value not a list/array/collection
  • The method returns after the first row in the data has been read


I've stripped the setup code and exception handling to make the changes easier to see.

C#
// If we want a list of all values then make sure the method returns a list.
public List<Properties> FetchCoordinates(String FetchParam) 
{
   // Chopped for clarity

   List<properties> props = new List<properties>();
   Properties p;
     
   sqlCon.Open();
   using (SqlDataReader reader = sqlCom.ExecuteReader())
   {
     while (reader.Read())
     {
       p = new Properties()
       { Longitude = reader["Longitude"].ToString(),
         Latitude  = reader["Latitude"].ToString()};
        props.Add(p);
       // Don't think you want to do this.
       // return p;
     }
    }
   sqlCon.Close();
  
   // Return _everything_ we read from the DB.
   return props;
   
 }


Were it me I'd also rename the Properties class "Position" or possibly Coordinate as that seems like a better description given its properties.
 
Share this answer
 
v3
Two things I can see right away.

You need to return a collection of Properties not the Properties object.

public List<properties> FetchCoordinates(String FetchParam) </properties>


Then you need to remove the return in your while loop. Move your return to after the loop.

C#
return props;
 
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