简体   繁体   中英

'Specified cast is not valid' error on call to SQL Server stored procedure

I'm calling a stored procedure in a SQL Server database with the following code.

The idea is to add the relevant events to a list that is a property of the EventSchedule model, but the code is returning a

Specified cast is not valid

error. The stored procedure pulls data from a view I've built. The call stack shows the error is at line 96, not sure if that'll help anyone much. I think I may need another set of eyes to see what I'm missing here.

var command = new SqlCommand("GetEvents", conn)
{
    CommandType = CommandType.StoredProcedure
};

command.Parameters.Add(new SqlParameter("Id", SqlDbType.BigInt, 0, "id"));
command.Parameters[0].Value = schedule.LeagueId;

using (var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        //line 96
        var _event = new Event
        {
            EventId = (Int64) reader["eventid"],
            HomeTeamId = (Int64) reader["home_team_id"],
            TeamName = (string) reader["team_name"],
            HomeTeamScore = (int) reader["home_team_score"],
            AwayTeamId = (Int64) reader["away_team_id"],
            AwayTeamName = (string) reader["Expr1"],
            AwayTeamScore = (int) reader["away_team_score"],
            WinningTeamId = (Int64) reader["winning_teamid"],
            EventStartDttm = (DateTime) reader["event_start_dttm"],
            CurrentDttm = (DateTime) reader["current_dttm"],
            Locked = (bool) reader["locked"]
        };

        schedule.Events.Add(_event);
    }
}

return schedule;

Models:

public class EventSchedule
{
    public Int64 UserId { get; set; }
    public Int64 LeagueId { get; set; }
    public string League { get; set; }
    public int Season { get; set; }
    public int Week { get; set; }
    public IList<Event> Events { get; set; }
}

public class Event
{
    public Int64 EventId { get; set; }
    public Int64 HomeTeamId { get; set; }
    public string TeamName { get; set; }
    public int? HomeTeamScore { get; set; }
    public Int64 AwayTeamId { get; set; }
    public string AwayTeamName { get; set; }
    public int? AwayTeamScore { get; set; }
    public Int64 WinningTeamId { get; set; }
    public DateTime EventStartDttm { get; set; }
    public DateTime CurrentDttm { get; set; }
    public bool Locked { get; set; }
}

Stored proc:

ALTER PROCEDURE [dbo].[GetEvents]
    @Id int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
       eventid, 
       home_team_id, 
       team_name, 
       home_team_score, 
       away_team_id, 
       Expr1, 
       away_team_score, 
       winning_teamid, 
       event_start_dttm, 
       current_dttm, 
       locked 
    FROM 
       leagueScheduleForCurrentWeek 
    WHERE 
       id = @Id
END

Your problem comes from the fact that one of your casts are failing, most likely one of the nullable types from your model. Change from a cast to using as for the nullable types, this will cause DbNull.Value (which is what is being returned by your reader) to be come the null value you want.

    var _event = new Event
    {
        EventId = (Int64) reader["eventid"],
        HomeTeamId = (Int64) reader["home_team_id"],
        TeamName = (string) reader["team_name"],
        HomeTeamScore = reader["home_team_score"] as int?, //here
        AwayTeamId = (Int64) reader["away_team_id"],
        AwayTeamName = (string) reader["Expr1"],
        AwayTeamScore = reader["away_team_score"] as int?, //and here
        WinningTeamId = (Int64) reader["winning_teamid"],
        EventStartDttm = (DateTime) reader["event_start_dttm"],
        CurrentDttm = (DateTime) reader["current_dttm"],
        Locked = (bool) reader["locked"]
    };

If your problem still exists then your model does not match your data table. you will need to go through your types to find out which one does not match. You can make this easier on yourself by breaking out the assignment out from the constructor.

    var _event = new Event();

    _event.EventId = (Int64) reader["eventid"];
    _event.HomeTeamId = (Int64) reader["home_team_id"];
    _event.TeamName = (string) reader["team_name"];
    _event.HomeTeamScore = reader["home_team_score"] as int?;
    _event.AwayTeamId = (Int64) reader["away_team_id"];
    _event.AwayTeamName = (string) reader["Expr1"];
    _event.AwayTeamScore = reader["away_team_score"] as int?;
    _event.WinningTeamId = (Int64) reader["winning_teamid"];
    _event.EventStartDttm = (DateTime) reader["event_start_dttm"];
    _event.CurrentDttm = (DateTime) reader["current_dttm"];
    _event.Locked = (bool) reader["locked"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM