Monday, December 21, 2015

Simple JavaScript Timer

Hi folks,

After long time I’m back, I’m really sorry about this two month delay. Because I’m really busy with my new job. By the way, today I’m here to share some simple, but little bit tricky JavaScript activity.
Develop a small JavaScript Timer, It’s a really easy thing. Actually, this may be easier than develop a simple “Hello World!” printing app.

setTimeout

This is the inbuilt method I use to develop this simple timer. Actually, this method gets two values as parameters. First, we need to pass a function name as a first parameter, then we need to pass a time in the millisecond format to mention execution time. In a simple word, setTimeout responsible for executing the passed function after giving time.

Here we go! This is the tricky point I use to build this simple web based timer, when page load I call the onload event to execute the ‘steamer’ function, this function responsible for print hours, minutes, seconds & milliseconds. After that recursively call it again after 1 millisecond its own function again. At that time these numeric values go thought some conditions for increase those values according to corresponding time format.
Additionally, use ‘twoDigitConverter’ function use for converting numeric values into two digit value.


That’s it, is that hard, actually this is what I’m always believe in this field, ‘Software industry depends on concepts & innovation thinking, instead of technology or resources’ isn’t it……..

JavaScript code jegment 
<script> var hour = 0; var minute = 0; var seconds = 0; var milliseconds = 0; function setTimer()  { if(milliseconds == 99) { milliseconds =0 if(seconds == 59) { seconds =0 if(minute == 59) { minute =0 hour++; }else { minute++; } }else { seconds++; } }else { milliseconds++; } document.getElementById('hour').innerHTML = twoDigitConverter(hour); document.getElementById('minute').innerHTML = twoDigitConverter(minute); document.getElementById('seconds').innerHTML = twoDigitConverter(seconds); document.getElementById('milliseconds').innerHTML = milliseconds; setTimeout("setTimer()", 10); } function twoDigitConverter(number) { var numberString = number+''; if(numberString.length == 1) { numberString = '0'+number; } return (numberString); </script>

CSS code segment
<style type="text/css"> body,td,th { color: #FFF; } .TimerTheme{ font-family: Segoe UI Light; background-color:#06F; font-size:36px; width:25%; text-align:center; } </style>

HTML code segment
<body onload="setTimer()">
<table width="100%" border="0">
  <tr>
    <td><table width="50%" border="0" align="center">
      <tr>
        <td class="TimerTheme" id="hour" />
        <td class="TimerTheme" id="minute" />
        <td class="TimerTheme" id="seconds" />
        <td class="TimerTheme" id="milliseconds" />
      </tr>
    </table></td>
  </tr>
</table>

</body>

Final Result
[Sample Code/Recourse]

Regards,
Denuwan Himanga.

Tuesday, September 1, 2015

LINQ (Language Integrated Query)



Hi guys,

The revolution of the computer programming technology rapidly change, Industry leaders such as Microsoft, Adobe try to add some values to their product in each releases. Within large amount of various product Microsoft own “C#” do an incredible revolution change in computer program developing history. They try to build universal language as well as universal IDE instead of different IDEs and different programing languages.   
According to their huge vision, Microsoft introduce Language Integrated Query with their amazing IDE called Visual Studio in 2008. Basically LINQ provide power full language capability to C#. Very simple explanation is, you can use only one programing language instead of SQL, XML etc.


In this demo, I’ll explain how to retrieve a data from SQL data base without using SQL query. LINQ consider table as a class, as well as data tuple as an object. That feature make computer programing technology to an amazing place J

Demo (Retrieving data from SQL database to WPF Application)    

Create a windows presentation foundation (WPF) application.



After that drag and drop a ‘list box’ from tool box, then formatted that list box according to our SQL table. In my case, Vehicle table formatted like this.


Using XAML code, we can easily format list box according to our requirement. Following XAML code segment format list box according to Vehicle table.

<ListBox x:Name="vehicleListBox" ItemsSource="{Binding}" Margin="0,40,0,0" >

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <Grid>

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition/>

                            <ColumnDefinition/>

                            <ColumnDefinition/>

                        </Grid.ColumnDefinitions>

                        <TextBlock Text="{Binding Vehicle_Name}" Grid.Column="0" Width="150"  FontSize="18" />

                        <TextBlock Text="{Binding Vehicle_Price}" Grid.Column="1" Width="150" FontSize="18"/>

                        <Image Source="{Binding Vehicle_Image}" Grid.Column="2" Width="150"/>

                    </Grid>

                </DataTemplate>

            </ListBox.ItemTemplate>

</ListBox>

Now we need to build a connection between SQL server and application, traditionally connection string responsibility for do this part. But in LINQ, add a .dbml Data classes and call when we need.
Add a .dbml data class to the project.


Add tables on to design surface.



After that, need to create a data classes data context object inside the application.

DataClasses1DataContext DB = new DataClasses1DataContext();

Finally we can easily call a values into special data type call ‘var’, this data type formatted according to values, which is assign to the variable. 

var result = from dt in DB.Vehicles
                         select dt;

vehicleListBox.ItemsSource = result.ToList<Vehicle>();
  
Source code/Project Repo.

Happy coding....

Best regards,
Denuwan Himanga.