2

I have a method initGrdAvail() that gets the latest info for a gridview from a MySQL database and updates the gridview

    private void initGrdAvail()
    {
        string selectedDept = drpLstDepts.SelectedItem.Text;
        string query;
        if (selectedDept == "ALL")
            query = "SELECT unitnum,department,rank,name FROM units WHERE status='Available'";
        else
            query = "SELECT unitnum,department,rank,name FROM units WHERE status='Available' AND department='"+selectedDept+"'";
        using (MySqlConnection con = new MySqlConnection(cnnStrData))
        {
            using (MySqlCommand cmd = new MySqlCommand(query))
            {
                cmd.Connection = con;
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                grdViewDisAvailable.DataSource = ds;
                grdViewDisAvailable.DataBind();
            }
        }
    }

This method works fine when calling it on a button click event, or a selected index changed event on a drop down list.

Here is where it's called from SignalR:

        HubProxyRegister.On<string, string>("UpdateUnitStatus", (username, status) =>
        { initGrdAvail(); });

However, when calling this method from an incoming Signalr event it does not update the gridview. I can see that this method is being called and ran fully through debugging, but it only displays the updated gridview on the page when the method is called from some sort of button event.

I am fairly new to ASP.NET, does this have something to do with Signalr not using a postback or something? I've tried searching around a bit but haven't had much luck yet.

Thanks!

Brian S
  • 385
  • 3
  • 16
  • Can you post the bit where you bind this method to the SignalR event? I'm not familiar with SignalR, but the method you're using might not have access to the UI elements. – David C Feb 28 '18 at 22:26
  • @DavidC799 Yes, sorry I forgot to post that part. I've update the post with where it's called by SignalR. – Brian S Feb 28 '18 at 22:33
  • So does loadDispatchViews() call initGrdAvail()? I think it will be something to do with the fact that the callback for `HubProxyRegister.On("UpdateUnitStatus", (username, status) => { loadDispatchViews(); });` is a Task and will run on a separate thread. I would have thought there would be an error though! – David C Feb 28 '18 at 22:44
  • https://msdn.microsoft.com/en-us/library/dn433672(v=vs.118).aspx – David C Feb 28 '18 at 22:45
  • Ah yes, loadDispatchViews() just calls initGrdAvail(), initGrdBusy), initGrdUnavail(). I think I understand what you're saying. Looking at the link you gave me, I'm a bit confused on how to use it. If you don't mind, how would I format this? – Brian S Feb 28 '18 at 22:51
  • public static IDisposable On(this IHubProxy HubProxyRegister, "UpdateUnitStatus" , Action loadDispatchViews); Not fully understanding the syntax @DavidC799 – Brian S Feb 28 '18 at 22:55
  • I tried changing the Signalr event call just initGrdAvail() instead of loadDispatchViews() that calls the initGrdAvail() and other methods, still no luck. It fully runs the method, but doesn't display the new data on the page. – Brian S Feb 28 '18 at 23:06
  • Read first https://stackoverflow.com/questions/18143599/can-signalr-be-used-with-asp-net-webforms befor you ask additional questons – Stephu Mar 02 '18 at 07:47

0 Answers0