So I have the following code :
listOfUserLogs.Add(new Log { TimeStamp = Convert.ToDateTime(myReader["TimeStamp"]), CheckpointId = Convert.ToInt32(myReader["CheckpointId"]) });
And when I run the program I get the System.IndexOutOfRangeException {"TimeStamp"}
. I don't understand why is this so and how to fix it .
Note: I edited the post so you can see the entire code and let me know what am I missing.
You can see my program here :
namespace Distance
{
class Program
{
static void Main(string[] args)
{
string connectionString = GetConnectionString();
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT User.Id , [Log].[TimeStamp] ,[Checkpoints].[Id] ,[Checkpoints].[Coordinates] FROM dbo.[Users] INNER JOIN dbo.[Log] ON [Log].[UserId] =[Users].[Id] INNER JOIN dbo.[Checkpoints] ON [Checkpoints].[Id] = [Log].[CheckpointId] ", sourceConnection);
//SqlCommand myCommand = new SqlCommand("SELECT User.Id ,User.Name ,Checkpoint.Id ,Checkpoint.Coordinates , Log.TimeStamp FROM dbo.Users, INNER JOIN dbo.Log ON Log.UserId = User.Id, INNER JOIN dbo.Checkpoints ON Checkpoint.Id = Log.CheckpointId ;", sourceConnection);
myReader = myCommand.ExecuteReader();
var listOfUsers = new List<User>(); //get users from db
//long countStart = System.Convert.ToInt32(myCommand.ExecuteScalar());
var listOfCheckpoints = new List<Checkpoint>(); //get checkpoints from db
var listOfUserLogs = new List<Log>();
while (myReader.Read())
{
listOfUsers.Add(new User
{
Id = Convert.ToInt32(myReader["Id"]),
Name = myReader["Name"].ToString(),
Coordinates = myReader["Coordinates"].ToString()
});
listOfCheckpoints.Add(new Checkpoint
{
Id = Convert.ToInt32(myReader["Id"]),
Coordinates = myReader["Coordinates"].ToString()
});
listOfUserLogs.Add(new Log
{
TimeStamp = Convert.ToDateTime(myReader["TimeStamp"]),
CheckpointId = Convert.ToInt32(myReader["CheckpointId"]),
UserId =Convert.ToInt32(myReader["UserId"])
});
}
StringBuilder sb = new StringBuilder();
foreach (var user in listOfUsers)
{
string address = user.Coordinates;
DateTime currentDate = new DateTime(2014, 8, 1);
var dictionary = new Dictionary<string, double>();
while (currentDate <= DateTime.Now)
{
double dayUserDistance = 0.00;
// var listOfUserLogs = new List<Log>(); //Get logs where day == currentDate from db
var previousCoordinate = address;
foreach (var log in listOfUserLogs)
{
Checkpoint checkpoint = listOfCheckpoints.FirstOrDefault(x => x.Id == log.CheckpointId);
dayUserDistance += DistanceCalculator.GetDistance(previousCoordinate, checkpoint.Coordinates);
previousCoordinate = checkpoint.Coordinates;
}
dayUserDistance += DistanceCalculator.GetDistance(previousCoordinate, address);
dictionary.Add(currentDate.ToString("yyyy-MM-dd"), dayUserDistance);
currentDate = currentDate.AddDays(1);
}
sb.Append(user.Name + ";");
foreach (KeyValuePair<string, double> keyValuePair in dictionary)
{
sb.Append(keyValuePair.Value + ";");
}
sb.AppendLine();
}
Console.WriteLine();
Console.ReadLine();
}
}
private static string GetConnectionString()
// To avoid storing the sourceConnection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=BESA-PC;" +
" Integrated Security = true;" +
"Initial Catalog=CykelScore2;";
}
}
}
internal class DistanceCalculator
{
public static double GetDistance(string previousCoordinate, string coordinates)
{
string[] PairSequence = previousCoordinate.Split(',');
float sLatitude = float.Parse(PairSequence[0]);
float sLongitude = float.Parse(PairSequence[1]);
string[] PairSequence2 = coordinates.Split(',');
float eLatitude = float.Parse(PairSequence2[0]);
float eLongitude = float.Parse(PairSequence2[1]);
var sCoord = new GeoCoordinate(sLatitude, sLongitude);
var eCoord = new GeoCoordinate(eLatitude, eLongitude);
return sCoord.GetDistanceTo(eCoord);
}
}
internal class Checkpoint
{
public int Id { get; set; }
public string Coordinates { get; set; }
}
internal class Log
{
public DateTime TimeStamp { get; set; }
public int CheckpointId { get; set; }
public int UserId { get; set; }
}
internal class User
{
public int Id { get; set; }
public string Coordinates { get; set; }
public string Name { get; set; }
}