Tags: , , , , , , , | Categories: Genel Posted by okutbay on 30.03.2010 10:55 | Yorumlar (5)

Bazen iki tarihin arasında geçen süreyi bulmanız gerekir. Bu kişinin şu anki yaşı olabileceği gibi bir işçinin o gün çalıştığı süre de olabilir. C# bize bu konuda yardımcı olmak için TimeSpan tipini sunar. Bu tipi kullanarak iki tarih arasında geçen süreyi farklı şekillerde alabiliriz. Örneğin iki tarih arasında kaç saat olduğunu bulmak istiyorsak TotalHours özelliğini kullanabiliriz. Eğer iki tarih arasında geçen sürenin sadece saat kısmı bizi ilgilendiriyorsa Hours özelliğini kullanabiliriz.

    1 DateTime myStartTime = Convert.ToDateTime("30.03.2010 08:04:00");

    2 DateTime myEndTime = Convert.ToDateTime("30.03.2010 18:02:00");

    3 TimeSpan myWorkingTime = myEndTime - myStartTime;

    4 double myWorkingHours = myWorkingTime.TotalHours;

Bu işi uygulamanız içinde birden çok kullanacaksanız bir method haline getirmek faydalı olacaktır.

    1 public static double GetWorkingHours(DateTime startTime, DateTime endTime)

    2 {

    3     TimeSpan workingTime = endTime - startTime;

    4     double workingHours = workingTime.TotalHours;

    5     return workingHours;

    6 }

 

bu yeni metodu soyle kullanirsiniz.

    1 DateTime myStartTime = Convert.ToDateTime("30.03.2010 08:04:00");

    2 DateTime myEndTime = Convert.ToDateTime("30.03.2010 18:02:00");

    3 double myWorkingHours = GetWorkingHours(myStartTime, myEndTime);

 

Not:Methodun ve kodun aynı classta olduğu varsayılmıştır...

Kolay gelsin...

1 kişi tarafından 1.0 olarak değerlendirildi

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , | Categories: Windows Uygulaması Posted by okutbay on 03.03.2010 15:14 | Yorumlar (0)

You develop a windows service application and installed to your machine. When you try to start service you get this error.

image

Possible reason to this error is service name definitions.

Check the service name and service installer service name. Both must be the same. For instance this is from service class.

    1 private void InitializeComponent()

    2 {

    3     components = new System.ComponentModel.Container();

    4     this.ServiceName = "DataExporter";

    5 }

and this code is from service installer settings

    1 //

    2 // serviceInstallerDataExporter

    3 //

    4 this.serviceInstallerDataExporter.Description = "Exports Data for SPSS as CSV file";

    5 this.serviceInstallerDataExporter.DisplayName = "x Data Exporter";

    6 this.serviceInstallerDataExporter.ServiceName = "x Data Exporter";

    7 this.serviceInstallerDataExporter.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

P.S.: If you have multiple service instances you must be more careful

Happy coding...

1 kişi tarafından 4.0 olarak değerlendirildi

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , | Categories: Tip Posted by okutbay on 17.12.2009 18:16 | Yorumlar (0)
  1. \' - single quote, needed for character literals
  2. \" - double quote, needed for string literals
  3. \\ - backslash
  4. \0 - Unicode character 0
  5. \a - Alert (character 7)
  6. \b - Backspace (character 8)
  7. \f - Form feed (character 12)
  8. \n - New line (character 10)
  9. \r - Carriage return (character 13)
  10. \t - Horizontal tab (character 9)
  11. \v - Vertical quote (character 11)
  12. \uxxxx - Unicode escape sequence for character with hex value xxxx
  13. \xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
  14. \Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

1 kişi tarafından 1.0 olarak değerlendirildi

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , , , , , , | Categories: Programlama, SSS (FAQ), Tip Posted by okutbay on 13.11.2009 17:32 | Yorumlar (0)

Clipboard is a windows object. If you want to use clipboard in your .NET framework console applications you must reference `System.Windows.Forms` and mark your main method with `STAThreadAttribute` attribute.

Then you can use this class's methods e.g. Clipboard.SetText("some text to store in the clipboard");

Happy coding.

