0

What are the essential controls before getting a piece of data from a row of a table of a dataset in c#? In order to get rid of the errors while converting a nullable data into decimal, or getting a coloumn that may not be in the datasource any more?

What do I do to validate the existence of a column?

I check if the datarow is not DBNULL.Value before a convert operation to decimal. I do check if the coloumn exists among the coloumns of the datarow.

What I do look for?

Are there any util class to manuplate the datasets, datatables to get row or an attribute's data? Or please tell me the possible check list over datasets, datatables, datarows to be always sure about no convertion error, no such an error linking with a dataset and its child objects.

Thank you!

Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
Bilgin Kılıç
  • 8,707
  • 14
  • 41
  • 67

1 Answers1

3

If you want to check if a column exists in the datatable you can simply do this:

YourDataTable.Columns.Contains("column")

If you want to check if the value of a target row is null then I would do like this:

if(!Convert.IsDBNull(YourDataTable.Rows[0]["column"]))
{
   //Something
}

If you get a dataset back you proboly also want to check if there is a DataTable

If(YourDataSet.Tables.Count>0)
{
   //something
}

Depending on how may rows you are expecting. If you are expecting one row the you can do this:

if(YourDataTable.Rows.Count>0)
{
  //something
}
Arion
  • 31,011
  • 10
  • 70
  • 88