Search code examples
c#ms-access-2007oledbjetoledbcommand

Weird exception with OLEDB Parameter Insert


Getting a strange error when trying to insert data into an Access database using parameters. the line where I am getting a problem is :-

                thisCommand.CommandText = "INSERT INTO Events (Venue_ID, Date_Start, Date_End, Time_Start, Time_End, Name, Description, Event_Type, Buy_Tickets_URL) VALUES (@VenID, @DStart, @DEnd, @evTime, @evTime, @Name, @Des, @EvType, @SysUrl);";

                //Other Parameters already inserted here
                string desc = GetDesc(rec.EvName);
                thisCommand.Parameters.AddWithValue("@Des", desc);
                thisCommand.ExecuteNonQuery();

None of the other parameters cause a problem but when trying to insert data to the description field I get a database exception saying the field is too small to accept the amount of data. The problem is my program is only trying to insert 3 characters when it throws the error and the Description field is a memo so should be able to hold up to 65000+ characters. When inserting a value manually in the CommandText everything works fine so it must be something to do with the parameter properties.


Solution

  • Parameter names are just a guide for you, for example, this runs, note that all parameters have the same name! Note also that the reserved word Name is escaped by square brackets.

        thisCommand.CommandText = "INSERT INTO Events (Venue_ID, Date_Start, " +
        "Date_End, [Name], Description, Event_Type, Buy_Tickets_URL) " +
        "VALUES (@VenID, @DStart, @DEnd, @Name, @Des, @EvType, @SysUrl);";
    
    
        thisCommand.Parameters.AddWithValue("@Des", 1);
        thisCommand.Parameters.AddWithValue("@Des", DateTime.Now.Date);
        thisCommand.Parameters.AddWithValue("@Des", DateTime.Now.Date);
        thisCommand.Parameters.AddWithValue("@Des", "abc");
        thisCommand.Parameters.AddWithValue("@Des", "abc");
        thisCommand.Parameters.AddWithValue("@Des", 1);
        thisCommand.Parameters.AddWithValue("@Des", "abc");
    

    Reserved words