Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to run a sqldependency method from a web form. I created "UpdateMethod", from a window-form.
However the web-form version, does not update the page-load automatically on the client-side, when there is a change in the database, whereas window-form does.

Window form method version:
C#
delegate void GridDelegate(DataTable table);      
private void UpdateGrid()
        {
            string sql = "SELECT * FROM [dbo].[User]";
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    dep = new SqlDependency(cmd); //Passing Command to SQL dependency 
                    dep.OnChange += dep_OnChange;
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        dt.Load(rdr);
                    }
                }
            }
            dataGridView1.Invoke((GridDelegate)delegate(DataTable table)
            { dataGridView1.DataSource = table; }, dt);
        }



/////// web form ////////////
Web-form method version:
C#
private void UpdateGrid()
        {
            string sql = "SELECT * FROM [dbo].[User] order by uploadDate desc";
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(GetConnectionString()))
            {
                
                try 
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        SqlDependency dep;
                        dep = new SqlDependency(cmd); //Passing Command to SQL dependency 
                        dep.OnChange += dep_OnChange;

                        using (SqlDataReader rdr = cmd.ExecuteReader())
                        {
                            dt.Load(rdr);
                            // GridView1.DataSource = rdr;
                        }

                    }

                }

                catch (Exception ex)
                {
                    throw ex;
                }
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }


Am I missing something from my web-form method. Please advice. Many thanks.
Posted
Updated 9-Oct-14 18:14pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900