Search code examples
c#ms-accessoledboledbexceptiondesignview

Can I programmatically determine (OLEDB) which column length is being exceeded?


For some reason, I can't open the Design View of the MS Access table in question; I can look at the data but not the desing, specifically, the length of columns.

When I try to insert a record into said table with this code:

using (var conn = new OleDbConnection(connStr))
{
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = 
            @"INSERT INTO tx_header (tx, site_no, xmlfile, collect_dttm, ccr_user, tx_memo, file_beg, file_end) 
              VALUES(@txval, @siteNum, @xmlfileName, Now(), @ccrUser, @TXMemo, @strfile_beg, @strfile_end)";
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@txval", tx);
        cmd.Parameters.AddWithValue("@siteNum", site_no);
        cmd.Parameters.AddWithValue("@xmlfileName", xmlfile);
        cmd.Parameters.AddWithValue("@ccrUser", ccr_user);
        cmd.Parameters.AddWithValue("@TXMemo", tx_memo);
        cmd.Parameters.AddWithValue("@strfile_beg", file_beg);
        cmd.Parameters.AddWithValue("@strfile_end", file_end);
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

...I get, "System.Data.OleDb.OleDbException was unhandled by user code HResult=-2147217833 Message=The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data. Source=Microsoft Office Access Database Engine"

Rather than have to guess which column has too much data, it would be nice if I could programmatically determine which column is the problematic one. Can I? How?


Solution

  • There's a pretty detailed explanation of how to query the underlying schema information in MSDN, starting at Retrieving Database Schema Information.

    Disclaimer: I've never tried using that against an Access database.