-2

I am not sure how to deal with this error I am getting: "cannot convert from object to string"

row["timeinseconds"] = TimeSpan.FromSeconds( double.Parse( r["timeinseconds"] ) ).ToString(@"hh\:mm\:ss");

The timeinseconds is coming from a sqlite database and its stored as an integer value

I need to parse it as a double so I can use the from seconds method of timespan so I can convert it to the right format

private void updateDgvTimesheet()
{
    dgvTimesheet.Rows.Clear();

    foreach (DataRow r in dataset.Tables[0].Rows)
    {
        DataRow row = this.dataset.Tables[0].NewRow();

        row["taskname"] = r["taskname"];

        row["timeinseconds"] = TimeSpan.FromSeconds( double.Parse( r["timeinseconds"] ) ).ToString(@"hh\:mm\:ss");
        row["date"] = r["date"];
        row["paid"] = r["paid"];

        dgvTimesheet.Rows.Add(row);
    }
}
Ben
  • 2,122
  • 2
  • 28
  • 48
  • What is the type of `r["timeinseconds"]` in `double.Parse( r["timeinseconds"] )` ? – Elwin Arens Jun 06 '16 at 19:27
  • 3
    Try formatting to string first `double.Parse(r["timeinseconds"].ToString())`, or maybe `(double)((int)r["timeinseconds"])` if you are sure it's an `int`. – juharr Jun 06 '16 at 19:28
  • 1
    Yup, if it's a cell from a DataTable, you'll have to `.TosTring()` it to get its value, otherwise it's just an object. – D. Petrov Jun 06 '16 at 19:28
  • 2
    If you have an `int` and want a `double`, why go through a `string` as an intermediary? Seems like a waste of resources. – David Jun 06 '16 at 19:28
  • @juharr, correct that's the solution. – Florian Schaal Jun 06 '16 at 19:29
  • @ElwinArens The datatype is integer – Ben Jun 06 '16 at 19:29
  • 2
    Your `.ToString()` is being called on the `TimeSpan.FromSeconds` and you likely only want to call it on the `r["timeinseconds"]` – pay Jun 06 '16 at 19:29

1 Answers1

1

You have to convert r["timeinseconds"].ToString()

Change your function to be:

private void updateDgvTimesheet()
{
    dgvTimesheet.Rows.Clear();

    foreach (DataRow r in dataset.Tables[0].Rows)
    {
        DataRow row = this.dataset.Tables[0].NewRow();

        row["taskname"] = r["taskname"];

        row["timeinseconds"] = TimeSpan.FromSeconds( double.Parse( r["timeinseconds"].ToString() ) ).ToString(@"hh\:mm\:ss");
        row["date"] = r["date"];
        row["paid"] = r["paid"];

        dgvTimesheet.Rows.Add(row);
    }
}
Avitus
  • 15,640
  • 6
  • 43
  • 53