2

I am mainly programming in PHP, but for my school project I have to use ASP.NET(C#). Now I have this code which works in PHP:

$Data = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $date = strtotime($row["Date"])*1000;
    $Data[] = array($date, $row["Data"]);
}
  1. As you can see it's a 2D array, which is expanding, but the inner array always has two parameters. Now I have searched on the web and haven't found how to declare a expanding array in ASP.NET(C#).

  2. Is there a replacement for strtotime in aspx?

0

2 Answers 2

4

Given it's now C# and the datatypes should come across through the reader, the following should work:

// use a struct to store your retrieved information
public struct Details
{
  DateTime date;
  Object data;

  public Details(DateTime Date, Object Data)
  {
    date = Date;
    data = Data;
  }
}

// use List<T> to define a dynamically-sized list
List<Details> dataArray = new List<Details>();
while (reader.Read()) // query the database
{
  // add the information to the list as we iterate over it
  dataArray.Add(new Details(reader.GetDateTime("Date"),reader["Data"]));
}

// your new expansive array: dataArray.ToArray();
// converting an object to datetime: Convert.ToDateTime();

This also assumes you're using a SqlDataReader (though you could use almost anything to retrieve the information from SQL).


Update

Given the comments below, the information is retrieved from a database and then output to JSON. Because of the new requirement I am going to change the store format a bit. This gives the flexibility to continue using the JavaScriptSerializer while outputting to a format javascript can understand.

// make a list we can use
List<Object[]> details = new List<Object[]>();

// populate the data
using (SqlConnection connection = new SqlConnection("<connection string>"))
{
    SqlCommand command = new SqlCommand("<query string>");
    SqlReader reader = command.ExecuteReader();

    .. go through the database
    while (reader.Read())
    {
      DateTime date = reader.GetDateTime("Date");
      Double data = reader.GetDouble("Data");

      // convert the datetime to unix time
      // http://www.epochconverter.com/
      var epoc = (date.ToUniversalTime().Ticks - 621355968000000000) / 10000000;

      details.Add(new Object[]{ epoc, data });
    }
}

// Now we serialize
JavaScriptSerializer serializer = new JavaScriptSerializer();
String result = serializer.Serialize(details.ToArray());

And here's what I receive from result (for format anyways):

[
  [1297627668,0.7],
  [1297714068,1.1],
  [1297800468,0.1],
  ...
]
Sign up to request clarification or add additional context in comments.

15 Comments

So this Convert.ToDateTime is equal to upper solution DateTime.Parse?
@JernejJerin: Yes, but it's implemented in the SQL Reader. You can also use Convert.ToDateTime, though DateTime.Parse will exception out should the information be invalid/unconvertable. You can also use DateTime.TryParse if you want it to carry on through invalid input (the function will return true/false if it could or could not convert).
@Brad Christie: I implemented your idea. The upper code I have implemented in Data.aspx.cs, then I call function (ReturnArray()), which iterates through reader and returns dataArray.ToArray(). I called that function from the parameter: string arrayJson = serializer.Serialize(ReturnArray()). But it returns empty result: [{},{}], on this example: dataArray.Add(new Details(DateTime.Parse("2010-05-01 00:00:00"), 5)); dataArray.Add(new Details(DateTime.Parse("2010-05-01 00:10:00"), 6));
@JernejJerin: Ah, you're looking to serailize TO json eh? Should have made that a little clearer. ;-) Give me a little bit, and I'll update my answer (and I'll leave another comment to let you know it's been updated).
@JernejJerin: What's consuming the JSON data? Is a value of {"date":"date(156266026"} acceptable in the encoded result?
|
3

First, how are you getting the data from the SQL database? I'll assume that you're getting it as a collection of DataRow objects.

For storing the data, I would suggest using a list of tuples, as shown below.

List<Tuple<DateTime, string>> Data = new List<Tuple<DateTime, string>>();
// Somehow get a collection of rows into a results object
foreach (DataRow row in results)
{
    DateTime dt = DateTime.Parse(row["Date"]);
    Data.Add(new Tuple<DateTime, string>(dt, row["Data"]));
}

You can then access items in the list by index, and the individual fields as Item1 and Item2:

DateTime dt = Data[0].Item1;
string itemData = Data[0].Item2;

2 Comments

I agree this look very good. But can I the use JavaScriptSerializer? Because I have to serialize it to JSON?
@JernejJerin: You should be able to, but you'll probably want to define the object structure in classes and deserialize to that. Otherwise, I believe, by default you'll end up with a Dictionary of items, and (depending your structure) they may or may not be desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.