A
A
Abdumalik09992018-09-12 07:26:04
ASP.NET
Abdumalik0999, 2018-09-12 07:26:04

SqlDependency not working for me in asp.net?

Hello everyone, I am creating a project on asp.net where I use SqlDependency to track changes in the database, but it does not work. here is the code

public List<Chat> getChat()
        {
            string Operator_ID = "1";
            SqlDataReader reader;
            List<Chat> ListChat = new List<Chat>();
            using (SqlConnection sql_connection=new SqlConnection(connString))
            {
                SqlCommand sql_command = new SqlCommand("Select ID as Chat_ID, Subject_ID as Subject, User_ID as Phone_Number, Private_Auth, Client_Name, Client_ID,(Select COUNT(*) from MIB_Message_Comment where Status_Read=0 AND Sender_User_ID=User_ID AND Chat_ID=ID)as  Count, Status from MIB_Message_Chat where Online_Consulting=1 And Operator_ID=1", sql_connection);
                sql_command.Parameters.Add(new SqlParameter("Operator_Id", Operator_ID));
                sql_command.Notification = null;
                SqlDependency dependency = new SqlDependency(sql_command);
                dependency.OnChange += new OnChangeEventHandler(OnChange_Chat);
                if (sql_connection.State==ConnectionState.Closed)
                {
                    sql_connection.Open();
                }
                reader = sql_command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Chat chat = new Chat();
                        chat.Phone_Number = reader["Phone_Number"].ToString();
                        chat.Num_Chat = reader["Chat_ID"].ToString();
                        chat.Private_Auth = reader["Private_Auth"].ToString();
                        chat.Status = reader["Status"].ToString();
                        chat.Subject = reader["Subject"].ToString();
                        ListChat.Add(chat);
                    }
                }
                return ListChat;
            }
        }
        void OnChange_Chat(Object sender, SqlNotificationEventArgs e)
        {
            index chat = new index();
            chat.UpdateListCHat();
        }

And this is a method for processing changes
public void UpdateListCHat()
        {
           
            var lisChat = chatData.getChat();
            foreach (var item in lisChat)
            {
                ListBox1.Items.Add(item.Num_Chat + " " + item.Phone_Number + " " + item.Subject);
            }
        }

I set the start for the connection line

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor P., 2018-09-12
@Jeer

Hi, did you do it in the database itself?
ALTER DATABASE [DB Name] SET ENABLE_BROKER

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question