Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
First of all, this error does not appear while testing the application locally on visual studio. It only appears in the production. And most importantly "IT DOES NOT APPEAR EVERYTIME". If that error occurs and we re-run the application and repeat the same steps then the error won't occur again. It occurs randomly..once in a day.

Here is my stored procedure where i get the error:
SQL
"SET CONTEXT_INFO statement requires varbinary (128) NOT NULL parameter."


SQL
CREATE PROCEDURE [dbo].[SetATUser]
@loginName AS VARCHAR(100)
AS
BEGIN 
DECLARE @userId INT
DECLARE @ctx    VARBINARY(128)

SELECT @userId = UserId
FROM Users
WHERE LoginName = @loginName

SET @ctx = CAST(@userId AS VARBINARY(128))

SET CONTEXT_INFO @ctx
END


Now what i think is that the loginName that is passed might be blank ultimately fetching no rows for userId and giving this error.
That leads to finding the reason for getting a blank loginName.
The loginName is assigned the value in Application_PostAutheticate event of global.asax like this:
C#
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    AUSORD.DataAccess.Common.LoginName = HttpContext.Current.User.Identity.Name;
}

And this login name is further passed to SetAtUser SP written on top whenever any stored procedure is called.
C#
public static SqlConnection GetConnectionAT(string loginName)
{
    SqlConnection cnn = new SqlConnection(Common.ConnectionString);

    SqlCommand cmd;

    cmd = new SqlCommand();
    cmd.CommandText = "SetATUser";
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Connection = cnn;

    cmd.Parameters.AddWithValue("@loginName", loginName);
    cnn.Open();
    cmd.ExecuteNonQuery();

    return cnn;
}

Can't figure out what is causing the error. Need help. The problem is the error does not appear every time. It occurs randomly. On some days the application works fine.
Posted
Updated 16-Oct-14 5:13am
v2

1 solution

I think you should add some control statements in your code and throw exceptions and/or write to a log file if something is amiss.

You can do this in several places in order to narrow down where your error origins.

1. Check if the property AUSORD.DataAccess.Common.LoginName is changed anywhere else in the code.

2. In the method Application_PostAuthenticateRequest
C#
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
        throw new ArgumentNullException(); // Or what ever you want to do here

    AUSORD.DataAccess.Common.LoginName = HttpContext.Current.User.Identity.Name;
}


3. In the method GetConnectionAT
C#
public static SqlConnection GetConnectionAT(string loginName)
{
    if (String.IsNullOrEmpty(loginName))
        throw new ArgumentNullException(); // Or what ever you want to do here
}


4. In the stored procedure
SQL
CREATE PROCEDURE [dbo].[SetATUser]
@loginName AS VARCHAR(100)
AS
BEGIN 
    DECLARE @userId INT
    DECLARE @ctx    VARBINARY(128)
     
    IF (ISNULL(@loginName) OR @loginName = '') THEN
    -- Raise an exception for loginName
    END IF
        
    SELECT @userId = UserId
    FROM Users
    WHERE LoginName = @loginName

    IF (ISNULL(@userId) OR @userId = '') THEN
    -- Raise an exception for userId
    END IF
     
    SET @ctx = CAST(@userId AS VARBINARY(128))
     
    SET CONTEXT_INFO @ctx
END

(Very sorry for the MySQL syntax, but I'm most familiar with that.)

It might seem to be overkill to do checks in so many different places, but you might change the code later on and make calls from other parts of the code.

If you decide to write error info to a log file, I suggest that you add a time stamp and perhaps system information such as CPU load, memory usage and a list of active processes.
This info can be helpful to identify patterns, such as the error only occurs at a specific time of the day, or the memory starts to reach the maximum or a specific process is taking more CPU speed than normal, e.g. virus protection.

You don't say if you are running the same software on many computers.
If so it can be useful to check if the error occurs on all machines or just a few or maybe only one. In that case, what is the difference between a working and non working PC?
Do a list of both HW and SW to see if you can spot any potential culprit.

I know this is not a direct solution to your problem, but maybe you can find these tips useful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900