473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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)
{
CanRequestNotifications();
NotificationListener();

}
private bool CanRequestNotifications()
{
SqlClientPermission permit = new
SqlClientPermission(System.Security.Permissions.Pe rmissionState.Unrestricted);
try
{
permit.Demand();
return true;
}
catch (System.Exception exc)
{
return false;
}
}

private void NotificationListener()
{
string mailSQL;
SqlConnection sqlConn;
try
{
this.connectionString =
ConfigurationManager.AppSettings["connectionString"];
mailSQL = "select * from Cast_Member where isMajor =
'1'";

SqlDependency.Stop(this.connectionString);
SqlDependency.Start(this.connectionString);

sqlConn = new SqlConnection(this.connectionString);
sqlCmd = new SqlCommand(mailSQL, sqlConn);
this.GetNotificationData();
}
catch (Exception e)
{
// handle exception
}
}

private void GetNotificationData()
{
DataSet myDataSet = new DataSet();
sqlCmd.Notification = null;

dependency = new SqlDependency(sqlCmd);
dependency.OnChange += new
OnChangeEventHandler(dependency_OnChange);
using (SqlDataAdapter adapter = new
SqlDataAdapter(sqlCmd))
{
adapter.Fill(myDataSet, "CAST");
DataTable mailLogTbl = (DataTable)myDataSet.Tables[0];

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

}
}
private void dependency_OnChange(object sender,
SqlNotificationEventArgs e)
{

SqlDependency dependency = (SqlDependency)sender;
dependency.OnChange -= dependency_OnChange;
this.GetNotificationData();
}
protected override void OnStop()
{
dependency.OnChange -= dependency_OnChange;
SqlDependency.Stop(this.connectionString);
}
------------------------------------------------------------------------------------------------------------------------------------------------

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 2841

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

Similar topics

0
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
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...
2
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...
2
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...
5
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)...
5
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...
0
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 /...
2
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 /...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.