Saturday, February 26, 2011

Simple Tips for Healthy Living for Software Engineers

Simple tips for healthy living for Software Engineers

This tips are specially for Software Engineers who usually sits for 8-10 hours a day in front of computers solving complex problems of world and have no time to take care of health. One can minimize ill effects of stress and stay healthy by following a few simple tips as below.

1. Get adequate sleep:

Remember that not getting enough sleep will do you harm in the long run. you will feel drowsy during the day, unable to concentrate on simple tasks and feel irritable. Try to get at least seven hours of sleep every day. Avoid keeping Televisions, Computer and gadgets in your bedroom, don’t eat large meals before bedtime, follow regular bedtimes and wake times. Ensure your bedroom is quite and relaxing.

2. Drink Hot Water:

While it may take a little while to get used to having hot water; it will greatly benefit you. It purifies your body and helps remove unwanted properties as well. Your digestive system will work smoothly and also clear out your skin.

3. Spend lesser time on Social Networking Sites:

Researchers say that an increasingly high number of people prefer to spend time surfing the internet and spending time on social networking sites instead of pursuing a hobby. Ask yourself how much time you spend on networking sites every week and whether it is worth it. Opt for a hobby, which will give you a chance to enjoy, learn something new and meet new people.

4. Exercise:

You don’t need to pump iron six days a week in a gym to stay in shape. A brisk walk for 30 minutes every day in your neighborhood will do you a whole lot of good. Do a few simple stretches and exercises every alternate day at home or alternatively start doing yoga and meditation. Walking, cycling, swimming or even dancing are great stress busters and also keep you in shape.

5. Quit Smoking:

If you have finally made up your mind to kick this habit, it’s really good for your health. To quit successfully you need encouragement and support from your doctor, family, friends, and coworkers. A doctor will help you tailor an approach that suits you best.

6. Follow a healthy diet:

Try eating a whole grain sandwich before your big meal, eat smaller portions of high calorie dishes, ask yourself if you’re an emotional eater, and avoid reaching out for snacks while watching TV. Include more vegetables and fruits in your daily diet. If you're trying to loose weight, don't starve yourself.

7. Blink your eyes at regular interval:

Follow the 20-20-20 rule. After every 20 minutes of work, look 20 feet away from the computer screen and blink 20 times. Also lubricate your eyes at regular interval to avoid dryness of eyes. Use lubricating eye drops in both the eyes before every session on the computer. Consult your eye surgeon before using any eye drops.


Wednesday, January 19, 2011

Multiple Active Result Sets (MARS) in SQL Server

Before few days, one of my colleague came with one issue - he was getting error while opening multiple DataReader with single Connection object i.e. he is trying to open 2nd datareader with same connection object in Loop. And he got error - There is already an open DataReader associated with this Command which must be closed first. I search and get way to open multiple DataReader connections with SQLServer and like to share with all.

Sql Server supports MARS (Multiple Active Result Sets) and we need to set MultipleActiveResultSets=true in connection string. Below is Connection string syntax and Code to demonstrate opening datareader in while loop (multiple datareader). MARS is supported from SQL Server 2005 Version.

Connection String:

<add name ="DBConnstr" connectionString ="Data Source=xxx.yyy.zzz.aa;Initial Catalog=name;User Id=id;Integrated Security=False;Password=pwd;MultipleActiveResultSets=true"  providerName ="System.Data.SqlClient" />

Code:

string strConn = ConfigurationManager.ConnectionStrings["DBConnstr"].ConnectionString;
string strSql = "select * From OrderDetail where OrderID = {0}";
string strOutput = "OrdNumber: {0} - Desc: {1}";

