Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8488805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:39:03+00:00 2026-06-10T21:39:03+00:00

Is there a way to allow data to get truncated if its too long

  • 0

Is there a way to allow data to get truncated if its too long when inserting it from one
row to another row? Whats happening now is that it gets an error and doesn’t add the row if one of the fields is too long.

Here is the piece of code that I have:

DataRow dr = dt.NewRow();  
for (int j = 0; j < edi50.Columns.Count; j++)  
    dr[j] = dr50[j];

dt.Rows.Add(dr);  

try  
{  
    RemoveNulls(dt);  
    daEDI40050.Update(dt);  
}  
catch (Exception e)  
{  
    string m = e.Message;  
}    

I have a description field 25 chars long but the data is 34 chars going into it. I want to be able to have it insert the 25 and truncate the rest and still add the row.

thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:39:05+00:00Added an answer on June 10, 2026 at 9:39 pm

    You could get the schema from database first(untested):

    DataTable schema;
    using (var con = new System.Data.SqlClient.SqlConnection(conStr))
    {
        var getSchemaSql = String.Format("SELECT * FROM {0}", tableName);
        using (var schemaCommand = new System.Data.SqlClient.SqlCommand(getSchemaSql, con))
        {
            con.Open();
            using (var reader = schemaCommand.ExecuteReader(CommandBehavior.SchemaOnly))
            {
                schema = reader.GetSchemaTable();
            }
        }
    }
    

    and then something similar to this:

    for (int j = 0; j < schema.Rows.Count; j++)
    {
        DataRow schemaRow = schema.Rows[j];
        Type dataType = schemaRow.Field<Type>("DataType");
        int columnSize = schemaRow.Field<int>("ColumnSize");
        if (dataType.FullName == "System.String")
        {
            String value = dr50[j] as String;
            if (value != null)
                value = value.Substring(0, columnSize);
        } 
     }
    

    Again, totally untested and written from scratch, but it might give you an idea how to get the column size. Of course this works only for string, but i assume that this is what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to allow the user to add additional items to one
Is there any way of reading and writing the data from a Microsoft Project
Is there a way to get NSMutableArray to allow you to have holes within
Is there a way to allow admins to modify the content of simple static
Is there a way to allow a user, after he has created a vector
Is there a way to allow an HTML file to open an application on
Using SQL Server 2008, is there a way to allow inserts to a table
Using htmlentities() is there a way I can set to allow only <b> and
I am wondering if there is a simple way to allow a JQuery datatable
I'm hoping there's a way in a config file or something that will allow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.