Solved

VB.NET System Call Fails - RuntimeCallableWrapper

Posted on 2013-02-05
6
2,803 Views
Last Modified: 2013-02-06
VB.NET program, Local Access Database

My program is generating this error -:

DisconnectedContext was detected
Message: Transition into COM context 0x676010 for this RuntimeCallableWrapper failed with the following error: System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)). This is typically because the COM context 0x676010 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0x675ea0). This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them.

The error started occuring after I added a second call to the database inside a loop.

The program I wrote loops through all items in a database.
It sums up the total number of items sold (located in one table) in one query, then in another query it sums up the total number of items on a Credit Memo -
so that the number of items on the credit memo can be subtracted from the total number of items sold.

Below is how it looks -
"iis" = integer (inventory items sold)
"cms" = integer (credit memo sold)

 sql2 = "select sum(InvoiceLineQuantity) as QSold from InvoiceLine where txnDate <= #" & newStart & "# and txnDate >= #" & newEnd & "# and InvoiceLineItemRefFullName = '" & ds.Tables(0).Rows(x).Item("FullName") & "'"
                    da2 = New OleDbDataAdapter(sql2, cn)
                    ds2 = New DataSet()
                    da2.Fill(ds2, "table1")
                    If ds2.Tables(0).Rows(0).Item("QSold").Equals(System.DBNull.Value) Then
                        iis = 0
                    Else
                        iis = ds2.Tables(0).Rows(0).Item("QSold").ToString()
                    End If

'Credit memo section (added to subtract item returns for accurate total sales)
sql2 = "select sum(CreditmemoLineQuantity) as CMSold from CreditmemoLine where txnDate <= #" & newStart & "# and txnDate >= #" & newEnd & "# and CreditMemoLineItemRefFullName = '" & ds.Tables(0).Rows(x).Item("FullName") & "'"
                    da3 = New OleDbDataAdapter(sql2, cn)
                    ds3 = New DataSet()
                    da3.Fill(ds3, "table1")

                    If ds3.Tables(0).Rows(0).Item("CMSold").Equals(System.DBNull.Value) Then
                        cms = 0
                    Else
                        cms = ds3.Tables(0).Rows(0).Item("CMSold")
                    End If

Open in new window


After the code runs, I subtract "cms" from "iis" to get "totalSales"

this code loops for each item from a specific vendor.  Some vendors have 20 items, our biggest vendor has 800+ items.  The error happens when I run the 800+ item vendor.

I'm a self taught programmer, so perhaps i'm going about this the wrong way - but I don't understand how a COM error happens - or how it could be related to the database calls.  But, since the report ran just fine prior to me adding the credit memo section, that's what must be causing the error.

Can anyone provide any insight into what might be causing the error, or what I can do to prevent it?  The compiler did not give me a pin-point spot or line number for the error...

Thanks for your help!
0
Comment
Question by:slightlyoff
6 Comments
 
LVL 39

Expert Comment

by:appari
ID: 38858267
are you executing the application in debug mode? normally this error is displayed only if you run the application in a debug session. try executing the exe file and see if you are still getting the error.
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 38858311
The problem is that the process takes too long to complete.

try to create new  connection object in second oledbadapter also.
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38858347
You can do it with one SQL statement and two derived tables (sub-selects); and even handle the nulls with NZ() too.
I haven't concatenated all the SQL for you, but this should give you the general idea.
SELECT NZ([t1].[QSold],0)+NZ([t2].[CMSold],0) AS totalSales
FROM 
  (SELECT Sum(CreditmemoLineQuantity) as QSold FROM CreditmemoLine)  AS t1
 ,(SELECT Sum(InvoiceLineQuantity) AS CMSold FROM InvoiceLine)
 AS t2;

-- returns:
totalSales
20

Open in new window

Then you populate your OleDbDataAdapter and DataSet using the concatenated SQL and pull the totalSales from your DataSet in one bite.
  ...
