473,602 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlDependency Event Handler Getting Fired continously - SQL Notification Service

das
Hello all,
I am using the SqlDependency to subscribe to any new inserts into
a database table, I enabled the DB to be borker ready and subscrbed to
Query notifications on the database.

My C# Windows service has a simple query that checks if a new entry is
made into a table,
select * from Cast_Member where isMajor = '1'

When I insert a new record into this table with isMajor = 1, then the
event handler gets fired, but doesn't stop there, it continously gets
fired on the same record. Also if I already have a OLD record in the
table with the isMajor = 1, then the event handler gets fired as soon
as I start the windows service - repeatedly.

Here's the code,
------------------------------------------------------------------------------------------------------------------------------------------------

protected override void OnStart(string[] args)
{
CanRequestNotif ications();
NotificationLis tener();

}
private bool CanRequestNotif ications()
{
SqlClientPermis sion permit = new
SqlClientPermis sion(System.Sec urity.Permissio ns.PermissionSt ate.Unrestricte d);
try
{
permit.Demand() ;
return true;
}
catch (System.Excepti on exc)
{
return false;
}
}

private void NotificationLis tener()
{
string mailSQL;
SqlConnection sqlConn;
try
{
this.connection String =
ConfigurationMa nager.AppSettin gs["connectionStri ng"];
mailSQL = "select * from Cast_Member where isMajor =
'1'";

SqlDependency.S top(this.connec tionString);
SqlDependency.S tart(this.conne ctionString);

sqlConn = new SqlConnection(t his.connectionS tring);
sqlCmd = new SqlCommand(mail SQL, sqlConn);
this.GetNotific ationData();
}
catch (Exception e)
{
// handle exception
}
}

private void GetNotification Data()
{
DataSet myDataSet = new DataSet();
sqlCmd.Notifica tion = null;

dependency = new SqlDependency(s qlCmd);
dependency.OnCh ange += new
OnChangeEventHa ndler(dependenc y_OnChange);
using (SqlDataAdapter adapter = new
SqlDataAdapter( sqlCmd))
{
adapter.Fill(my DataSet, "CAST");
DataTable mailLogTbl = (DataTable)myDa taSet.Tables[0];

foreach (DataRow dataRow in mailLogTbl.Rows )
{
int castID = (int)dataRow[0];
logFile.WriteLi ne("New Entry into CAST_MEMBER table: " +
castID.ToString ());
}

}
}
private void dependency_OnCh ange(object sender,
SqlNotification EventArgs e)
{

SqlDependency dependency = (SqlDependency) sender;
dependency.OnCh ange -= dependency_OnCh ange;
this.GetNotific ationData();
}
protected override void OnStop()
{
dependency.OnCh ange -= dependency_OnCh ange;
SqlDependency.S top(this.connec tionString);
}
------------------------------------------------------------------------------------------------------------------------------------------------

If anyone sees any problems with this code please point it out. As you
can see for every new entry into the table I write it into a log file,
so my log file has the same exact information multiple times for just
one new inserted record into the table.

thanks in advance. (btw, I copied the above code from MSDN, so its not
mine except for writing it to log)

Jul 30 '07 #1
0 2868

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7030
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
18
2873
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
2
6690
by: Stefan | last post by:
Hey all, I tried to use SqlDependency in WinFormsApplication to show up with new information upon a change in a SqlResult. The Problem I have is that the OnChangeEvent is fired when I create the SqlDependency, but not again afterwards. Any idea on that?
2
3366
by: Deepesh | last post by:
Good day, I have a specific case of the DataGrid in my solution which is causing the ItemCommand Event Not Firing. So I'm creating a "Skinnable" set of controls. I seperate the actual ASCX file and .CS file. When I initialize my .CS file, in that code there is a method that goes: Page.LoadControl(FILENAME) Which associates a .ascx file with my .CS file, allowing me to plug in any
5
1888
by: Richard Grant | last post by:
Hi, I need to "save" in a variable the event handler sub of a control's event, then perform some process, and finally "restore" the originally saved event handler. Example in pseudo-code: 1) Save cmbMyCombo's event handler for SelectedIndexChange event. 2) Assign a temporary event handler sub to cmbMyCombo's for its SelectedIndexChange event.
5
2373
by: ShaunO | last post by:
BACKGROUND I have the following classes as part of a program that opens 3 Asynchronous Sockets. Each socket is in a separate instance of a wrapping class. 1x User Interface class 1x Client Manager class 3x Client class Instances The Client class raises events when the client connects. The User interface has registered with that event.
0
1591
by: erbilkonuk | last post by:
Hi, I am very new to .NET Remoting and I try to run a simple program to subscribe to an event raised by Remoting Class. The Remoting Server initiates an instance of Remoting Class as Singleton / Server activated mode on startup. The Remoting Client accesses the Remoting Class through the interface of the Class and subscribes to an event of the Remoting Class that will be fired upon the private member value change. The problem is that I...
2
3050
by: erbilkonuk | last post by:
Hi, I am very new to .NET Remoting and I try to run a simple program to subscribe to an event raised by Remoting Class. The Remoting Server initiates an instance of Remoting Class as Singleton / Server activated mode on startup. The Remoting Client accesses the Remoting Class through the interface of the Class and subscribes to an event of the Remoting Class that will be fired upon the private member value change.
1
1767
by: jan.loucka | last post by:
I'm developing WinForms application in .NET 2.0 that talks to web service. The automatically generated proxy (reference class) has got methods for both synchronous and asynchronous invocations of the web service methods. What I like about the asynchronous calls that they use events. So when I call the web service operation is finished is fires the event. So all my objects that I'm using in my client application just subscribe to whatever...
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.