Field Truncation in a DataTable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a dotnet 1.1 app that I am redeveloping in 2.0. Part of the work
involves replacing code that executed generated SQL commands against a
database with a dataset approach.

In the original system, if text field data was longer than the field size in
the table, the data was simply truncated. This turned out to be a fine result
(sorry).

Using datasets, when I call the
myDatatable.AddMyTypedDatarow(myTypedDatarow) method, I get an
ArgumentException with the message...

"Cannot set column 'label'. The value violates the MaxLength limit of this
column."

I can't find anything in the datatable designer that would let me specify
truncation, which would be my (ugly but) preferred approach.

Alternately, is there an event or more descriptive exception I can hook or
catch so that I can do the truncation myself? I REALLY don't want to have to
check each text field manually. The field data is being loaded from a file
produced by another app that is not concerned about text length.

Thx
Marc
 
Hi Marc,

After doing some research, I found that we could handle the ColumnChanging
event of the DataTable to truncate the proposed value, if necessary.

The ColumnChanging event occurs when a value is being changed for the
specified DataColumn in a DataRow, even if the DataRow has not been added
to the DataTable.

The following is a sample.

public Form2()
{
InitializeComponent();
this.dataSet11.TestTable.ColumnChanging += new
DataColumnChangeEventHandler(TestTable_ColumnChanging);
}
void TestTable_ColumnChanging(object sender, DataColumnChangeEventArgs e)
{
if (e.Column.ColumnName == "columnname")
{
e.ProposedValue = ((string)e.ProposedValue).Substring(0,
maxlengthofcolumn);
}
}

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top