Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am querying an Oracle 9i db from within a vb.aspx page with the following code:

 

strStat1 = "SELECT SUM(C_POINT + C_POLYGON + C_POLYLINE + C_ANNO) FROM DRAW WHERE DPATH IN (" & strPathList & ")"
cmdStat.CommandText = strStat1
Total = CType(cmdStat.ExecuteScalar, Integer)

 

It works fine when the answer is some number. But when it comes back as zero I get a weird error:

 

OCI-22060: argument [2] is an invalid or uninitialized number

 

This is all in a try catch block so it doesn't just die but I would like to be able to trap it so that I can assign zero to the answer gracefully. How can I go about doing that?

Wanna-Be C# Superstar
  • Moderators
Posted

I think that it's Oracle complaining about one of the fields being null, this is not a complaint from .NET. The reason your getting a Zero is that Total is initialized as zero, it's not coming from the executeScalar.

 

Handling this depends on what you want to do in case one of the columns is null, do you still want to SUM the remaining columns?

Visit...Bassic Software
Posted

I would like to sum the remaining columns in the event of a NULL.

 

I'm not sure about it being an Oracle gripe though, because the same query (when the variables are filled in) does execute in TOAD against Oracle. I believe you can Sum even when a field is NULL.

 

In the even all fields are NULL I would like to report a zero.

 

Thanks for the help, I appreciate it.

Wanna-Be C# Superstar
  • *Experts*
Posted

I believe the standard behavior for "SUM" and addition is that if any column of any row is NULL, then the entire result is NULL.

 

My guess would be that the SUM (or the addition - doesn't really matter) is returning NULL. ExecuteScaler should be able to return System.DBNull.Value, but you're casting it directly as Integer which will definitely blow.

 

If you want 0, you could handle it a number of ways. If Oracle supports it (don't have my reference handy), try something like this:

SELECT IsNull(SUM(C_POINT + C_POLYGON + C_POLYLINE + C_ANNO), 0) FROM DRAW 

(I added the IsNull function). If that doesn't work, look at Coalesce (or Coallesce).

 

If all else fails, you can do this in VB with something like:

cmdStat.CommandText = strStat1
Dim o As Object = cmdStat.ExecuteScalar
If o = System.DBNull.Value Then
   Total = 0
Else
   Total = System.Convert.ToInt32(o)
End If

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Figured it out. Just took a little more SQL code

 

SELECT SUM(NVL(C_POINT,0) + NVL(C_POLYGON,0) + NVL(C_POLYLINE,0) + NVL(C_ANNO,0)) FROM DRAW

Wanna-Be C# Superstar

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...