1

Hi i'm trying to make a reservation page. If someone makes a reservation that date gets saved in the database and will also be showed on their page. The type of the column 'dayid' is date in postgresql. In razor pages C# i used the type DateTime for variable Dayid. I need to convert the dayid value from database to a string. But i don't know how to solve this error: "No overload for method 'ToString' takes 1 arguments" Here is the code

public List<ReservationModel> ShowReservation()
        {
            var cs = Database.Database.Connector();
            List<ReservationModel> res = new List<ReservationModel>();
            using var con = new NpgsqlConnection(cs);
            {
                string query = "Select dayid, locationid FROM reservation";
                using NpgsqlCommand cmd = new NpgsqlCommand(query, con);
                {
                    cmd.Connection = con;
                    con.Open();
                    using (NpgsqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            res.Add(new ReservationModel { Dayid = dr["dayid"].ToString("MM/dd/yyyy"), Locationid = dr["locationid"].ToString() });
                        }
                    }
                    
                    con.Close();
                }
            }


            return res;
        }
4
  • 1
    What type of dr["david"] is? Dec 1, 2020 at 9:27
  • 1
    Try assigning dr["dayid"] to a variable and double check that it's a datetime. It's likely object. I'd cast it to DateTime before calling ToString to be able to use the formatting parameters. Also consider a possibility where you get DateTime from your model and avoid casting. Dec 1, 2020 at 9:27
  • 1
    I suspect the problem is down to dr["dayid"] boxing the value, so you're actually accessing it as object. Dec 1, 2020 at 9:27
  • Alright i tried to do DateTime Dayid = dr["dayid"] above the while loop but it tells me cant convert object to System.DateTime
    – anonD
    Dec 1, 2020 at 9:57

1 Answer 1

5

The compile-time type of the NpgsqlDataReader indexer is just object, and the object.ToString() method is parameterless. You need an expression of type DateTime to call the ToString overload that you want.

You could cast to DateTime instead:

while (dr.Read())
{
    res.Add(new ReservationModel
    {
        Dayid = ((DateTime) dr["dayid"]).ToString("MM/dd/yyyy"),
        Locationid = dr["locationid"].ToString() 
    });
}

(Or find the column index and call dr.GetDateTime(...).)

However, I'd encourage you to change your model (ReservationModel) to keep the value as a DateTime instead of converting it to a string at this point anyway. In general, it's a good idea to keep data in its most natural data type for as much of the time as possible, only converting it to/from text at boundaries.

4
  • Hi i tried your code but then i got this error: Cannot implicitly convert type 'string' to 'System.DateTime'
    – anonD
    Dec 1, 2020 at 9:52
  • @anonD: Well I was assuming that your Dayid property is of type string, given your previous code. Is that not the case? Perhaps it's already a DateTime? In which case, why were you calling ToString at all?
    – Jon Skeet
    Dec 1, 2020 at 9:57
  • Yes the variable DayId in the reservationmodel was of type DateTime. because in the database that column is of type date. I want this date to show on the page thats why i want it to convert into a string.
    – anonD
    Dec 1, 2020 at 9:59
  • 1
    @anonD: So you need to do the conversion to a string later. You can't assign a string to a DateTime property, and it's better not to anyway. In your ShowReservation() method you should just cast to DateTime, e.g. Dayid = (DateTime) dr["dayid"] and then perform appropriate text conversions in your view.
    – Jon Skeet
    Dec 1, 2020 at 10:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.