Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

SQL Dependency - Not Firing data to datagridview/listview after inserting data in the database (Sql server 2012)

$
0
0

Need Help Guys, I've watched and implement the tutorial cause I'm fascinated how sql dependency works, and i think it's good to use when you want to fetch data after inserting, modifying or deleting in the database instead of using the refresh() that is inside the timer_Tick , though timer is still perfectly valid to use. But if you use timer, the Listview/Datagridview will disappear the focus when you select an item inside of it and the flickering of both views occurs also when it's refreshing, so the best solution is to implement the sql dependency. 

So this is the code i've been doing, but after inserting data in the database while the form is open with the datagridview nothing happens, no data fires back to datagridview. So I'm hoping that you can help me guys ! Thank you so much!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Security.Permissions;

namespace SQLDependency
{
    public partial class Form1 : Form
    {
        public string m_connect = @"Data Source=DESKTOP-3B561M1\SQLEXPRESS; Initial Catalog=Users;Integrated Security=True";
        SqlConnection con = null;
        public delegate void NewHome();
        public event NewHome OnNewHome;
        public Form1()
        {
            InitializeComponent();
            try
            {
                SqlClientPermission ss = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted);
                ss.Demand();
            }
            catch(Exception)
            {
                throw;
            }
            SqlDependency.Stop(m_connect);
            SqlDependency.Start(m_connect);
            con = new SqlConnection(m_connect);

        }

       public void Form1_OnNewHome()
        {
            ISynchronizeInvoke i = (ISynchronizeInvoke)this;
            if(i.InvokeRequired)
            {
                NewHome dd = new NewHome(Form1_OnNewHome);
                i.BeginInvoke(dd, null);
                return;
            }
            LoadData();
        }


        void LoadData()
        {
            DataTable dt = new DataTable();
            if(con.State==ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("SELECT FirstName, LastName from dbo.Uss", con);
            cmd.Notification = null;

            SqlDependency de = new SqlDependency(cmd);
            de.OnChange += new OnChangeEventHandler(de_OnChange);

            dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
            dataGridView1.DataSource = dt;
        }

        public void de_OnChange(object sender, SqlNotificationEventArgs e)
        {
            SqlDependency de = sender as SqlDependency;
            de.OnChange -= de_OnChange;
            if(OnNewHome != null)
            {
                OnNewHome();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OnNewHome += new NewHome(Form1_OnNewHome);

            LoadData();
        }

      
    }
}




Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>