Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2007

Tip: Looking for answers? Try searching our database.

Field Truncation in a DataTable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MarcG - 15 Mar 2007 01:41 GMT
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
Linda Liu [MSFT] - 15 Mar 2007 08:40 GMT
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.
MarcG - 15 Mar 2007 14:36 GMT
Perfect Linda.

Thanks
Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.