using (SqlConnection con = new SqlConnection(strConn))
{
 //Opening Connection   
 con.Open();

 //Creating two commands form current connection   
 SqlCommand cmd1 = con.CreateCommand();
 SqlCommand cmd2 = con.CreateCommand();

 //Set the comment type   
 cmd1.CommandType = CommandType.Text;
 cmd2.CommandType = CommandType.Text;

 //Setting the command text to first command   
 cmd1.CommandText = "select OrderID From [OrderMaster]";

 //Execute the first command   
 IDataReader idr1 = cmd1.ExecuteReader();
 while (idr1.Read())
 {
  //Read the first Tree value from data source       
  int intTree = Convert.ToInt32(idr1.GetInt16(0));

  //create another command, which get values based on Tree value       
  cmd2.CommandText = string.Format(strSql, intTree);

  //Execute the reader       
  IDataReader idr2 = cmd2.ExecuteReader(); // If MARS is not enable in Conn string ; error will come at this line.
  while (idr2.Read())
  {
   //Read the values from Reader 2
   Console.WriteLine(string.Format(strOutput, idr2.GetString(1), idr2.GetString(4)));
  }
  //Dont forgot to close second reader, this will just close reader not connection       
  idr2.Close();
 }
 idr1.Close();
}




Friday, November 12, 2010

DayLight Saving and DateTime in .Net

Today, while looking to solve one issue related to DataTime in .Net, I came across article from MSDN that describes the best practice recommendations for writing programs that use the DateTime type in Microsoft .NET-based applications and assemblies.

While going through article, I came across the Daylight saving time (DST) and its impact on Applications. So I decided to give brief about DST and will discuss how it effects the Programmers.

Daylight saving time:

Daylight saving time (DST)—also summer time in British English is the practice of temporarily advancing clocks during the summer time so that afternoons have more daylight and mornings have less. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn.

In a typical case where a one-hour shift occurs at 02:00 local time, in spring the clock jumps forward from 02:00 standard time to 03:00 DST and that day has 23 hours, whereas in autumn the clock jumps backward from 02:00 DST to 01:00 standard time, repeating that hour, and that day has 25 hours.

A digital display of local time does not read 02:00 exactly at the shift, but instead jumps from 01:59:59.9 either forward to 03:00:00.0 or backward to 01:00:00.0. Clock shifts are usually scheduled near a weekend midnight to lessen disruption to weekday schedules.

Advantages of DST:
  • Many countries observe DST, and many do not. The reason many countries implement DST is in hopes to make better use of the daylight in the evenings.
  • Some believe that it could be linked to reducing the amount of road accidents and injuries.
  • The extra hour of daylight in the evening is said to give children more social time with friends and family
  • DST can even boost the tourism industry because it increases the amount of outdoor activities.
  • Majority of people also believe that DST saves energy as it influences people to spend more time out of the house, thus decreasing the need for artificial lighting as well as the likelihood of using home electric appliances.

Disadvantages of DST:
  • Flight schedules and inaccurate transportation timetables have caused confusion among travelers, for both personal and business purposes, and regular commuters. The transport industry needed to take into account the costs for adjusting to new time schedules.
  • Other complaints about daylight saving include the safety fears in the dark mornings, especially for school children waiting for a bus in some areas. Those who went early to work or study, they would be leaving their homes in the dark, the time when crime was at its highest, putting them in potentially dangerous situations.
  • Farming groups have also expressed anti-daylight saving views, saying that daylight saving time had a significant adverse impact on rural families, businesses, and communities. There are also those who express health concerns linked with daylight saving time.

Daylight Saving Time and .Net:

Consider a case when on particular morning during DST, at 2:00 AM, the local clock is reset to 1:00 AM, creating a 25-hour day. Since all values of clock time between 1:00 AM and 2:00 AM occur twice on that particular morning—at least in most of the United states and Canada. The computer really has no way to know which 1:10 AM was meant—the one that occurs prior to the switch, or the one that occurs 10 minutes after the daylight savings time switch.

Similarly, programs have to deal with the problem that happens in the springtime when, on a particular morning, there is no such time as 2:10 AM. The reason is that at 2:00 on that particular morning, the time on local clocks suddenly changes to 3:00 AM. The entire 2:00 hour never happens on this 23-hour day.

Programs have to deal with these cases (Particularly applications that deals with Scheduler. Such as Marketing Emails to send during night hours or taking Backup during night), possibly by prompting the user when you detect the ambiguity. 

