Showing posts with label C Sharp. Show all posts
Showing posts with label C Sharp. Show all posts

Thursday, 29 August 2013

Deserializing CSV file to Collection

Every one know that CSV file stores the tabular data in plain text format.A CSV file consists of any number of records, separated by line breaks of some kind; each record consists of fields, separated by some other character or string, most commonly a literal comma or tab. Usually, all records have an identical sequence of fields. I have a requirement like I want to read the CSV file and Convert to Collection of objects. Steps to convert the CSV file to collection as follows.
  1. Read all the lines from the CSV file
  2. Take the first line as the header and remaing lines as records
  3. Convert the data to data table by parsing first line to data columns and remaining lines to data rows
  4. Convert data table to collection
Sample code to implement step 1 to 3

 public static DataTable ToDataTable(string csvFileNameIncludingPath)
        {
            var dt = new DataTable();

            var s = File.ReadAllLines(csvFileNameIncludingPath);
            string[] tableData = s.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var col = from cl in tableData[0].Split(",".ToCharArray())
                      select new DataColumn(cl.TrimStart('"').TrimEnd('"')); // In my case all the field values are included in the double quotes ex: "FirstName". So used TrimStart and TrimEnd methods to remove double quotes
            dt.Columns.AddRange(col.ToArray());

            (from st in tableData.Skip(1)
             select dt.Rows.Add(st.Split(",".ToCharArray()).Select(str => str.TrimStart('"').TrimEnd('"')).ToArray())).ToList(); // In my case all the field values are included in the double quotes ex: "jhon". So used TrimStart and TrimEnd methods to remove double quotes

            return dt;
        }
Sample code to implement step 4

 public static List<T> ToCollection<T>(this DataTable dt, IList<KeyValuePair<string, string>> changedNames = null) // changeNames parameter used for mapping the column name change with object propertyname
        {
            List<T> lst = new List<T>();
            Type tClass = typeof(T);
            PropertyInfo[] pClass = tClass.GetProperties();
            List<DataColumn> dc = dt.Columns.Cast<DataColumn>().ToList();
            T cn;
            foreach (DataRow item in dt.Rows)
            {
                cn = (T)Activator.CreateInstance(tClass);
                foreach (PropertyInfo pc in pClass)
                {
                    // Can comment try catch block. 
                    try
                    {
                        DataColumn d = dc.Find(c => c.ColumnName == pc.Name || (changedNames != null && changedNames.Any(x => x.Key == c.ColumnName)));
                        var ds = dc.Where(x => x.ColumnName == pc.Name);
                        if (d != null)
                            pc.SetValue(cn, item[pc.Name], null);
                    }
                    catch
                    {
                    }
                }
                lst.Add(cn);
            }
            return lst;
        }
I hope this will helps you.

Saturday, 17 August 2013

DataSet Deserialization from Webservice

I observed that few of the webservices or REST services returns the DataSet with the xml read mode as DiffGram. But our requirement is to deserialize into collection of object of sepecified type. The deserialization of dataset is different from the normal xml or json deserialization. Please find the below steps to deserialize dataset.

Step1: Create the response to DataSet object as follows.
private DataSet GetDataSet(string url)
        {
            DataSet ds = new DataSet();
            using (var client = new WebClient())
            {
                var xml = client.DownloadString(sURL);
                using (var reader = XmlReader.Create(new StringReader(xml)))
                {
                    ds.ReadXml(reader, XmlReadMode.DiffGram);
                }
            }
            return ds;
        }
