rito1
asked on
Checking a Dataset for DBNull whiclst using the Compute SUM function
Hi All
I am using a Dataset's Compute function but get the error 'Object cannot be cast from DBNull '. Obviously there is no data being returned with the TYPE_ID 1.Please could you let me know how I could check the the returning SUM for DBNull...
ds.Tables("reporttbl").Com pute("SUM( TOTAL_MONT H_WEIGHT)" , "TYPE_ID=1")
Many thanks
Rit
I am using a Dataset's Compute function but get the error 'Object cannot be cast from DBNull '. Obviously there is no data being returned with the TYPE_ID 1.Please could you let me know how I could check the the returning SUM for DBNull...
ds.Tables("reporttbl").Com
Many thanks
Rit
ASKER
Hi
Good idea though i get the following error based on the syntax above..
Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier
Rit
Good idea though i get the following error based on the syntax above..
Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier
Rit
why not check your row for dbNull value before you compute it?
if(myfield = System.DBNull.Value)
{
//Do something like giving a default value
}
else
{
//Compute
}
if(myfield = System.DBNull.Value)
{
//Do something like giving a default value
}
else
{
//Compute
}
ASKER
Hi
I am not sure this will help as its actually the criteria TYPE_ID=1 that is causing the issue as in there are rows within the datatable but non with the TYPE_ID 1 on this occasion.
Rit
I am not sure this will help as its actually the criteria TYPE_ID=1 that is causing the issue as in there are rows within the datatable but non with the TYPE_ID 1 on this occasion.
Rit
ASKER
This may help.. The aggregate from the compute function is being assigned to a Decimal variable like so...
Dim conftotal As Decimal = 0.0
conftotal = Convert.ToDecimal(ds.Table s("reportt bl").Compu te("SUM(TO TAL_MONTH_ WEIGHT)", "TYPE_ID=1"))
Rit
Dim conftotal As Decimal = 0.0
conftotal = Convert.ToDecimal(ds.Table
Rit
ASKER
How about this for a workaround
'HERE IS MY INITIAL DATASET
Dim ds As DataSet = Snapshotdata.RetrieveData( )
'I THEN CREATE A DATAVIEW AND USE THE COUNT FUNCTION TO TEST WHETHER THERE ARE ANY RECORDS BASED ON THE TYPE_ID CREITERIA.
Dim dvConf As New DataView(ds.Tables("report tbl"))
dvConf.RowFilter = "[TYPE_ID] = '1'"
If dvConf.Count = 0 Then
conftotal = 0.0
Else
conftotal = Convert.ToDecimal(ds.Table s("reportt bl").Compu te("SUM(TO TAL_MONTH_ WEIGHT)", "TYPE_ID=1"))
End If
Rit
'HERE IS MY INITIAL DATASET
Dim ds As DataSet = Snapshotdata.RetrieveData(
'I THEN CREATE A DATAVIEW AND USE THE COUNT FUNCTION TO TEST WHETHER THERE ARE ANY RECORDS BASED ON THE TYPE_ID CREITERIA.
Dim dvConf As New DataView(ds.Tables("report
dvConf.RowFilter = "[TYPE_ID] = '1'"
If dvConf.Count = 0 Then
conftotal = 0.0
Else
conftotal = Convert.ToDecimal(ds.Table
End If
Rit
try putting the isnull check on the outside...
ds.Tables("reporttbl").Com pute("ISNU LL(SUM(TOT AL_MONTH_W EIGHT),0)" , "TYPE_ID=1")
it works in SQL...not sure about when using ds.table.compute....
against AdventureWorks...
SELECT ISNULL(SUM(UnitPrice), 0) AS SumPrice
FROM SalesOrderDetail
WHERE (SpecialOfferID = 66)
will return zero in Enterprise Manager
it returns ".0000" in Query Analyzer
SELECT SUM(ISNULL(UnitPrice,0)) AS SumPrice
FROM SalesOrderDetail
WHERE (SpecialOfferID = 66)
--returns nothing
ds.Tables("reporttbl").Com
it works in SQL...not sure about when using ds.table.compute....
against AdventureWorks...
SELECT ISNULL(SUM(UnitPrice), 0) AS SumPrice
FROM SalesOrderDetail
WHERE (SpecialOfferID = 66)
will return zero in Enterprise Manager
it returns ".0000" in Query Analyzer
SELECT SUM(ISNULL(UnitPrice,0)) AS SumPrice
FROM SalesOrderDetail
WHERE (SpecialOfferID = 66)
--returns nothing
ps...in my example above, there is no SpecialOfferID in the table
lol...i mean
ps...in my example above, there is no SpecialOfferID=66 in the table
ps...in my example above, there is no SpecialOfferID=66 in the table
ASKER
Hi samtran0331
I see your logic but it doesn't like it. I get the following error:
Please note. Cannot evaluate. Expression 'System.Data.FunctionNode' is not an aggregate.
I can handle using my Dataview solution, its just you would have thought there would be a more streamlined approach woudn't you.
Rit
I see your logic but it doesn't like it. I get the following error:
Please note. Cannot evaluate. Expression 'System.Data.FunctionNode'
I can handle using my Dataview solution, its just you would have thought there would be a more streamlined approach woudn't you.
Rit
lol...yeah...wasn't sure if ".Compute" was going to like that "isnull" or not....
are you using the dataset for other purposes?
are you using the dataset for other purposes?
ASKER
Yep, 3 .Computes of similar nature. Doh!
Rit
Rit
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
VB used to have a function Nz. I recreated it and use it to filter out nulls and nothing and replace with a default value.
Dim confTotal as Decimal = Nz(ds.Tables("reporttbl").Compute("SUM(TOTAL_MONTH_WEIGHT)", "TYPE_ID=1"),0)
'Some where else in your class or use Public Shared Function in another class
Private Function Nz(ByVal value As Object, ByVal replaceValue As Object)
If value Is Nothing Then
Return replaceValue
ElseIf value.Equals(DBNull.Value) Then
Return replaceValue
ElseIf value.GetType.ToString = "System.DateTime" And value = Nothing Then
Return replaceValue
Else
Return value
End If
End Function
ds.Tables("reporttbl").Com