Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:57:43+00:00 2026-06-16T00:57:43+00:00

I have the following SQL/Server function defined in C# to detect when a vehicle

  • 0

I have the following SQL/Server function defined in C# to detect when a vehicle has refuelled. I keep a context of the last fuel used so that I don’t need to move a cursor back over the data:

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlDouble GetFuelRefill(SqlString ID, SqlString FuelLeft)
{
    object _lastID = CallContext.GetData("lastID6");
    object _fuelRefill = CallContext.GetData("fuelRefill");
    double fuelRefill = _fuelRefill == null ? 0.0 : Convert.ToDouble(_fuelRefill);
    object _lastFuelLeft = CallContext.GetData("lastFuelLeft");
    double lastFuelLeft = _lastFuelLeft == null ? 0.0 : Convert.ToDouble(_lastFuelLeft);

    double result = 0.0;

    if ((_lastID == null) || (Convert.ToString(_lastID) != ID.Value) || (_lastFuelLeft == null))
    {
        fuelRefill = 0;
        CallContext.SetData("lastFuelLeft", 0.0);
    }
    else if (!FuelLeft.IsNull)
    {
        double fl = Convert.ToDouble(FuelLeft.Value);
        if ((fl > 0.0) && (lastFuelLeft > 0.0) && ((fl - lastFuelLeft) / fl * 100.0 >= 5.0))
            fuelRefill += fl - lastFuelLeft;
        CallContext.SetData("lastFuelLeft", FuelLeft.Value);
    }
    result = fuelRefill;

    CallContext.SetData("lastID6", ID.Value);
    CallContext.SetData("fuelRefill", fuelRefill);
    return new SqlDouble(result);
}

For the purpose of repeating the problem I have created a small test table:

SequenceNo  AssetID   FuelLeft
1           PJ1       50
2           PJ1       49
3           PJ1       48
4           PJ1       98
5           PJ1       95

Then I execute the following command from SQL/Server Management Studio:

SELECT SequenceNo,dbo.GetFuelRefill(AssetID,FuelLeft) AS Refill 
FROM TestTable ORDER BY SequenceNo

Which yields the following result that I expect:

SequenceNo  Refill
1           0
2           0
3           0
4           50
5           50

However then I try executing the same query using ADO from VBScript:

const DatabaseName = "MyDB"
const DatabaseServer = "(local)"
const adOpenForwardOnly = 0
const adLockOptimistic = 3
Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject")
Dim Conn : Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=" & DatabaseName & ";Server=" & DatabaseServer
Set Query = CreateObject("ADODB.Recordset")
Query.Open "SELECT SequenceNo,dbo.GetFuelRefill(AssetID,FuelLeft) AS Refill FROM TestTable ORDER BY SequenceNo", Conn, adOpenForwardOnly, adLockOptimistic
Set f = FSO.CreateTextFile("E:\Temp\Test.TXT", true)
do while not Query.EOF
    f.WriteLine Query("SequenceNo") & ", " & Query("Refill")
    Query.MoveNext
loop
Set FSO = Nothing
Query.Close
Conn.Close

The test.txt file contains the following:

1, 0
2, 0
3, 0
4, 0
5, 0

Doing some further debugging it appears that the call context isn’t being saved for the duration of the query, but I wondered if anyone knows why and a way to solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:57:44+00:00Added an answer on June 16, 2026 at 12:57 am

    After further experimentation I found two solutions. One was to place the query inside a simple stored procedure such as:

    CREATE PROCEDURE sp_Test
    AS
    BEGIN
        SET NOCOUNT ON;
        SELECT SequenceNo,dbo.GetFuelRefill(AssetID,FuelLeft) AS Refill 
            FROM TestTable ORDER BY SequenceNo
    END
    

    And the other was to change the VBS code to use a client-side cursor:

    const DatabaseName = "MyDB"
    const DatabaseServer = "(local)"
    const adOpenForwardOnly = 0
    const adLockOptimistic = 3
    const adUseClient = 3
    Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject")
    Dim Conn : Set Conn = CreateObject("ADODB.Connection")
    Conn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=" & DatabaseName & ";Server=" & DatabaseServer
    Set Query = CreateObject("ADODB.Recordset")
    Query.CursorLocation = adUseClient
    Query.Open "SELECT SequenceNo,dbo.GetFuelRefill(AssetID,FuelLeft) AS Refill FROM TestTable ORDER BY SequenceNo", Conn, adOpenForwardOnly, adLockOptimistic
    Set f = FSO.CreateTextFile("E:\Temp\Test.TXT", true)
    do while not Query.EOF
        f.WriteLine Query("SequenceNo") & ", " & Query("Refill") 
        Query.MoveNext
    loop
    Set FSO = Nothing
    Query.Close
    Conn.Close
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 and would like to have a user defined function
I have been writing a user-defined function in SQL Server: It looks like this
I have the following code (Database is SQL Server Compact 4.0): Dim competitor=context.Competitors.Find(id) When
I have a user defined function in a SQL Server 2005 database which returns
I'm using SQL Server and ASP.NET. I have the following function: Using js =
I have the following SQL Server query: SELECT area FROM places WHERE REPLACE(area,'-',' ')
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
I'm using SQL Server 2005 and have the following T-SQL statement: DECLARE @MP VARCHAR(500)
I have following tsql code in sql server 2008: declare @ID INT SET @ID
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.