Skip to main content

Posts

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: using   System; using System.Reflection; using   System. ComponentModel ; public   static   class   EnumExtensions {       public static string GetDescription( this Enum 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: < column   canbenull = "true"   dbtype = "Xml"   name = "PermissionsXml"   type...

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.

Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

Today while working suddenly my laptop crashed and restarted again. Once laptop restarted, I opened the project in Visual Studio 2012 and tried to run my application, but I got weird Assembly Load Error: Could not load file or assembly...: Full error read as: Could not load file or assembly ‘AjaxControlToolkit’ or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0×80070057 (E_INVALIDARG)) I tried to clean and rebuild my application, and even closed and re-open visual studio, but still I faced the same error. Even I removed the assembly reference of 'AjaxControlToolkit' and added reference back to my application, but still no luck. After some research I found out that AjaxControlToolkit assembly corrupted in Temporary ASP.NET Files folder, due to which it is throwing error. Solution:   ·          Close all instances of Visual Studio.   ·        ...

LINQ to Entities does not recognize the method 'Boolean like String (System.String)' method, and this method cannot be translated into a store expression.

While working with LINQ queries (IQueryable<T>) one may face the error of 'LINQ to Entities does not recognize the method...’ For example consider following LINQ statement: dbContext.Employees .Where(c => ! string .IsNullOrWhiteSpace(c.Name)); There will be not any compile error and it'll be compiled successfully, however you'll get the error (mentioned below) when this query get executed. "LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method, and this method cannot be translated into a store expression." Reason: Once you get the error, you'll try to figure out what went wrong with this LINQ statement, as there is no error if you compile the above statement. Simple reason of this error is IQueryable<T> creates the SQL friendly query and once you enumerate (i.e. by accessing any property or converting it into IEnumerable<T> or List) over this query it...

C#: How to format a Numeric String or Decimal as Currency

In this topic I will show examples on how to format your string, decimal/double or integer value in currency with C#. While working on any application it is always good to show user more human friendly money format rather than showing simple value. Formatting currency is quite easy with C#. We’ll use the standard Currency (“C”) Format Specifier, it works like this: {0:C}. //declare amount. decimal amount = 1704.40m; string currency = string .Format( " {0:C} " , amount); We can get same result with the help of ToString() method like: string currency = amount.ToString( "C" ); Output in both case will be: $1,704.40 We can also get output in different currency symbol (e.g. ₹ $ £ ₩ € etc.) using culture information. With the help of “ CultureInfo .CreateSpecificCulture( "culuture-name" ) “ one can set the culture information, and can print the price with the proper currency symbol. For example: ...

SignalR Tutorial: Your first SignalR chat application with ASP.net MVC 4

In last post I explained what SignalR, why do us need it is and what are the prerequisites. In this tutorial we’ll use ASP.NET SignalR to create a real-time chat application. First of all we’ll add SignalR library to an MVC 4 application and then create hub class (to push content to client) and after that we’ll use SignalR jQuery library to send messages and display updates from the hub. ·          Open Visual Studio -> Select ASP.NET MVC4 Web Application template (under Web Templates) and enter the name of application as “MVCSignalRApp” and then click OK. ·          On the next screen select “Internet Application” template and then click OK. ·          Your basic MVC application is ready to use, now we need to add SignalR libraries to this application. To add SignalR libraries open Package Manager Console (TOOLS - >Library Package M...