1

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
}
makambi
  • 1,100
  • 3
  • 13
  • 30
  • when you step thru the code.. do you actually have data in the DataTable..? also have you thought about creating the DataTable and explicitly assigning the DataType in code behind..? for example `DataTable table = new DataTable() DataColumn = longTextName = new DataColumn() longTextNam.DataType = Type.GetType("System.String");` – MethodMan Nov 25 '14 at 18:43
  • I believe the problem might be that strings in C# have a max size of 2,147,483,647 – bowlturner Nov 25 '14 at 18:44
  • 1
    I think that the equiv of `LongText` as seen in MS Access Days in `ANSI SQL` is just `Text` as the datatype.. [MSDN ANSI SQL DataType Equivalent](http://msdn.microsoft.com/en-us/library/bb177899%28v=office.12%29.aspx) – MethodMan Nov 25 '14 at 18:48
  • @DJKRAZE, I thought about setting it manually, but it looks like reinventing the wheel. This thing should be handled by ado net, data table or what ever. – makambi Nov 25 '14 at 18:48
  • @bowlturner, might be, but what is the solution to this? – makambi Nov 25 '14 at 18:49
  • can you show what the datatype schema looks like for that column in the database that you are using..? you may be better off creating a stored procedure and `CAST(CONVERT` the type to Text – MethodMan Nov 25 '14 at 18:49
  • It's not reinventing the wheel sometimes you have to code for what's not given to you .. [MSDN C# DataColumn.ColumnName Property](http://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname%28v=vs.110%29.aspx) – MethodMan Nov 25 '14 at 18:51
  • @makambi well it would need to be converted to something <= that number coming back. The ado.net doesn't know how you want it handled so you'll have to do something, if everything is less than the string size convert it, if not you'll have to split it into 2 – bowlturner Nov 25 '14 at 19:16
  • @DJKRAZE, updated my answer. In my opinion those things should be handled by mysql connector or at least it should be possible to configure it. I do not actually have any changes to handle it myself on datatable.load() – makambi Nov 26 '14 at 08:32
  • `makambi` you have a lot of reading up and learning in regards to understanding how to handle thing manually vs automatically.. so what's the status on your issue.. is there still an outstanding issue and or problem..? – MethodMan Nov 26 '14 at 16:36
  • @dj-kraze, I just did a workaround. Updated my question. – makambi Nov 27 '14 at 14:54

0 Answers0