If you aren't collecting date-time strings from users and parsing them, then you probably don't have these issues. Programs that need to determine whether a particular time falls in daylight savings time can make use of the following:
    Timezone.CurrentTimeZone.IsDaylightSavingTime(DateTimeInstance)
or
DateTimeInstance.IsDaylightSavingTime
   
So, if you need to perform DateTime calculations (add/subtract) on values representing time zones that practice daylight savings time, unexpected calculation errors can result. Instead, convert the local time value to universal time, perform the calculation, and convert back to achieve maximum accuracy.

For the complete tips for dealing with DateTime, go through the below link from MSDN.

http://msdn.microsoft.com/en-us/library/ms973825.aspx

I believe that above link from MSDN is helpful to all the Programmers. If we take care of above points, our software are least prone to errors.


For further details on DST:
http://www.timeanddate.com/time/dst/

Thursday, October 14, 2010

Accessing Master page control values from child page

Accessing Master page control values from child page:

Today, I am discussing about very common requirement where we need to access value of Master page from Child page. We will consider a case where there is dropdownlist in Master page and on change of dropdownlist, we will use selected value of Dropdownlist in child page. Lets start by following below steps:

1. Create Master page and add dropdownlist on aspx page

<asp:DropDownList ID="ddlThemes" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlThemes_OnSelectedIndexChanged">
<asp:ListItem Text="Select Theme" Value="0" Selected="true"></asp:ListItem>
<asp:ListItem Text="Red" Value="Red"></asp:ListItem>
<asp:ListItem Text="Blue" Value="Blue"></asp:ListItem>
<asp:ListItem Text="Green" Value="Green"></asp:ListItem>
</asp:DropDownList>

2. We need to raise an event when dropdownlist is changed. For this create public event and raise event from dropdownlist selectedindex change event in masterpage.cs file

public event CommandEventHandler ThemeChanged;
protected void ddlThemes_OnSelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlThemes.SelectedIndex != 0 && ThemeChanged != null)
        ThemeChanged(this, new CommandEventArgs(ddlThemes.SelectedItem.Text, ddlThemes.SelectedValue));
}

3. Now create child page referring to above master page created. Add below code in aspx file:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPages/masterpage.master" CodeFile="ChildPage.aspx.cs" Inherits="ChildPage" %>
<%@ MasterType VirtualPath="~/MasterPages/masterpage.master" %>

4. Now write the code to handle the event (created in Master page) in child code behind page:
public partial class ChildPage : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        Master.ThemeChanged += new CommandEventHandler(ThemeChangedFromMasterPage);
    }
    private void ThemeChangedFromMasterPage(object sender, CommandEventArgs e)
    {
        string ThemeText = e.CommandName;
        string ThemeValue = e.CommandArgument.ToString();
        Response.Write(ThemeText + " " + ThemeValue);
    }
}

That's all. Above steps will also help you to understand concept of Events and Events Handlers in .Net.

Friday, October 8, 2010

Google Announcing Google TV: TV meets web. Web meets TV.

Google had launched its official website this week for Google TV (a new experience of enjoying TV and Web together). Check out all the features of Google TV here. Some of the features of Google TV are quite interesting i.e. multiple phones acting as remote control, voice search, watching and browsing simultaneously, recording of program and great home page for TV. I hope with introduction of Google TV, TV will no longer be 'idiot box' and will become an interactive way to do lot more things.

There are some questions arises with introduction of Google TV, i.e. Does Google TV will replace Personal Computers in near future? Will all websites owners do changes to make compatible with Google TV?

For the Software Professionals, Google TV is built on Android and comes with the Google Chrome web browser. If you are interested in learning more about how to optimize website for viewing on Google TV, please visit developer page.

Also, I think I need to wait for another year for Google TV to come in India. Hope Google will launch Google TV soon in India.
Also check out Official Google Blog Announcing Google TV.

Checkout Logitech Revue and other stuff to use with your existing TV:

1. Logitech Mini Controller for Logitech Revue and Google TV


2.Logitech Keyboard Controller for Logitech Revue and Google TV


3. Logitech Revue Companion Box with Google TV and Keyboard