Click here to Skip to main content
16,018,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm using SQLDependency to catch changes in my database.
http://code.msdn.microsoft.com/How-to-use-SqlDependency-5c0da0b3

This code really works. When I'm changing something in table, event is fired.

C#
private void RefreshWithSqlDependency()
 {
     iquery = from order in context.Order
              where order.Client.Nickname==nickName && order.StatusId!=5
              select new { Id = order.Id, Description = order.Description, OrderStatus = order.Status.Name };

     notification = new ImmediateNotificationRegister<Order>(context, iquery);
     notification.OnChanged += NotificationOnChanged;
 }

 protected void NotificationOnChanged(object sender, EventArgs e)
 {
     BindOrderDataList(); //this is executed
 }


Inside BindOrderDataList method, I'm setting datasource of my datalist.

C#
private void BindOrderDataList()
{
    DataListOrders.DataSource = context.Order.Where(x => x.Client.Nickname == nickName && x.StatusId != 5)
        .Select(x => new { Id = x.Id, Description = x.Description, OrderStatus = x.Status.Name }).ToList();
    DataListOrders.DataBind();
}


Of course nothing happened. Then I put Datalist inside UpdatePanel.

XML
<asp:UpdatePanel ID="UpdatePanelOrdersList" runat="server" UpdateMode="Conditional" OnLoad="UpdatePanelOrdersList_Load">
            <ContentTemplate>

                <asp:DataList ID="DataListOrders" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" OnItemDataBound="DataListOrders_ItemDataBound"
                    CellPadding="5" Width="100%" OnItemCommand="DataListOrders_ItemCommand" EnableViewState="False">
                    <ItemStyle Wrap="True" HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle>
                    <ItemTemplate>

                        <asp:Panel ID="Order" runat="server">
                            <div style="padding: 3px; border: 3px solid; border-color: #F0F0F0">
                                <h4>Order
                                <asp:Label ID="OrderId" runat="server" Text='<%# Eval("Id") %>'></asp:Label></h4>
                                <h5>Desc:
                                <asp:Label ID="Description" runat="server" Text='<%# Eval("Description") %>'></asp:Label></h5>
                                <h5>Status:
                                <asp:Label ID="OrderStatus" runat="server" Text='<%# Eval("OrderStatus") %>'></asp:Label></h5>
                                <br />
                                <asp:DataList ID="DataListOrderProducts" runat="server" RepeatDirection="Vertical" RepeatColumns="1"
                                    OnItemDataBound="DataListOrderProducts_ItemDataBound" OnItemCommand="DataListOrderProducts_ItemCommand"
                                    EnableViewState="False">
                                    <ItemStyle Wrap="True"></ItemStyle>
                                    <ItemTemplate>
                                        <asp:Panel ID="OrderItem" runat="server">
                                            <h6>Product:
                                            <asp:Label ID="OrderProductId" runat="server" Text='<%# Eval("LineId") %>' CssClass="hiddencol"></asp:Label></h6>
                                            <h6>Product:
                                            <asp:Label ID="Product" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label></h6>
                                            <h6>Status:
                                            <asp:Label ID="ProductStatus" runat="server" Text='<%# Eval("ProductStatus") %>'></asp:Label></h6>
                                            <asp:LinkButton ID="ItemSubmit" runat="server" CommandName="ItemCompleted" Text="Complete" Visible="False" />
                                        </asp:Panel>
                                    </ItemTemplate>
                                </asp:DataList>

                                <asp:LinkButton ID="OrderSubmit" runat="server" CommandName="OrderCompleted" Text="Complete" Visible="false" />
                            </div>
                        </asp:Panel>

                    </ItemTemplate>
                </asp:DataList>

                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="NotificationOnChanged" />

            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="DataListOrders" EventName="DataBinding" />
                <asp:AsyncPostBackTrigger ControlID="DataListOrders" />
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>


When I'm clicking this Button1 same method as in RefreshWithSqlDependency is executed (NotificationOnChanged) and this is working...
Is there any event that would force UpdatePanel to update when content in DataList changes?

I already tried delegate to raise update event in UpdatePanel...

C#
protected void NotificationOnChanged(object sender, EventArgs e)
    {
        BindOrderDataList();

        LongTimeTask_Delegate d = new LongTimeTask_Delegate(LongTimeTask);
        IAsyncResult r = d.BeginInvoke("String", new AsyncCallback(TaskCompleted), null);
        d.EndInvoke(r);
    }

    public delegate void LongTimeTask_Delegate(string str);
    public void LongTimeTask(string str)
    {
        Thread.Sleep(50);
    }

    public void TaskCompleted(IAsyncResult r)
    {
        UpdatePanelOrdersList.Update();
    }


but no success... I don't want to use timer or anything like that. This need to be updated on database change method - RefreshWithSqlDependency.
Posted

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