Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

We have a problem with multiple user access. When two users clicking same button at same time, One is able to get data and other getting an error 'Invalid attempt to read when no data is present'.

C#
Inner exception :System.InvalidOperationException: Invalid attempt to read when no data is present. at getData(int id)at PopulatePage() at Page_Load(Object sender, EventArgs e) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Please find function getData below.

C#
private static SqlConnection _connection;
    public static SqlConnection Connection
    {
        get
        {
            if (_connection == null)
            {
                SecurityClass Secure = new SecurityClass();
                _connection = Secure.PublicConnection();
            }
            return _connection;
        }
    }

public static object getData(int id)
    {
        
        using (SqlCommand cmd = new SqlCommand("spGetdata", Connection))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ID", id);

            try
            {
                if (Connection.State != ConnectionState.Open)
                {
                    Connection.Open();
                }
                using (SqlDataReader sqlReader = cmd.ExecuteReader())
                {
                    sqlReader.Read(); 
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (Connection.State == ConnectionState.Open)
                {
                    Connection.Close();
                }
            }
        }
        return object;
    }
Please suggest a solution to this issue.
Thanks in advance.
Posted
Updated 22-Aug-12 3:18am
v2

1 solution

You have a race condition. You can't have multiple users on a single static connection, you get exactly the errors you are getting.
 
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