Total of String Column in Dataset

August 20, 2008 by Rakesh Bindal


'Column whose total has to be calculated must contain numeric values

'Dataset is created through Xmldocument

Dim objXmlDoc As New XmlDocument()

Dim objXmlReader As XmlNodeReader

Dim objDS As New DataSet()

Dim inttotal As Integer = 0

Dim dc As New DataColumn("tempSalary", GetType(System.Int32))

objXmlDoc.LoadXml('< Employees> <emp id='100' name='abc' sal='2000' /> <emp id='101' name='def' sal='3000' /> <emp id='102' name='prq' sal='8000' /> <emp id='103' name='xyz' sal='10000' /> </Employees>')

objXmlReader = New XmlNodeReader(objXmlDoc)

objDS.ReadXml(objXmlReader)

objDS.Tables(0).Columns.Add(dc)

objDS.Tables(0).Columns("tempSalary").Expression = "Convert(sal,'System.Int32')"

inttotal = Convert.ToInt32(objDS.Tables(0).Compute("sum(tempSalary)", ""))