Yeni yazmaya başladığımız projede teknolojinin son nimetlerinden faydalanmak için Visual Studio 2010, .NET Framework 4.0, Entity Framework 4.0 kullanalım dedik... Hatta veritabanımızda Sql Server 2008 olsun dedik...
Development başladı. Her şey çok güzeldi... Ta ki müşteriye göstermek üzere mevcut sunucuların üzerinde bir test sürümü yayınlayana kadar...
Uygulamayı publish ettik... Ama yazdığımız formlardan bir kısmı kayıt eklemiyordu. Logları incelediğimizde şöyle bi exception ile karşılaştık...
"System.ArgumentException: The version of SQL Server in use does not support datatype 'datetime2'."
İlk başta afalladık kaldık tabi... "datetime2 de ne ola ki?" dedik... Farklı kaynaklardan bulduğumuz çözüm işe yaramayınca daha da bi afalladık...
Sorun geliştirme yaptığımız veritabanı ile sunucu üzerindeki veritabanı versiyon farklılığından kaynaklanıyor. Model .EDMX xmlinde bulunan ProviderManifestToken="2008" değerini 2005 olarak değiştirdik... Tekrar derleyip uygulamayı güncelledik. Hata değişmedi. Üstüne gittik veritabanı uyumluluğunu 2008 den 2005 e çekip aynı işlemi tekrarladı... Sorunu aşamadık...
Bakalım nasıl çözeceğiz... Merakla araştırmaya ve denemeye devam ediyoruz...
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
There is an open source project to use with your projects. It's easy to use.
dotnetzip.codeplex.com
Let's zip a folder :)
1 using (ZipFile zip = new ZipFile())
2 {
3 zip.AddDirectory("c:\somedirectory");
4 zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
5 zip.Save("nameofthezipfile.zip");
6 }
Happy coding...
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
You can always write comment to your codes. But comment itself can be outdated and misleads a developer. So writing self-documented codes must our first aim to make our code more maintainable.
In my opinion a big step to achieve self-documented code begins writing if clauses.
If a developer understands if clause, he/she understand purpose of the code block more easily.
But how can we write more readable if clause.
Very easy: Don't write logical comparisons into if clause.
1 if ((preprocessedFailedEmails.Count > 0) || (failedEmails.Count > 0))
2 {
3 //some code
4 }
Although this is a very simple if clause it's a bit hard to get what is happening there. Let's make it more readable.
1 bool hasErrors = ((preprocessedFailedEmails.Count > 0) || (failedEmails.Count > 0));
2 if (hasErrors)
3 {
4 //some code
5 }
Happy coding...
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
This is sample application to serializes sample object
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 using System.IO;
6 using System.Xml.Serialization;
7
8 namespace ConsoleApplication1
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 SampleObject sampleObject = new SampleObject();
15 sampleObject.Id = 35;
16 sampleObject.Name = "Ozan K. BAYRAM";
17
18 XmlSerializer serializer2 = new XmlSerializer(typeof(SampleObject));
19 MemoryStream ms = new MemoryStream();
20 XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
21 xmlSerializerNamespaces.Add("", ""); //if you want namespace for your XML define here
22 serializer2.Serialize(ms, sampleObject, xmlSerializerNamespaces);
23 StringBuilder XMLText = new StringBuilder();
24 ms.Position = 0;
25 using (StreamReader Reader = new StreamReader(ms))
26 {
27 XMLText.Append(Reader.ReadToEnd());
28 }
29
30 string serializedObject = XMLText.ToString();
31 }
32 }
33 }
Output string
Sample class to use
1 [Serializable]
2 [System.Xml.Serialization.XmlRoot("sampleobject")]
3 public class SampleObject
4 {
5 [System.Xml.Serialization.XmlAttribute("id")]
6 public int Id { get; set; }
7
8 [System.Xml.Serialization.XmlElement("name")]
9 public string Name { get; set; }
10 }
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
Tags: .net, .net framework 2.0, .net framework 3.0, .net framework 3.5, .net framework 4.0, c#, vb.net, dotnet, console |
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.
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
While we are coding we used to delete blank lines with 'Shift + Del'. Shift + Del shortcut is same as 'Ctrl + X'. And if you copy something to clipboard before cutting the blank lines, it will be replaced by the blank line.
You can change this behaviour with VS 2008 settings. Tools > Option > Text Editor > All Languages (or just for specific language) > Uncheck 'Apply Cut or Copy commands to blank lines when there is no selection'
Happy coding...
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
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...
1 kişi tarafından 2.0 olarak değerlendirildi
- Currently 2/5 Stars.
- 1
- 2
- 3
- 4
- 5
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...
1 kişi tarafından 4.0 olarak değerlendirildi
- Currently 4/5 Stars.
- 1
- 2
- 3
- 4
- 5
For example you have a base class and a class that inherits it like below.
1 public class A
2 { }
3
4 public class B : A
5 { }
And you have generic list of these types. Then if you want to convert List<B> to List<A>
You can use this code block:
1 List<B> listB = new List<B>();
2 List<A> listA = listB.Cast<A>().ToList();
Happy coding.
Bu yazıyı ilk değerlendiren siz olun
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
Bir süredir blogengine.net kullanıyorum. Wordpressle tabiki kıyaslanamaz ama .NET muadilleri arasında fena bir yerde değil.
Arkadaşlarımın yazılım ile ilgili tecrübelerini paylaştıkları sitelere link vermek için BlogRoll extensionı ana sayfaya ekledim ve bir kaç link tanımladım.
Herşey çok güzeldi. Ta ki bir linke tıklayana kadar. Tıkladım ve link benim sayfam ile aynı pencerede açıldı. Hemen panik yapmadım. "Sanırım yeni sayfada aç demeyi unuttum" diyerek yönetim sayfasına gittim. Ama ne yazıkki böyle bir ayar olmadığını görünce bir miktar üzüldüm... Tabi bu dünyanın sonu değildi. Open source bunun için vardı. Hiç açmayacaksak nasıl open source olacaktı değil mi?
App_Code/Controls dizini altında bulunan Blogroll.cs dosyasını açtım ve kodu biraz inceledim. Kısa bir gözatmadan sonra şu kodlara ulaştım...
135 HtmlAnchor webAnchor = new HtmlAnchor();
136 webAnchor.HRef = item.WebsiteUrl;
137 webAnchor.InnerHtml = EnsureLength(item.Name);
Tabi bu noktada iki şey yapılabilirdi... Gidip bu webAnchor class ı neymiş diye bakabilirdim. Ya da yazılımcılık hislerime güvenerek ufak bir kumar oynayabilirdim. Ve kumar oynadım.
135 HtmlAnchor webAnchor = new HtmlAnchor();
136 webAnchor.HRef = item.WebsiteUrl;
137 webAnchor.Target = "_blank";
138 webAnchor.InnerHtml = EnsureLength(item.Name);
Her halde bu sınıfı yazan yazılcı bu özelliği unutacak kadar eşşek olamazdı. Allah'tan olmamış da... Artık blogroll linlerim yeni sayfada açıyor...
1 kişi tarafından 5.0 olarak değerlendirildi
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5