1 kişi tarafından 2.0 olarak değerlendirildi

  • Currently 2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , , , | Categories: Genel, Uygulama Posted by okutbay on 12.11.2009 12:54 | Yorumlar (0)

We, the developers sometimes need work with different languages for some projects. Especially if you are a .NET developer you will need a conversion tool to convert C# to VB.NET or VB.NET to C#.

Here is a useful free tools for that need.

C Sharp to VB.NET

VB.NET to C Sharp

You can check other tool from Toolbox section 

 

1 kişi tarafından 5.0 olarak değerlendirildi

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , , , | Categories: Genel Posted by okutbay on 08.10.2009 16:01 | Yorumlar (0)

According to business rule all footballer name and surname must be capitalized and not longer than 8 chars length.

Very simple to develop. We develop a wrapper method for this job. Seems working fine. Until this morning. Game masters of the http://icanfootball.com noticed that some player names violates this business rule. After my first debuging i found the problem. ToLowerInvariant method is the guilty. No, infact the developer who uses this method is guilty. Because this methods unable to convert "i" to "İ", "ı" to "I" or reverse. This chars specific to Turkish locale and this methods are invariant (means Culture Indepentend and works same as calling ToLower or ToUpper method with CultureInfo.InvariantCulture enum). 

So in my opinion this methods are useless. Please use ToLower or ToUpper method with appropriate culture info.

You can check this sample code.

 

    1 string a = "ı I ş Ş ç Ç ğ Ğ ö Ö ü Ü i İ";

    2 

    3 string b = a.ToLowerInvariant();//b = "ı i ş ş ç ç ğ ğ ö ö ü ü i İ" fails to convert

    4 string d = a.ToUpperInvariant();//c = "ı I Ş Ş Ç Ç Ğ Ğ Ö Ö Ü Ü I İ" fails to convert

    5 

    6 string culture = "tr-TR";

    7 System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(culture);

    8 

    9 string c = a.ToLower(cultureInfo);//d = "ı ı ş ş ç ç ğ ğ ö ö ü ü i i"

   10 string e = a.ToUpper(cultureInfo);//e = "I I Ş Ş Ç Ç Ğ Ğ Ö Ö Ü Ü İ İ"

 

 

1 kişi tarafından 5.0 olarak değerlendirildi

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , , , | Categories: Tip, Web Uygulaması Posted by okutbay on 09.09.2009 10:26 | Yorumlar (0)

You can use using statement to inculde different namespaces in you code behind file. But still you can't call methods from classes that live in other namespaces (Different from you page class namespace) from you content pages (.aspx).

To make this work you must use import namespace directive after you page directive.

    1 <%@ Import="" Namespace="ICF.Common" %>

This can be a hard tast if you have a lot of pages. So you can define your namespaces in you web.config file and call your business class methods in your .aspx pages whenever you want. 

    1 <?xml version="1.0"?>

    2 <configuration>

    3     <system.web>

    4         <pages>

    5             <namespaces>

    6                 <add namespace="ICF" />

    7                 <add namespace="ICF.Common"/>

    8             </namespaces>

    9         </pages>

   10     </system.web>

   11 </configuration>

Happy coding...

1 kişi tarafından 5.0 olarak değerlendirildi

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , | Categories: Tip, Web Uygulaması Posted by okutbay on 08.09.2009 17:46 | Yorumlar (0)

"HttpContext.Current.Cache" has a metod to remove items one by one with their key names.

If you want to remove all items from the cache you can use this little code snippet to achive this task:

 

    1 IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();

    2 while (enumerator.MoveNext())

    3 {

    4     HttpContext.Current.Cache.Remove(enumerator.Key.ToString());

    5 }

Happy coding...

P.S.: This techique don't effect the output cached pages.

1 kişi tarafından 5.0 olarak değerlendirildi

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , | Categories: Genel, Programlama Posted by okutbay on 08.09.2009 09:45 | Yorumlar (0)