Step 2: Use the following helper method to Convert Data Table to Collection
 public static List<T> ToCollection<T>(this DataTable dt)
        {
            List<T> lst = new System.Collections.Generic.List<T>();
            Type tClass = typeof(T);
            PropertyInfo[] pClass = tClass.GetProperties();
            List<DataColumn> dc = dt.Columns.Cast<DataColumn>().ToList();
            T cn;
            foreach (DataRow item in dt.Rows)
            {
                cn = (T)Activator.CreateInstance(tClass);
                foreach (PropertyInfo pc in pClass)
                {
                    // Can comment try catch block. 
                    try
                    {
                        DataColumn d = dc.Find(c => c.ColumnName == pc.Name);
                        if (d != null)
                            pc.SetValue(cn, item[pc.Name], null);
                    }
                    catch
                    {
                    }
                }
                lst.Add(cn);
            }
            return lst;
        }
Step 3: Use the methods as follows to Convert DataTable to collection
var ds= GetDataSet(url);
ds.Tables[DataTables.LotTable].ToCollection<Lot>();

I hope this helps you..

Monday, 24 December 2012

Xml Serialization Behavior

Xml Serializer behaves differently based on the datatype and the behaviour of the property(Nullable/Not Nullable). Some time we may get confusion on why the xml serializer throwing an exception. Find the below table contains when the xml serializer throws an exception or when the object is instantiated.


Data Type

Property Behavior

Tag removed

i:nil="true"

Empty Tag

Invalid Content

Comments

Int, decimal

Nullable

Null

Null

Exception

Exception

Not Nullable

0

Exception

Exception

Exception

DateTime

Nullable

Null

Null

Exception

Exception

DateTime should be the following format, otherwise throws an exception
1) yyyy-MM-ddTHH:mm:ss
2)yyyy-MM-dd
3)HH:mm:ss

Not Nullable

0001-01-01T00:00:00

Exception

Exception

Exception

Collections

-NA-

Initialized new object with collection count as zero

Initialized new object with collection count as zero

Initialized new object with collection count as zero

Initialized new object with collection count as zero

Decorate property with [XmlElement(IsNullable=true)] attribute to set the property as null instead of initializing new object

Csharp Object

-NA-

Null

Initialized to New Object

Initialized to New Object

Initialized to New Object

Decorate property with [XmlElement(IsNullable=true)] attribute to set the property as null instead of initializing new object

Enum

Nullable

Null

Null

Exception

Exception

Not Nullable

Initialized with firt enum element

Exception

Exception

Exception

Tuesday, 10 July 2012

Could not load type 'System.Web.Http.Dependencies.IDependencyScope'

I am working on one Asp.Net WebApi project along with 5 members in my team. We are using
Asp.net MVC4  Beta for the WebApi project. Everything is working for me fine. Suddenly one day I got the latest version of code from TFS and try to run the application. The solution build succeed, but the application throwing the following exception.
Could not load type 'System.Web.Http.Dependencies.IDependencyScope' from assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
The same application is running in my other teammates. I researched around one day, but did not found any solution that why this dll is not loading run time even though the dll is present in the bin folder.  One of my team mate told that Asp.Net MVC4 is updated from beta to release version. I updated that in my system then the application is working fine. There was mismatch in the team communicatin about this updatation I lost my one day. If any one get this exception try to update the WebApi to release candidate.

Friday, 18 May 2012

structuremap 2.6 IncludeConfigurationFromConfigFile=true not working

When we are using structuremap 2.6.x  and adding structuremap configuration in the app.config/web.config. To  include configuration every one set the property IncludeConfigurationFromConfigFile=true. It is not working and seems to be not implemented. But when we are refering same app.config with AddConfigurationFromXmlFile(configfilepath) then it is working fine. when we are using app.config the file name of configuration is changed after the solution build. To include that configuration file find the solution below.
To get the name and path of configuration file as follows
var configName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 
The example method and usage as follows:

 private static IContainer InitiazlizeFromAppConfigOrWebConfig()
 {
   var configName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 
    return new Container(x =>
    {
     x.AddConfigurationFromXmlFile(configName);
      //x.IncludeConfigurationFromConfigFile = true; //seems not imeplemented
    });
}

Wednesday, 16 May 2012

ObjectFactory Vs Container StructureMap

ObjectFactory is a static gateway for an instance of container. If you only ever want one instance of a container, and want a simple static way to get at it, use ObjectFactory. You must Initialize the ObjectFactory, and then retrieve your instances via ObjectFactory.

 Alternatively, if you want to manage the lifetime of the container yourself, you can create an instance of Container, passing an initialization expression to the constructor. You then retrieve instances from the variable you declared to store the Container.

Thursday, 26 April 2012

[Fixed]How to overcome the linq contains limitation.Too many parameters were provided in this RPC request. the maximum is 2100

When we want to filter the data from the Linq to Sql query with the collection every one is trying to use the Contains() operator as follows. Linq to Sql will try to generate a sql query to represent the whole expression. This means that it will try to pass in the collection as parameters to a sql query. If there are too many parameters the query will hit the parameter limit (2100) and it cannot be executed. If there are not that many parameters, then you can use a contains expression which will be converted to an "IN" expression in sql. Sample code as follows
List<int> contactIds=Contact.GetContactIds()
//count of contactIds is morethan 2100
var contacts = Context.Contacts.Where(c => contactIds.Contains(c.ContactId)).ToList();
If the parameter limit exeeds then the following exception throws.
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RPC request. The maximum is 2100.
To avoid this many developers will try to join the in memory collection with database table as follows. 
List<int> contactIds=Contact.GetContactIds()
//count of contactIds is morethan 2100
var contacts = Context.Contacts.Join(contactIds, c => c.ContactId, ci => ci, (c, ci) => c).ToList();
When developer trying this the following exception throws.
Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
This means that the linq to sql generates sql query and executes on the database server. Our in memory collection will not support to execute on the database server. so will get the above exception.
The easiest resolution is to convert the LINQ to SQL table into an IEnumerable list, which can be done as follows.
List<int> contactIds=Contact.GetContactIds()
//count of contactIds is morethan 2100
var contacts = Context.Contacts.AsEnumerable().Join(contactIds, c => c.ContactId, ci => ci, (c, ci) => c).ToList();
I hope this solution helps you.

Wednesday, 25 April 2012

How to read the conig file of class library in C Sharp

Every body knows that we can define the appsettings in the app.config file of class library. When we are refering the existing class library to the other new class library that only takes dll but not the configuration file of existing class library. To read values from the configuration file one option is to define same appsettings in the new class library. the other solution is as follows.

Solution:

  1. Refer the class library to the other new class library.
  2. Copy the .dll.config file from bin/debug folder of the existing class library and add to the new class library.
  3. Use the following code to get the values from the .dll.config file.
/// Load the .config file for the current DLL
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

Configuration dllConfig = ConfigurationManager.OpenExeConfiguration(assembly.Location);

KeyValueConfigurationCollection settings = dllConfig.AppSettings.Settings;
//read the value as follows
string value=settings[key].value;

Friday, 16 March 2012

Handling string null or empty in Deserialization c sharp

When we are deserializing the string to object which we are passing the parameter with dynamic values, we don't know the parameter is null or empty. If we do not handle null or empty then it will throws an exception. But if the method is generic type then we don't know which type of class it is to create an instance and return. To create an instance without new keyword and the type of class use the solution below.
public static T DeSerializeDataContract<T>(String obj)
        {
            if (string.IsNullOrEmpty(obj))
            {
                Type type = typeof(T);
                return (T)Activator.CreateInstance(type);
            }
            using (var stringReader = new StringReader(obj))
            using (var reader = new XmlTextReader(stringReader))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                return (T)serializer.ReadObject(reader);
            }

        }

Monday, 20 February 2012

How to get the current controller and action names in View Mvc3

In MVC we can use the partial views to avoid the redundant code. I have one requirement that the action names are same in two different controllers and there is partial view which has one link which needs to point to the current controller with the same action. I want to access the controller name in View to avoid the redundant code. The solution to get the Controller and Action names in View as follows. Solution:
 @{
        var controller = this.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
    
        Change password
        }
If you want to access the action name
var action=this.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();

Jquery Ajax Request and Unauthorized Access handling in MVC3

When we are posting the data through jquery $.ajax and  we are using asp.net forms authentication, if the user is not authorized then we will redirect to the login page. the same login page response is return result as on jquery ajax success. If we throw any exception in controller or any custom authorize attribute then ajax request returns the 500 status code internal server exception as its default behaviour. 500 status code reruns any exception in the request processing. But our view is to return the 401 as the status code. I research about many hours to get this solution.

Solution1:

1. Custom authentication attribute
public class AdminAuthorizeAttribute : AuthorizeAttribute  
{   
 protected override bool AuthorizeCore(HttpContextBase httpContext)
 {    
  User currentUser = ClientContext.Current.User;    

  return currentUser != null;      

 }

}
If the user is not authenticated the reruns to logon action as i am using forms authentication in asp.net.
2. In the controller action result write the following
 public ActionResult Logon()
 {
  if (Request.IsAjaxRequest())
  {
 
  //this is used when the authentication fails in the ajax request
  //this returns the httpstatus code 401 to the browser.
 
   ControllerContext.HttpContext.Response.StatusCode = 401;
 
   return Content(string.Empty);
  
   }

 } 
3. Handle the status codes in jquery $.ajaxSetup
$.ajaxSetup({
             statusCode: {
                        401: function () {
 
                            // Redirect the to the login page.
                            window.location = "/login/";
 
                        }
                    }
            });
Solution2:
public class AdminAuthorizeAttribute : AuthorizeAttribute  
{   
 protected override bool AuthorizeCore(HttpContextBase httpContext)
 {    
  User currentUser = ClientContext.Current.User;    

  return currentUser != null;      

 }


public override void OnAuthorization(AuthorizationContext filterContext)
{
   base.OnAuthorization(filterContext);

  // If its an unauthorized/timed out ajax request go to top window and redirect to logon.
    if (filterContext.Result is HttpUnauthorizedResult && filterContext.HttpContext.Request.IsAjaxRequest())
    {
filterContext.Result = new JavaScriptResult() { Script = "top.location.reload()"    };
    }

  // If authorization results in HttpUnauthorizedResult, redirect to error page instead of Logon page.
            if (filterContext.Result is HttpUnauthorizedResult)
            {
                if (ClientContext.Current.User == null)
                {
                    filterContext.Result =
                        new RedirectResult(
                            string.Format(
                                "~/logon/logon?ReturnUrl={0}",
                                HttpUtility.HtmlEncode(filterContext.HttpContext.Request.RawUrl)));
                }
                else
                {
                    filterContext.Result =
                       new RedirectResult(string.Format("~/error/unauthorized"));
                }
            }
        }
}
I hope this will help you. 

Friday, 14 October 2011

Replacing special characters while creating directory or file

When we are creating directory in windows it could not accept the spacial character for directory name.
If folder name string is dynamic( we don't know which string it is) then we need to write the spacial character from that string. To repalce special characrers while creating directory as follows(this step must be added if we don't confirm that string)
directoryname=System.Text.RegularExpressions.Regex.Replace(directoryname,@"[*?:\|<>\\/]", "-");

Monday, 3 October 2011

How to get comma separated string from the list of object for specified property

I have a one requirement that I have list of objects with that object have properties(ID, Name, Description). I want a comma separated string from the list of object for ID property.
We can achieve this by using Reflection classes.

The solution for the requirement has follows

Create a generic method to get the Comma separated string
 public static string GetCommaSeparatedString<T>(List<T> list, string property)
        {
                string value = string.Empty;
                PropertyInfo info = typeof(T).GetProperties().Where(i => i.Name == property).FirstOrDefault();
                if (info != null)
                {
                    foreach (T listItem in list)
                    {
                        value += info.GetValue(listItem, null).ToString() + ",";
                    }
                }
            return value.Substring(0,value.Length-1);
        }
Usage:
Take an example with Category class with the properties ID,Name
public class Category
{      
    public int ID { get; set; }
    public string Name { get; set; }
}
Create a list with Category objects
List<category> categories=new List<category>();
categories.Add(new Catagory {ID=1, Name = "abc" });
categories.Add(new Catagory {ID=10, Name = "pqr" });
Call the method as follows
GetCommaSeparatedString<Category>(categories,"ID");
//output: 1,10

Friday, 29 July 2011

Getting the Enums Description

Some times we need to enums for strongly typed strings in place of normal string values. But while displaying the enums we need the space between two words at that time we uses the enum description as follows
public enum MenuOptions
{
[Description("Park Vehicle")]
ParkVehicle = 1,
[Description("Exit Vehicle")]
ExitVehicle = 2,
[Description("Check Slot is Empty")]
CheckSlotIsEmpty = 3,
[Description("Parked Vehicles List")]
ParkedVehiclesList = 4,
[Description("Exit")]
Exit = 5
}

Code to get the Enum Description:
public String GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes
(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}

The usage of enum description as follows
public static void GetNavigation()
{
foreach (int i in Enum.GetValues(typeof(MenuOptions)))
Console.WriteLine("{0}. {1}", i,GetEnumDescription((Enum)Enum.Parse(typeof(MenuOptions),Enum.GetName(typeof(MenuOptions), i))));
}

Declaring and using delegates in csharp

The following is the simple way to declare and using delegates
using System;
using System.Drawing;
using System.Windows.Forms;

// custom delegate
public delegate void Startdelegate(); //we can pass any type of parameter ex StartDelegate(string x);

class Eventdemo : Form
{
// custom event
public event Startdelegate StartEvent;

public Eventdemo()
{
Button clickMe = new Button();

clickMe.Parent = this;
clickMe.Text = "Click Me";
clickMe.Location = new Point(
(ClientSize.Width - clickMe.Width) /2,
(ClientSize.Height - clickMe.Height)/2);

// an EventHandler delegate is assigned
// to the button's Click event
clickMe.Click += new EventHandler(OnClickedMe);
// our custom "Startdelegate" delegate is assigned
// to our custom "StartEvent" event.
StartEvent += new Startdelegate(OnStartEvent);

// fire our custom event
StartEvent(); // if pass the parameters while declaring then call like StartEvent("hello")
}

// this method is called when the "StartEvent" Event is fired
public void OnStartEvent()
{
MessageBox.Show("I Just Started!");
}
//this method is called when button is clicked
public void OnClickedMe(object delSender, EventArgs delE)     
    { 
       MessageBox.Show("You Clicked My Button!");  
     };

 static void Main(string[] args)
{
Application.Run(new Eventdemo());
}
}

Note: EventHandler is a builtin type delegate. We can write our own delegates to handle events.

Creating an Xml document using XmlWriter in C Sharp

The following is the sample code to create xml document using c sharp
FileStream fs = new FileStream("products.xml", FileMode.Create);

XmlWriter w = XmlWriter.Create(fs);

w.WriteStartDocument();
w.WriteStartElement("products");

w.WriteStartElement ("product");
w.WriteAttributeString("id", "1001");
w.WriteElementString("productName", "Gourmet Coffee");
w.WriteElementString("productPrice", "0.99");
w.WriteEndElement();

w.WriteStartElement("product");
w.WriteAttributeString("id", "1002");
w.WriteElementString("productName", "Tea Pot");
w.WriteElementString("productPrice", "12.99");
w.WriteEndElement();

w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();

We can write it also by using XmlTextWriter. the following is the sample code is using XmlTextWriter
// prepare and write XML document
using (StringWriter writer = new StringWriter())
{
using (XmlTextWriter doc = new XmlTextWriter(writer))
{
// prepare XML header items
doc.Formatting = Formatting.Indented;
doc.WriteComment(”Summarized Incoming Call Stats”);
doc.WriteStartElement(”contacts”);
// join calls with contacts data
foreach (Contact con in contacts)
{
if (callGroups.ContainsKey(con.Phone))
{
List calls = callGroups[con.Phone];
// calculate the total call duration and average
long sum = 0;
foreach (CallLog call in calls)
sum += call.Duration;
double avg = (double)sum / (double)calls.Count;
// write XML record for this contact
doc.WriteStartElement(”contact”);
doc.WriteElementString(”lastName”,
con.LastName);
doc.WriteElementString(”firstName”,
con.FirstName);
doc.WriteElementString(”count”,
calls.Count.ToString());
doc.WriteElementString(”totalDuration”,
sum.ToString());
doc.WriteElementString(”averageDuration”,
avg.ToString());
doc.WriteEndElement();
}
}
doc.WriteEndElement();
doc.Flush();
doc.Close();
}
Console.WriteLine(writer.ToString());
}

Declaring and using anonymous types

The following is the way of declaring and using anonymous types
// simple anonymous type declaration
Console.WriteLine(”- Simple Anonymous Type -”);
var item = new { Name = ”Car”, Price = 9989.00 };
Console.WriteLine(”Type: {0}, Name: {1}, Price: {2}”,
item.GetType().ToString(), item.Name, item.Price);

 

// declaring and working with array of anonymous types
Console.WriteLine();
Console.WriteLine(”- Iterating Anonymous Types Array -”);
var list = new[] {
new { LastName = ”Magennis”,
DateOfBirth = new DateTime(1973,12,09)
},
new { LastName = ”Doherty”,
DateOfBirth = new DateTime(1978,1,05)
}
};
foreach (var x in list)
Console.WriteLine(”{0} ({1})”,
x.LastName, x.DateOfBirth);

Thursday, 28 July 2011

Grouping list of objects

There are two methods for grouping list of  objects in c sharp. one is using  Dictionary and other one is using linq to objects

Using StoredDictionary
// group by state (using a sorted dictionary)
SortedDictionary<string, List<Contact>> groups =
new SortedDictionary<string, List<Contact>>();
foreach (Contact c in contacts)
{
if (groups.ContainsKey(c.State))
{
groups[c.State].Add(c);
}
else
{
List<Contact> list = new List<Contact>();
list.Add(c);
groups.Add(c.State, list);
}
}
// write out the results
foreach (KeyValuePair<string, List<Contact>>
group in groups)
{
Console.WriteLine(”State: “ + group.Key);
foreach (Contact c in group.Value)
Console.WriteLine(” {0} {1}”,
c.FirstName, c.LastName);
}

Using Linq to objects:

Linq to objects works from 3.0 .net frame work

var query = from c in contacts group c by c.State;
// write out the results
foreach (var group in query)
{
Console.WriteLine(”State: “ + group.Key);
foreach (Contact c in group)
Console.WriteLine(” {0} {1}”,
c.FirstName, c.LastName);
}

Sorting a list of object in c sharp

There are three methods to sort list of  objects in c sharp. one is using Deletages, second Implementing IComparer interface, and other one is using linq to objects

Implementing IComparer(C#1.0)
class ContactNameComparer : IComparer
{
public int Compare(Contact x, Contact y)
{
return x.Name.CompareTo(y.Name);
}
}
...
List<Contact> contacts=Contact.SampleData();
contacts.Sort(new ContactNameComparer());
foreach (Contact contact in contacts)
{
Console.WriteLine(contact);
}


Using  inline delegates(from C#2.0)

List<Contact> contacts = Contact.SampleData();
// sort by last name
contacts.Sort(
delegate(Contact c1, Contact c2)
{
if (c1 != null && c2 != null)
return string.Compare(
c1.LastName, c2.LastName);
return 0;
}
);

Using Linq to Objects(From C#3.0)

List<Contact> contacts = Contact.SampleData();
// perform the LINQ query
var query = from c in contacts
orderby c.State, c.LastName;