Sunday, August 16, 2015

.Net Sound Timer

This tutorial show you how to set sound timer depend on the number of order. It plays sound for notification whenever the number of order change.
Sound Timer

Firstly, copy the following javascript to <head> tage.
<script type="text/javascript">
    function EvalSound(soundobj) {
        var thissound = document.getElementById(soundobj);
        thissound.play();
    }
</script>
Secondly, copy the following code to <form> tag.
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblOrder" runat="server" Text=""></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" />
    </Triggers>
</asp:UpdatePanel>
<audio id="audio1" src="sound/Console_beep.mp3" preload="auto" autobuffer>
</audio>
In code-behind, the following snippet should be there.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string dt = DateTime.Now.ToLongTimeString();
        string count = DAL.GetOrderCount();
        lblOrder.Text = string.Format("There are {0} records at {1}.", count, dt);
        Session["count"] = count;
        
    }
}
protected void Timer1_Tick(object sender, EventArgs e)
{
    string dt = DateTime.Now.ToLongTimeString();
    string count = DAL.GetOrderCount();
    if (count != Session["count"].ToString())
    {
        lblOrder.Text = string.Format("There are {0} records at {1}.", count, dt);
        ScriptManager.RegisterStartupScript(Page, GetType(), "PlaySound", "EvalSound('audio1');", true);
    }
    Session["count"] = count;
}
It uses "Northwind" database and connection string in web.config seem like below:
<connectionStrings>
<add name="sqlConn" providerName="System.Data.SqlClient" connectionString="Server='(local)';uid='sa';pwd='p@ssw0rd';Database='Northwind';Pooling=False;" />
</connectionStrings>
It has the following code snippet in the DAL class. It uses the Microsoft.Practices.EnterpriseLibrary.Data.dll to shorten the code.
private static Database getDatabase()
{
    Database database = null;
    try
    {
        database = DatabaseFactory.CreateDatabase("sqlConn");
    }
    catch (Exception exception)
    {
               
    }
    return database;
}
public static string GetOrderCount()
{
    string count = "";
    try
    {
        count = getDatabase().ExecuteScalar("GetOrderCount").ToString();
    }
    catch (Exception exception)
    {
        
    }
    return count;
}
The "GetOrderCount" stored procedure is as shown below:
USE [Northwind]
GO
ALTER PROCEDURE [dbo].[GetOrderCount]
AS
BEGIN
    SELECT COUNT(*) FROM Orders;
END
This application demonstrates a simple use of the Timer control with UpdatePanel combining HTML5 audio element in a real world example.

10 comments:

  1. Wonderful, just what a blog it is! This blog has provided the helpful data to us continue the good work.
    index

    ReplyDelete
  2. Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students. .Net Training in Chennai

    ReplyDelete
  3. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.

    java training in bangalore

    ReplyDelete
  4. Updated information about html.Thanks for sharing such a information.
    selenium training in chennai

    ReplyDelete
  5. It is really amazing...thanks for sharing....provide more useful information...

    Embedded training in chennai | Embedded training centre in chennai

    ReplyDelete
  6. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.

    Hadoop Training in Chennai

    ReplyDelete
  7. Thanks for making me this article. You have done a great job by sharing this content in here. Keep writing article like this.

    J2EE Training in Chennai
    JEE Training in Chennai

    ReplyDelete
  8. It is interesting to read your blog post and I am going to share it with my friends.aybabg

    ReplyDelete