SqlDependency in WinFormsApp

S

Stefan

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?



Source:

namespace sql_dependency
{
public partial class Form1 : Form
{
public System.Data.SqlClient.SqlConnection SqlConn;
public Form1()
{
InitializeComponent();

System.Data.SqlClient.SqlDependency.Start([serverconnectionstring]);
SqlConn = new
System.Data.SqlClient.SqlConnection([serverconnectionstring]);
}

private void SqlSelect_TextChanged(object sender, EventArgs e)
{
if(SqlSelect.Text == String.Empty)
DepCheckActivateBtn.Enabled = false;
else
DepCheckActivateBtn.Enabled = true;
}

private void DepCheckActivateBtn_Click(object sender, EventArgs e)
{
// Assume c is an open SqlConnection.
SqlConn.Open();
// Create a new SqlCommand object.
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand("SELECT * FROM MyTestTable", SqlConn);
// Create a dependency and associate it with the SqlCommand..
System.Data.SqlClient.SqlDependency dep = new
System.Data.SqlClient.SqlDependency(cmd);
// Maintain the refence in a class member.
// Subscribe to the SqlDependency event.
dep.OnChange += new
System.Data.SqlClient.OnChangeEventHandler(SqlResultChanged);
// Execute the command.
cmd.ExecuteReader();
// Process the DataReader.
}

private void SqlResultChanged(object sender, EventArgs e)
{
MessageBox.Show("Result changed!");
}
}
}
 
J

Jeffrey Tan[MSFT]

Hi Stefan,

It seems that this is a Whidbey issue. Currently, Whidbey is still in beta
version, and Whidbey issues are not in support boundary. I suggest you post
such issue in:
http://forums.microsoft.com/msdn/

This forum is on Whidbey discussion, and you will get much more useful
replies.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
K

Kevin Yu [MSFT]

Hi Stefan,

Thanks for posting your code here. The code seems to be fine. Do you mean
that the OnChange event fired once when the SqlDependency was created and
then never fired again? Please try to remove the following line to have a
try. Because SqlDependecy.Start starts monitoring the the dependency
changes on the connection string, not the command result set.

System.Data.SqlClient.SqlDependency.Start([serverconnectionstring]);

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top