JSON stands for Javascript Object Notation and this is the new lightweight (then XML) standart for transferring data from one point to another over the network.  Here you can find a sample web page codebehind to see usage in C#.

 

    1 namespace Prototype

    2 {

    3     public partial class Json : System.Web.UI.Page

    4     {

    5         protected void Page_Load(object sender, EventArgs e)

    6         {

    7             //create instance of the object

    8             SampleObject sampleObject = new SampleObject();

    9 

   10             //Json Serialize

   11             string json = Functions.ToJson<SampleObject>(sampleObject);

   12 

   13             Response.Write(string.Format("Json result: {0}<br/>",Server.HtmlEncode(json)));

   14 

   15             SampleObject deserializedSampleObject = Functions.FromJson<SampleObject>(json);//Json Deserialize

   16             Response.Write(string.Format("Object Result: {0}<br/>", deserializedSampleObject.ToString()));

   17 

   18         }

   19     }

   20 

   21     [Serializable]

   22     public class SampleObject

   23     {

   24         public int Id { get; set; }

   25         public string Name { get; set; }

   26         public string Surname { get; set; }

   27         public string EMail { get; set; }

   28 

   29         public SampleObject()

   30         {

   31             this.Id = 2345;

   32             this.Name = "Ozan K.";

   33             this.Surname = "BAYRAM";

   34             this.EMail = "xyz@abc.com";

   35         }

   36 

   37         public override string ToString()

   38         {

   39             return string.Format("{0} {1}", Name, Surname);

   40         }

   41     }

   42 }

Static methods in 'Functions' class

 

    1 /// <summary>

    2 /// Serializes to Json string

    3 /// Class must be serializable

    4 /// </summary>

    5 /// <typeparam name="T"></typeparam>

    6 /// <param name="ObjectToSerialize"></param>

    7 /// <returns>json string</returns>

    8 public static string ToJson<T>(object ObjectToSerialize)

    9 {

   10     DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

   11     MemoryStream ms = new MemoryStream();

   12     ser.WriteObject(ms, ObjectToSerialize);

   13 

   14     string json = Encoding.Default.GetString(ms.ToArray());

   15     ms.Close();

   16 

   17     return json;

   18 

   19 }

   20 

   21 /// <summary>

   22 /// Deserializes json string to object

   23 /// </summary>

   24 /// <typeparam name="T"></typeparam>

   25 /// <param name="JsonString"></param>

   26 /// <returns></returns>

   27 public static T FromJson<T>(string JsonString)

   28 {

   29     MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(JsonString));

   30 

   31     DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

   32     T returnObject = (T)ser.ReadObject(ms);

   33     ms.Close();

   34 

   35     return returnObject;

   36 }

Happy codings...

2 kişi tarafından 1.5 olarak değerlendirildi

  • Currently 1,5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , , , , , | Categories: Programlama, SSS (FAQ), Tip, Veritabanı Posted by okutbay on 03.09.2009 11:54 | Yorumlar (0)

When you are developing application you may need to store timespan database. (Assuming you are using Sql Server 2005)

TimeSpan.TotalSeconds can be used to store. This returns a double. And maps to decimal data type of the sql server. For reverse action to get the time span you can use TimeSpan.FromSeconds method. This will give you the nearest millisecond value of the stored data.

But if you need accuracy you can use TimeSpan.Ticks method is suitable for you. This returns Int64 (long) and maps to bigint data type for to store in db.You can convert it to TimeSpan with the TimeSpan.FromTicks method.

"Using Ticks more accurate then FromSeconds"

Sample (.aspx page):

 

    1 string startTime = "13:46:33.5268767";

    2 string endTime = "13:47:03.1988761";

    3 DateTime startDate = Convert.ToDateTime(startTime);

    4 DateTime endDate = Convert.ToDateTime(endTime);

    5 

    6 Response.Write(string.Format("Start Date : {0}<br/>", startDate));

    7 Response.Write(string.Format("End Date : {0}<br/>", endDate));

    8 

    9 

   10 TimeSpan timeSpan = endDate - startDate;

   11 double timeSpanValue1 = timeSpan.TotalSeconds;

   12 long timeSpanValue2 = timeSpan.Ticks;

   13 

   14 Response.Write(string.Format("From Seconds : {0} (Rounded To Nearest Millisecond)<br/>",TimeSpan.FromSeconds(timeSpanValue1)));

   15 Response.Write(string.Format("From Ticks : {0} (More Accurate)<br/>", TimeSpan.FromTicks(timeSpanValue2)));

Happy coding...

 

 

 

2 kişi tarafından 2.5 olarak değerlendirildi

  • Currently 2,5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5