Click here to Skip to main content
15,901,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i pass wrong date format e.g. omitting minutes part then it throws error:

System.FormatException: String was not recognized as a valid DateTime.

how to handle it in code so clients gets decent message ?

C#
public List<Coordinates> FetchCoordinates(String FetchParam) 
    {
        List<Coordinates> Coords = new List<Coordinates>();
        Coordinates c;
 
        if(String.IsNullOrEmpty(FetchParam))
        {
            c = new Coordinates()
                        {
                            Error = "No Input Provided"
                        };
                        Coords.Add(c);
                        return Coords;
        }

        String[] parts = FetchParam.Split(',');
        sqlCom.CommandText = "FetchCoordinates";
        sqlCom.CommandType = CommandType.StoredProcedure;

        String IMEI = parts[0].ToString();
        DateTime DateTimeFrom = Convert.ToDateTime(parts[1]);
        DateTime DateTimeTo = Convert.ToDateTime(parts[2]);

        sqlCom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value = IMEI;
        sqlCom.Parameters.Add("@DateTimeFrom", SqlDbType.VarChar).Value = DateTimeFrom;
        sqlCom.Parameters.Add("@DateTimeTo", SqlDbType.VarChar).Value = DateTimeTo;
        SqlParameter sqlParam = new SqlParameter("@result", SqlDbType.Int);
        sqlCom.Parameters.Add(sqlParam);
        sqlCom.Parameters["@result"].Direction = ParameterDirection.Output;

        try
        {
            sqlCon.Open();
            using (SqlDataReader reader = sqlCom.ExecuteReader())
            {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            c = new Coordinates()
                            {
                                Longitude = reader["Longitude"].ToString(),
                                Latitude = reader["Latitude"].ToString()
                            };
                            Coords.Add(c);

                        }
                        return Coords;
                    }
                    else
                    {
                        c = new Coordinates()
                        {
                            Error = "No Data Found for Given Input"
                        };
                        Coords.Add(c);
                        return Coords;
                    }
                
                }
           
          }

        catch (Exception)
        {

            c = new Coordinates() 
            { 
                Error = "Something Went Wrong" 
            };
            Coords.Add(c);
            return Coords;

        }

        finally
        {
            sqlCon.Close();
        }
    }


how ?
Posted

Refer - String was not recognized as a valid DateTime.[^] for a complete answer.
 
Share this answer
 
SQL
sqlCom.Parameters.Add("@DateTimeFrom", SqlDbType.VarChar).Value = DateTimeFrom;
sqlCom.Parameters.Add("@DateTimeTo", SqlDbType.VarChar).Value = DateTimeTo;


Are these stored procedure parameters really strings? (VarChar), if so I'd look at changing the proc so they are DateTime instead.

You can get into all sorts of horrid problems representing dates as strings.
 
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