Column longtext exceeds the MaxLength limit

2.2k Views Asked by At

This error occur on modifying data in datable if dt has longtext column. First I load datatable:

 var command = new MySqlCommand(queryTableData, connection);
 DataTable table = new DataTable();

 try
 {
      connection.Open();
      table.Load(command.ExecuteReader());
 }

And when I am trying to modify any column data:

table.BeginLoadData();
for (var i = 0; i < table.Rows.Count; i++)
{
    table.Rows[i]["columnName"] = value;
}
table.EndLoadData();

It fails on table.EndLoadData(); with error: RowError: "Column 'longtext_val' exceeds the MaxLength limit." Also it sets MaxLength: 0 property of longtext column to 0, other properties have normal max length. In database it is set to '4294967295'.

I can avoid this by removing constraints at all:

    using (var reader = command.ExecuteReader())
    {
        DataSet dataSet = new DataSet();
        dataSet.Tables.Add(table);
        dataSet.EnforceConstraints = false;
        table.Load(reader);
    }

Last snippet is from mysql site

But it's not an option in my case, it is possible to change max length of this column? Or is there a solution?

P.S. DataType in MySQL looks like: name: 'longtext_val', default: NULL, is_nullable: 'YES', data_type: 'longtext', character_max_length: '4294967295', character_set_name: 'utf8', collation_name: 'utf8_general_ci',
column_type: 'longtext'

Note that I do not know schema in advance, this db is used only for test purposes.

P.S.S. I did not find exact solution so I had to do a workaround.

I thought about two things: 1. Select only columns that are not longtext 2. Deal with actual problem - update of datatable.

MySQL datatables throw error row-based, so If you have any error on your column and you are trying to modify any another column - you will get an exception.

MsSQL raises an exception on column-based. So it wont throw an exception if you are trying to modify non-error column.

Thus I get all distinct columns with errors and if column I am interested in threw an exception I will handle:

 var errorColumns = table.GetErrors()
    .SelectMany(row => row.GetColumnsInError()).Distinct().ToList();

if (errorColumns.Any(column => column.ColumnName == columnMeta.ColumnName))
{
    //handle
}
0

There are 0 best solutions below