Friday, June 28, 2013

C++ Win32 Smart Device project creation error in VS2008


Reason:
Settings conflict with IE. more info in the link below
http://blogs.msdn.com/b/vcblog/archive/2009/03/28/some-vs2005-and-vs2008-wizards-pop-up-script-error.aspx

Solution:

  1. Open Regedit
  2. Under “HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones”, create a new key called 1000 (if it isn't already there)
  3. Under 1000, create a DWORD entry with:

    •    Name = 1207
    •    Type = REG_DWORD
    •    Data = 0x000000




Wednesday, June 26, 2013

Change System date time of windows Mobile

This class will change the system date time  of windows Mobile.
scenario:
sync your mobile time with centralized server.

class TimeSyncer
    {
        [DllImport("coredll.dll")]
        private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);


        private struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

        private DateTime GetDateTime(DateTime currentDateTime)
        {
            double hourOffsetValue;

            // *** Converting time to GMT
            //gmt from india is -5.5 hrs
            hourOffsetValue = -5.5;
            currentDateTime = currentDateTime.AddHours(
            hourOffsetValue);

            return currentDateTime;
        }

        public void SetSystemDateTime(DateTime serverDateTime)
        {
            try
            {
                SYSTEMTIME st = new SYSTEMTIME();

                DateTime updatedDateTime = GetDateTime(
                 serverDateTime);

                st.wYear = (ushort)updatedDateTime.Year;
                st.wMonth = (ushort)updatedDateTime.Month;
                st.wDay = (ushort)updatedDateTime.Day;
                st.wHour = (ushort)updatedDateTime.Hour;
                st.wMinute = (ushort)updatedDateTime.Minute;
                st.wSecond = (ushort)updatedDateTime.Second;

                uint nReturn = SetSystemTime(ref st);


            }
            catch (Exception ex)
            {
                throw;
            }

        }
    }


Friday, June 14, 2013

Getting only IPv4 address in c#

 private string GetMyIP()
        {
            IPHostEntry hostentry = Dns.GetHostEntry(Dns.GetHostName());
            if (hostentry != null)
            {
                IPAddress[] collectionOfIPs = hostentry.AddressList;

                foreach (IPAddress address in collectionOfIPs)
                {
                    if (address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                        continue;
                    if (IPAddress.IsLoopback(address))
                        continue;

                    return address.ToString();
                }

                return null;
            }
            else
                return null;
        }


Allow only numbers and decimal in a textbox

This piece of code will allow users to enter only numbers and one decimal point (.) in a textbox
private void txtMCurReading_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar))
            {
                //
            }
            else if (char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
            else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
            {
                TextBox tb = sender as TextBox;
                int cursorPosLeft = tb.SelectionStart;
                int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
                string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
                string[] parts = result.Split('.');
                if (parts.Length > 1)
                {
                    if (parts[1].Length > 2 || parts.Length > 2) // change here for setting number of chars after decimal

                    {
                        e.Handled = true;
                    }
                }
                if (parts[0].Length > 10) // change here for setting number of chars before decimal
                {
                    e.Handled = true;
                }
            }
            else if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }

        }

SQL server compact edition : "The application is trying to load native components of version 5386 that are incompatible with ADO.NET provider of version 8080." error

Cause:
This error occurs when SQL server compact edition 3.5 is upgraded to SP2.

Altough the 3.5 SP2 files are installed on the desktop, the files deployed on the CE devices are still the old ones.

Solution:
After upgrade Goto Path: C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices\wce500\armv4i
copy Cabs:
1. sqlce.ppc.wce5.armv4i.CAB
2. sqlce.repl.ppc.wce5.armv4i.CAB
3. sqlce.dev.ENU.ppc.wce5.armv4i.CAB
to your device and install them. This will replace the SQL CE 3.5  with 3.5 SP2

Reason for the err:
Installing SQL CE 3.5 will install 3.5.0.0 version of System.Data.SqlServerCe.dll whereas 3.5 SP2 will install 3.5.1.0 version of System.Data.SqlServerCe.dll