0

I kept getting the above error when trying to bind an XML document to a GridView. The problem is that some of the XML documents were missing the column and some weren't for some unknown reason. The XML documents that were missing the column threw the error above and blew up the page.

How does one account for missing columns in an XML document, so the page can still load successfully?

1 Answer 1

0

The answer is to first check for the existence of the column that could be missing. If the column is missing, you need to create it. Then you can bind the DataSet to the GridView.

                if (myDataSet.Tables["myTable"] != null)
                {
                    if (myDataSet.Tables["myTable"].Columns.Contains("myColumn"))
                    {
                        gvSkills.DataSource = myDataSet.Tables["myTable"];
                        gvSkills.DataBind();
                    }
                    else
                    {
                        DataColumn colSkill = new DataColumn();
                        colSkill.ColumnName = "myColumn";
                        myDataSet.Tables["myTable"].Columns.Add(colSkill);

                        gvSkills.DataSource = myDataSet.Tables["myTable"];
                        gvSkills.DataBind();
                    }
                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.