Skip to main content

Posts

Showing posts from November, 2013

C#: How to get description from Enum value (using Extension method).

To get the description from Enum value we can write our own string extension method and can use that method in same way, we are using other existing methods. We’ll take help of reflection to get description from Enum value.
Extension method:
usingSystem; using System.Reflection; usingSystem.ComponentModel;
publicstaticclassEnumExtensions { publicstaticstring GetDescription(thisEnum en)     { var type = en.GetType(); var mInfo = type.GetMember(en.ToString()); if (mInfo.Length > 0)        { var attrs = mInfo[0].GetCustomAttributes(

System.NotSupportedException: SQL Server does not handle comparison of NText, Text, Xml, or Image data types.

While updating in SQL server via LINQ to SQL sometimes you get the exception: “System.NotSupportedException: SQL Server does not handle comparison of NText, Text, Xml, or Image data types”
To fix this exception let’s see the possible solutions:
Solution 1: If you are getting this exception with NText, Text, or Image data type fields, the reason is that NText, Text and Image types are deprecated and these fields must be replaced with the NVARCHAR(MAX), VARCHAR(MAX) and VARBINARY(MAX) types respectively. These types support string operators, including equality comparison. And you should be fine then.
Solution 2: If you are getting this exception with XML field of your table, then reason is XML field can never be compared as a string. In order to fix this issue open the dbml file with xml editor and set the “updatecheck” to “Never” as follows:
<columncanbenull="true"dbtype="Xml"name="PermissionsXml" type="System.Xml.Linq.XElement"updatecheck="Neve…

String or binary data would be truncated. The statement has been terminated.

While inserting or updating data into SQL table one may get exception like “String or binary data would be truncated. The statement has been terminated.”
Reason: This exception occurs when length of your DB column data type is less as compared to value entered by user. For example: You have one Email field with data type as Nvarchar(20) in Employee table, and user try to enter testuseremail@testemail.com in this email field, if you see email entered by user is of 27 characters which exceeds DB column value and user will get this error.
Solution: To solve this error change the size of your DB field, i.e. in above scenario we need to increase the size of Email field, change it to Nvarchar(50) and you’ll see error is gone.