Event (OnChange) of (SqlDependency) object occur like the timers , not only on the change appear

Hi

I have Use this Code snippet to apply the concept of notification in SQL Server 2005 Developer Edition April CTP and Also I use VS.NET 2005 Beta2

But the problem the Event (OnChange) of (SqlDependency) object occur like the timers hundreds time not only on the change appear in the database … Why ? and How can stop this event to occur only when the change in the database ?

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsApplication1

{

publicpartialclassForm1 :Form

{

privateint ChangeCount = 0;

privateDataSet dataSet =newDataSet();

privatedelegatevoidUICallback();

public Form1()

{

InitializeComponent();

}

privatevoid btnGetData_Click(object sender,EventArgs e)

{

GetData();

this.ChangeCount = 0;

}

privatevoid GetData()

{

this.dataSet.Clear();

string strConnection =@"Data Source=server\SQL2005;Integrated Security=True;Initial Catalog=FraasDB";

SqlConnection SQLConnection =newSqlConnection(strConnection);

string SQL ="SELECT * FROM Table_1";

SqlDataAdapter SQLDataAdapter =newSqlDataAdapter(SQL, SQLConnection);

SqlDependency SQLDependency =newSqlDependency(SQLDataAdapter.SelectCommand);

SQLDependency.OnChange +=newOnChangeEventHandler(this.SQLDependency_OnChange);

SQLDataAdapter.Fill(dataSet,"Table1");

this.dataGridView1.DataSource = dataSet;

this.dataGridView1.DataMember ="Table1";

}

void SQLDependency_OnChange(object sender,SqlNotificationEventArgs e)

{

this.ChangeCount++;

this.Invoke(newUICallback(ReBind));

MessageBox.Show(e.Info.ToString());

}

privatevoid ReBind()

{

GetData();

this.toolStripStatusLabel1.Text =this.ChangeCount.ToString();

}

}

}

And also when I try to get information about the changes type with the help of this line of code Invalid value

void SQLDependency_OnChange(object sender,SqlNotificationEventArgs e)

{

MessageBox.Show(e.Info.ToString());

}


And thanks with my best regarding

[25560 byte] By [Fraas] at [2008-2-2]
# 1
Your command statement is wrong. Under the covers, the SqlDependency relies on the same functionality as when you use indexed (materialized) views, which means that they have the same syntactical requirements etc. One of the requirements for the select statement is that you use two-part object names and you are not allowed to use "select * ... ". So your command text should look something like:

Select t.col1, t.col2, t.col3 from Table_1 t

On tell-tale sign that the statement is wrong is that the callback happens immediately after the command hes been executed.
For more info, you can read some stuff I wrote on my blog a while back.
Hope this helps!
Niels
nielsb at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
Hi nielsb thanks for your reply

I have change the SQL Statement but still I get The Seme result


SELECT t.F1, t.F2 FROM Table_1 t

With my regarding

Fraas

Fraas at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3
Yes .. The Problem In SQL Statements ..

The SQL Statement Must be


SELECT F1, F2 FROM dbo.Table_1

Now it's work fine

thanks

Fraas at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified