Type of value has a mismatch with column type

Posted on 2006-05-19
Last Modified: 2007-12-19
After migrating to VS2005 / .net 2 The following code gets me a server error:

          public DataTable CreateDataSource(DataSet ds,int page_current, int page_products)

               int startIndex = page_current * page_products;

               DataTable dt = new DataTable();

               for (int i = 0; i < ds.Tables[0].Columns.Count ; i++)
                    dt.Columns.Add(ds.Tables[0].Columns[i].ColumnName, ds.Tables[0].Columns[i].GetType() );

            for (int i = startIndex; i < (startIndex + page_products); i++)

                if (i < ds.Tables[0].Rows.Count)
                    DataRow dr = dt.NewRow();

                    for (int k = 0; k < dt.Columns.Count; k++)
                        dr[k] = ds.Tables[0].Rows[i][k];

                    i = startIndex + page_products;

               return dt;

The error is:

Server Error in '/' Application.

Type of value has a mismatch with column type
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Type of value has a mismatch with column type

Source Error:

Line 289:                         DataRow dr = dt.NewRow();
Line 290:                         for (int k=0;k<dt.Columns.Count;k++){
Line 291:                              dr[k] = ds.Tables[0].Rows[i][k];
Line 292:                         }
Line 293:                         dt.Rows.Add(dr);

Any ideas why this is happening? It works perfectly under vs2003.

Question by:infodigger
    LVL 6

    Accepted Solution

    I think the problem is the line

      dt.Columns.Add(ds.Tables[0].Columns[i].ColumnName, ds.Tables[0].Columns[i].GetType() );

    because the GetType() there will always return DataColumn. Instead you should use .DataType like

      dt.Columns.Add(ds.Tables[0].Columns[i].ColumnName, ds.Tables[0].Columns[i].DataType);

    Why It did work in 2003 I don't know...


    Author Comment

    Thank you so much for the fast reply! It worked perfect!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now