Gaurav_0093
Gaurav_0093

Reputation: 1020

Invalid cast from 'System.String' to 'System.TimeSpan'

Hello I am using this stored procedure to get the data from different tables Here is the stored procedure

DECLARE @default time='16:43:22.560'
  SELECT
  ev.Id,
  ev.Title,
  ev.PageUrl,
  ev.FromDate,
  ev.ToDate,
  ev.Isactive,
 CONVERT(char(10), ISNULL(eventtime,@default), 108) as EventTime,  
  ev.UserType,
  ev.Street,
  ev.Image,
  ev.Description,
  ev.City,
  ev.CountryCode,
  ev.CategoryId,
  ev.UserId,
  ev.StateCode,
  cm.Name as 'CountryName',
  sm.name as 'StateName',
  asp.FirstName as 'FirstName',
  Cat.Name as 'CategoryName',
  ev.ZipCode
 from events ev
 inner join countrymaster cm on ev.CountryCode=cm.Id
 inner join statemaster sm on ev.StateCode=sm.Id
 inner join category cat on ev.Categoryid=cat.Id
 left join aspnetusers asp on ev.userid=asp.Id
 order by createddate desc

in the 7th column event time I am casting it and checking that there is null value in that column then set the default value. But when I execute stored procedurue through my c# code hers is my code

public static async Task<IEnumerable<Event>> GetallPostedEvents()
        {
            using (var con = DbHelper.SqlConnection())
            {
                return await con.QueryAsync<Event>(GetEventsSP, commandType: CommandType.StoredProcedure);

it returns error like this

System.String' to 'System.TimeSpan

the datatype of EventTime column in table is time

and the datatype of EventTime in my c# code is

 public TimeSpan?EventTime { get; set; }

please help.

Upvotes: 0

Views: 2598

Answers (1)

FakeisMe
FakeisMe

Reputation: 464

Note: In SP you are doing char(10) conversion and that's y it is string and mapping fails.

You can do this

EventTime = TimeSpan.Parse(dbstring);

Since you are using mapper directly to type Event. Map it to a string and then do parsing.

Upvotes: 1

Related Questions