0

I have

xmlDoc.DataSet.Tables["<tablename>"].Columns

I want to loop through those columns via DataColumn, for say this xmlDoc has 10 entries and 5 are null, then the loop should happen for the non-null entries only.

Any thoughts?

2 Answers 2

2

How about this:

foreach (DataColumn column in xmlDoc.DataSet.Tables[""].Columns)
{
    if (column != null)
    {
        // your code here
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes i do have this kind of code, but i get-> System.Exception: Object cannot be cast from DBNull to other types.
Thanks, does it check whether other 5 columns have null value, am getting DBNull cast exception.
@Sharpeye500 then you would want to use if (column != DBNull) instead @Andrew Hare didn't want to hijack a great answer
Sorry, I'm still new here. I didn't mean to hijack your answer. My apologies. msarchet is correct, and you should also make sure your dataset allow for null values.
0
for (int i = 0; i < ds.Tables[0].Columns.Count; i++ )
{
    DataColumn col = ds.Tables[0].Columns[i] != null ? ds.Tables[0].Columns[i] : "<some default value>";
    if (col != "<some default value>")
                    // do something
}

4 Comments

I keep getting this, System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Object cannot be cast from DBNull to other types. Any idea?
I don't really have a place to test this, but have you tried this? --Updated my answer.
Also, be sure to allow the columns in the dataset in question to allow null values for that column. You are trying to copy an xml document from a soap service into a strongly typed dataset. Make sure the dataset has been explicitly set to allow null values for whatever column you believe may have null values.
Thanks. I was having your similar kind of code in one dataset, but didn't have the same kind of logic for another dataset.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.