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

  • Home
  • SEARCH
  • 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 8197471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:44:53+00:00 2026-06-07T05:44:53+00:00

I want to get max size of any column of a table using datatable

  • 0

I want to get max size of any column of a table using datatable in C#. Example is that if i have a table in database named dept and its attributes are NAME and ADDRESS and i have set size of NAME as varchar(50) and Address as varchar(30).

Now i want to get 50 and 30…..

i have written this code

DataSet ds1 = new DataSet();
    DataSet ds = new DataSet();
        string query = Form1.query1Pass;

        SqlCommand cmd = new SqlCommand(query.ToString(), con);
        this.tablename1 = query.Substring(query.LastIndexOf("from")+4); ;
        cmd.ExecuteNonQuery();

        SqlDataAdapter adp = new SqlDataAdapter(cmd);


        adp.Fill(this.ds);
          string query2 = Form1.query2Pass;
        cmd = new SqlCommand(query2, con);
        adp = new SqlDataAdapter(cmd);

        adp.Fill(this.ds1);

            StringBuilder datatype2 = new StringBuilder();
            this.datatype2.Append("Data Type Conflict: ");
            this.datetimeCon.Append("DateTime Format Conflict: ");
            DataTable table1 = this.ds.Tables[0];
            DataTable table2 = this.ds1.Tables[0]; ;
            DataColumnCollection col1 = table1.Columns;
            DataColumnCollection col2 = table2.Columns;
            foreach (DataColumn ds in col1) {
                foreach (DataColumn ds2 in col2) {
                    if (ds.ColumnName.Equals(ds2.ColumnName)) {
                        if (!ds.DataType.Equals(ds2.DataType)) {
                            this.datatype2.Append(ds.ColumnName + "<" + ds.DataType + ">, " + ds2.ColumnName + "<" + ds2.DataType + ">,");
                        }
                        else 
                            if (ds.MaxLength!=ds2.MaxLength) {
                            maxLength.Append(ds.ColumnName+"<"+ds.MaxLength+">, "+ds2.ColumnName+"<"+ds2.MaxLength+">,");
                        }

                    }
                }
            }
  • 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-07T05:44:54+00:00Added an answer on June 7, 2026 at 5:44 am

    I assume that the columns are not Text columns. The MaxLength property is only set on columns with DataType=String. MSDN:

    The MaxLength property is ignored for non-text columns. A
    ArgumentException exception is raised if you assign MaxLength to a
    non-string column.

    By the way, you might want to use a LINQ approach, but that’s a matter of taste:

    var t1Cols = table1.Columns.Cast<DataColumn>();
    var t2Cols = table2.Columns.Cast<DataColumn>();
    
    var diffTypes =  
        from t1Col in t1Cols
        join t2Col in t2Cols on t1Col.ColumnName equals t2Col.ColumnName
        where t1Col.DataType != t2Col.DataType
        select string.Format("{0}<{1}>,{2}<{3}>",
            t1Col.ColumnName, t1Col.DataType, t2Col.ColumnName, t2Col.DataType);
    var diffMaxLength = 
        from t1Col in t1Cols
        join t2Col in t2Cols on t1Col.ColumnName equals t2Col.ColumnName
        where t1Col.MaxLength != t2Col.MaxLength
        select string.Format("{0}<{1}>,{2}<{3}>",
            t1Col.ColumnName, t1Col.MaxLength, t2Col.ColumnName, t2Col.MaxLength);
    
    Console.WriteLine("diff. Types: " + string.Join(", ", diffTypes));
    Console.WriteLine("diff. max Length: " + string.Join(", ", diffMaxLength));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get max and min values with other column's values in ms
I have a table listing (gameid, playerid, team, max_minions) and I want to get
In my sql server table, I want to add a computed column that is
I want get all of the Geom objects that are related to a certain
Basically I want get data I already have accessed from javascript and passing it
I want to get list of tables in a given database and a given
I want to create multiple RichTextBox to deal with the UIElement 2048px max size
I want to get file size I'm doing this: my $filename=$query->param(upload_file); my $filesize =
I have a file of true random bytes. I want a function that returns
I have a requirement for a service. The requirement states that I must get

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.