Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2005

Tip: Looking for answers? Try searching our database.

SqlDependency in WinFormsApp

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stefan - 29 Sep 2005 19:34 GMT
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!");
        }
    }
}
"Jeffrey Tan[MSFT]" - 30 Sep 2005 07:27 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Kevin Yu [MSFT] - 30 Sep 2005 09:07 GMT
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
Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.