September 19, 2012 by Nitesh

Solution – Cannot serialize the DataTable. DataTable name is not set

.Net Library provides a very easy way to generate the XML schema of any dataset/data table using the WriteXmlSchema(). For this, we need to execute the following code:

dt.WriteXmlSchema("sample.xsd")

Using the above code, the schema of the datatable dt will be retrieved and saved in sample.xsd file. However, sometimes using the above code throws an error at run time as “Cannot serialize the DataTable. DataTable name is not set“.

As the error implies, a data table can not be serialized until or unless it has a name. To resolve this error, we need to provide a name to the data table using the TableName property. Assign the data table a name before calling the WriteXmlSchema() and you’re done.

dt.tableName=”MySampleTable”

The complete sample to retrieve the Xml Schema of a Data table looks as below. Ensure that the TableName property is assigned after getting the data in the data table, else you will continue to get the error!

Dim dt As New DataTable()
dt = GetData() //Get the data here
dt.TableName = "MySampleTable"
dt.WriteXmlSchema("sample.xsd")

Hope this post helps you!

#.Net#C##VB.Net
  • Ellingsen

    Good post and right to the point. I don’t know if this is in fact the best place to ask but do you folks have any idea where to get some professional writers? Thanks in advance 🙂

  • Beats

    That’s a nice post.

  • gurunathan natarajan

    Thank You //// It’s Work

  • Cornad wii

    thank you guy, nice!

  • Miroslav

    I have tableName set but still get this error while serializing into byte array:

    BinaryFormatter bf = new BinaryFormatter();
    using (var ms = new MemoryStream())
    {
    bf.Serialize(ms, myTableWithTableNameSet); // Exception is thrown here
    }

    • This should not be an issue. Can you post more code to figure out the issue?

      • Miroslav

        Hi Nitesh,
        I know it shouldn’t be an issue.
        I can’t provide more code as the code above is already the whole function body. I just omitted function name and braces
        The code also works perfectly in other projects with the similarly structured tables (the same set of columns).
        I’ve also noticed that if I feed an empty table to it (no rows, but having structure), the code works.
        I’ve tried to remove rows one by one to get the failing one – all of them were failing. I’ve also tried to set all row items to their types’ values and to null or DBNull to no avail.

        • If all the rows are failing, there might be a chance, one of the column datatypes is creating such issue. Try removing columns and see if you figure out a problematic column?

          • Miroslav

            Hi Nitesh,
            Indeed. I had a dynamically added column of type DataTable which I have totally missed and was 100% sure it’s not there.
            As they say “Double checking is our everything.”
            The exception was pointing to these tables. As soon as I ran over the main datatable and gave names to these sub-datatables the exception were gone.
            Thank you for your help.

          • Hi Miroslav,

            Glad the issue is resolved. 🙂

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup