Skip to main content

Posts

Read and parse a CSV file into an array of rows and columns in C#

The following PopulateCsvIntoArray method used to read the CSV file into a two-dimensional array of strings. I have included explanation of method lines, wherever needed. /// <summary> /// Populate the CSV file into an array, /// We assume that every line has the same number of fields and there may be blank lines. /// </summary> /// <returns></returns> private string [,] PopulateCsvIntoArray() {     // Get path of CSV file.     var path = Server.MapPath( "~/Folder_Name/testfile.csv" );     // Get the file's text using ReadAllText method.     string fileData = System.IO. File .ReadAllText(path);     // Split CSV data into lines.     fileData = fileData.Replace( '\n' , '\r' );     string [] lines = fileData.Split( new char [] { '\r' },         StringSplitOptio...

Fixed: ‘TypeError: $.browser is undefined’ jQuery?

Reason: Once you upgrade your jQuery ( 1.9.0 or above ) or jQueryUI ( 1.9.2 or above ) library in your application, then you’ll see that many of the existing functionalities get affected. When you see in error console there is error mentioned as: ‘TypeError: $.browser is undefined’ This is because of ‘$.browser’ has been removed from JQuery 1.9 or above. Solution: To resolve the issue you can include the jQuery migrate script and this script file will allow you to use ‘$.browser’ again. This migrate script also restore many other depreciated features. See details of the same here Directly include jQuery migrate script as: < script src ="http://code.jquery.com/jquery-migrate-1.2.1.js"></ script > Alternatively, you can download this script and can use this locally from your application.

C#: Auto-Property Initializer in C# 6.0

Any developer who has worked with C# must have used properties sometimes during development. As you know, Auto-Property is declared with simple get and set (i.e. without any backing field), and can be initialized in the constructor once they are declared. In C# 6.0 a new features is introduced names as Auto-Property initializer . Auto-Property initializer allows property to be initialized like any other field in the same line where it has been declared. Let’s see Auto-Property Initializer in action: public bool UserName { get ; set ; } = "Sandeep" ; Auto-Property initializer in C# 6.0 also allows us to initialize read only Auto-Property in the same line where it has been declared. In older version of C#, we had to use a private set for read only properties, but with C# 6.0, without a private set, you can declare and initialize a property in the same line. Auto-Property Initializer for read only properties: public bool UserN...

Using LINQ concatenate unique items of two List and Sort them.

In this post I’ll show how you can combine unique items of two List < string > and then Sort them with the help of LINQ. Let’s take two List < string > contains employee names: List < string > empNameList1 = new List < string >() {     "Sandeep" ,     "Ashwani" ,     "Ashish" ,     "Saurav" }; List < string > empNameList2 = new List < string >() {     "Rahul" ,     "Sachin" ,     "Sandeep" ,     "Yuvraj" }; As you can see one name is common in both employee lists, so let’s write LINQ statement ( using the Enumerable .Concat method ) to get unique names and then sort them and finally print all the sorted unique names by looping through it. // Concatenate Unique Names of two List<string> and then sort. var finalNameList = empNameLis...

Different ways of String Reversal in C#

As you may be aware C#'s string class don’t have a Reverse() function by default, so let’s discuss different ways to reverse a given string: Using manual reversal way: The traditional way is to reverse a string by manually looping through it character by character and creating a new string. string inputStr = "This is test string" ; string outputStr = "" ; for ( int i = inputStr.Length - 1; i >= 0; i--) {     outputStr += inputStr[i]; } One thing to remember though, if you are using this approach to reverse a large string then you should use StringBuilder to create output string instead of string because a string instance is immutable and you cannot change it after it was created. Any operation that appears to change the string instead returns a new instance. Using Array.Reverse(): The second approach we can reverse a string is with the inbuilt Array.Reverse() method of Array class. st...

Ordering data by more than one column in LINQ query

In this post I’ll show how you can apply ordering on the multiple columns in LINQ query. If you know LINQ basics, you must be aware that with the help of .OrderBy(x => x.Columnname) in the LINQ query we can order data of the source collection. So many of the novice developers use the same function twice as mentioned below and thinks that will do the ordering on the multiple columns. var students = context.Students                 .OrderBy(x => x.Name)                 .OrderBy(x => x.ClassName); But that is not the solution and LINQ query always does the order by the column you specified in the last OrderBy() method. We can do order by on more than one columns using lambda expressions as well as in traditional LINQ query. Mentioned below are two solutions to achieve using Lambda and t...

Solved: Default filters lost on rebind of Telerik MVC Gridview.

Problem: While working with MVC Telerik GridView let’s say you have some default filters using Filterable , as mentioned in below code: @( Html.Telerik().Grid(Model)     .Name( "testGrid" )     .Columns(c =>             {                 c.Bound(m => m.Id).Width(30);                 c.Bound(m => m.Name).Width(110);                 c.Bound(m => m.Telephone).Width(100);                 c.Bound(m => m.IsActive).Width(100);             })     .DataBinding(dataBinding =>  ...