none
Connection_StateChange sender is null RRS feed

  • Question

  • Hi

    May I know in what condition the sender parameter of SqlConnection_StateChange event is NULL?

    Thank you

    Friday, May 27, 2011 4:14 AM

All replies

  • Hi chanmy8,

    Welcome!

    Let's  see the delegate:

    public delegate void StateChangeEventHandler(object sender, StateChangeEventArgs e);
    

    protected virtual void OnStateChange(StateChangeEventArgs stateChange)
    {
      StateChangeEventHandler handler = this._stateChangeEventHandler;
      if (handler != null) handler(this, stateChange);
    }
    
    

    You mean you want to know when the sender will be null? I think it depends on how do you call the Event, but by default(from the code), I don't think it will be null, when your connection add this event, you can see this key word, the connection will be sent to sender, you can get the connection by this way: (SqlConnection)sender. but you can pass null instead of this, the sender will be null, this just indicates the current instance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.




    Monday, May 30, 2011 7:13 AM
    Moderator
  • Hi chanmy,

     

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

     

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 2, 2011 7:32 AM
    Moderator
  • Hi

    I just bind to this event with +=. I also can't think of why sender is null.

    Thursday, June 2, 2011 7:36 AM
  • Hi chanmy8,

    Thanks for your feedback.

    Yeah, it willn't be null without set null to invoke this event. you mean your problem is your sender is null? would you please clarify it, thanks!

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 2, 2011 7:40 AM
    Moderator
  • Hi

    Yes

    Thursday, June 2, 2011 7:43 AM
  • Hi,

    Thanks!

    Would you please give me your code? it is strange thing, I'd like to help you.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 2, 2011 7:49 AM
    Moderator
  • I hit specified cast is invalid at DBConnection_StateChange. Therefore I suspect it is null. The DbConnection returned by base.CreateBlankDbConnection is actually SqlConnection. It doesn't hapen all the time. I suspect only during high volumn request or system crash may be

     

        public override DbConnection CreateBlankDbConnection()
        {
          DbConnection connection = base.CreateBlankDbConnection();
          connection.StateChange += new StateChangeEventHandler(DBConnection_StateChange);
    
          return connection;
        }
    
    
        private void DBConnection_StateChange(object sender, EventArgs e)
        {
          DbConnection conn = (DbConnection)sender;
    
          if (conn.State == ConnectionState.Open)
          {
            this.InitializeAuditLog(conn);
            this.OpenDatabaseEncryptionKey(conn);
          }
        }
    Thursday, June 2, 2011 8:00 AM
  • Hi chanmy8,

    Thanks for your code.

    If the sender is null, the DbConnection must be null, and the Event will be not invoked. the problem doesn't happen all the time, just as you said it may relate to high volumn request or system crash(database crash) or in multithread and some thread disposed the connection.

    BTW, the base.CreateBlankDbConnethion --->why let the parent class to create connection?

    You can set breakpoint before the DbConnection conn=(DbConnection)sender and debug and watch the type of sender.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, June 2, 2011 8:33 AM
    Moderator
  • Hi Chanmy8,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 6, 2011 2:26 AM
    Moderator