totalSales= ds.Tables(0).Rows(0).Item("totalSales")

I suspect your connection is going out of scope, most likely because you are not closing it in the loop, essentially leaving 800 open connections, some of which will be cleaned up the garbage collector, but eventually you will exceed your max open connections limit.


Alan ";0)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:slightlyoff
ID: 38861092
Thanks for the replies!

Alan, I tried your SQL String - finishing it with the where clause:
SELECT NZ([t1].[QSold],0)-NZ([t2].[CMSold],0) AS totalSales FROM (SELECT Sum(CreditmemoLineQuantity) as CMSold FROM CreditmemoLine where CreditMemoLine.txnDate <= #11/1/2012# And CreditmemoLine.txnDate >= #8/3/2012# And CreditmemoLine.CreditMemoLineItemRefFullName = 'HA:BRGMP01AS') AS t2 , (SELECT Sum(InvoiceLineQuantity) AS QSold FROM InvoiceLine where InvoiceLine.txnDate <= #11/1/2012# And InvoiceLine.txnDate >= #8/3/2012# And InvoiceLine.InvoiceLineItemRefFullName = 'HA:BRGMP01AS') AS t1

Open in new window


When I execute it in access, it works as advertised :)  - However, when I run it in VB.NET, I get :  Undefined function 'NZ' in expression.

I have changed my code to be this:

cn2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\quickbooks2.mdb;Persist Security Info=True"
                    cn2.Open()
                    da2 = New OleDbDataAdapter(sql2, cn2)
                    ds2 = New DataSet()
                    da2.Fill(ds2, "table1")
                    TotalSales = ds2.Tables(0).Rows(0).Item("totalSales")
                    cn2.Close()

Open in new window


VB.NET doesn't like the NZ function, which is too bad, cause that looks handy.  I'm not sure if there is away around it - but I'm checking for nulls before setting the variable, so I think that will work.

Thank you so much for your help!
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 500 total points
ID: 38861297
Maybe try the Microsoft.ACE.OLEDB.12.0 provider, depends which version the Access DB is, 12.0 is for an Access 2007 catalog. Alternatively you could try using the Jet immediate if function in the SQL:
SELECT IIF([t1].[QSold]=NULL,0,[t1].[QSold])+IIF([t2].[CMSold]=NULL,0,[t2].[CMSold]) AS totalSales

Open in new window

I know the VB IIF function had a glitch (not sure if it still does) where it would execute both sides of the IIF, which was a big problem if you were calling some insert or update procedure on each side of the IIF; but the JET IIF has no such limitiation.

Also checking for nulls can be a bit pedantic, =Null, Is Null can return unexpected results, I have sometimes resorted to wrapping the value in Len() function to remove any ambiguity, IIF(Len([t1].[QSold])=0,0,[t1].[QSold]) ...

Anyhow there's a few options.

Alan ";0)
1
 
LVL 1

Author Comment

by:slightlyoff
ID: 38861482
Thanks, I used

SELECT IIF(ISNULL([t1].[QSold])=TRUE,0,[t1].[QSold])- IIF(ISNULL([t2].[CMSold])=TRUE,0,[t2].[CMSold]) as totalSales FROM (SELECT Sum(CreditmemoLineQuantity) AS CMSold FROM CreditmemoLine where CreditMemoLine.txnDate <= #11/1/2012# And CreditmemoLine.txnDate >= #8/3/2012# And CreditmemoLine.CreditMemoLineItemRefFullName = 'SB:SB044') AS t2 , (SELECT Sum(InvoiceLineQuantity) AS QSold FROM InvoiceLine where InvoiceLine.txnDate <= #11/1/2012# And InvoiceLine.txnDate >= #8/3/2012# And InvoiceLine.InvoiceLineItemRefFullName = 'SB:SB044') AS t1

Open in new window


And it works as its supposed to.  Thank you very much for your help!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now