Skip to content
Advertisement

System.InvalidCastException: When trying to add an object to a SQL database using Entity Framework in C#

I am trying to use Entity Framework in C# to add objects to a SQL database. The data is coming from a combination of textboxes and datepicker controls. I’m guessing this is a problem with how I’m parsing the data but the error seems vague to me. Any advice would be greatly appreciated.

Here is the code that adds the data to the database.

private void new_btn_Click(object sender, RoutedEventArgs e)
{
        var load = new Load
        {
            pro_num = pro_txt.Text,
            quote_num = quote_txt.Text,
            ref_num = ref_txt.Text,
            weight = Convert.ToDouble(weight_txt.Text),
            pieces = Convert.ToInt32(pieces_txt.Text),
            commodity = commodity_txt.Text,
            mileage = Convert.ToDouble(mileage_txt.Text),
            carrier_rate = Convert.ToDecimal(carrierRate_txt.Text),
            customer_rate = Convert.ToDecimal(customerRate_txt.Text),
            pick_appointment = pickDate_picker.SelectedDate,
            drop_appointment = dropDate_picker.SelectedDate,
            driver_id = Convert.ToInt32(driver_txt),
            dispatch_id = Convert.ToInt32(dispatch_txt),
            customer_id = Convert.ToInt32(customer_txt),
            broker_id = Convert.ToInt32(broker_txt),
        };

        HotloadModel.Loads.Add(load);
        HotloadModel.SaveChangesAsync();
}

Here is the code for the EF entity class:

public partial class Load
{
    public int bol_num { get; set; }
    public string pro_num { get; set; }
    public string quote_num { get; set; }
    public string ref_num { get; set; }
    public Nullable<double> weight { get; set; }
    public Nullable<int> pieces { get; set; }
    public string commodity { get; set; }
    public Nullable<double> mileage { get; set; }
    public Nullable<decimal> carrier_rate { get; set; }
    public Nullable<decimal> customer_rate { get; set; }
    public Nullable<System.DateTime> pick_appointment { get; set; }
    public Nullable<System.DateTime> drop_appointment { get; set; }
    public Nullable<int> driver_id { get; set; }
    public Nullable<int> dispatch_id { get; set; }
    public Nullable<int> customer_id { get; set; }
    public Nullable<int> broker_id { get; set; }
}

Here is the schema for my database table.

CREATE TABLE [dbo].[Loads] 
(
    [bol_num]          INT IDENTITY (1, 1) NOT NULL,
    [pro_num]          VARCHAR(20)  NULL,
    [quote_num]        VARCHAR(20)  NULL,
    [ref_num]          VARCHAR(20)  NULL,
    [weight]           FLOAT(53)    NULL,
    [pieces]           INT          NULL,
    [commodity]        VARCHAR(20)  NULL,
    [mileage]          FLOAT(53)    NULL,
    [carrier_rate]     MONEY        NULL,
    [customer_rate]    MONEY        NULL,
    [pick_appointment] SMALLDATETIME NULL,
    [drop_appointment] SMALLDATETIME NULL,
    [driver_id]        INT           NULL,
    [dispatch_id]      INT           NULL,
    [customer_id]      INT           NULL,
    [broker_id]        INT           NULL,

    PRIMARY KEY CLUSTERED ([bol_num] ASC)
);

Advertisement

Answer

I realized after staring at the code for 15 minutes that I neglected to specify the .Text property on these lines

        driver_id = Convert.ToInt32(driver_txt),
        dispatch_id = Convert.ToInt32(dispatch_txt),
        customer_id = Convert.ToInt32(customer_txt),
        broker_id = Convert.ToInt32(broker_txt),

In addition I used this link from a related SO post to verify I was converting my values correctly. https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings?redirectedfrom=MSDN

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement