.NET Framework 4.5.2 missing in Visual Studio 2013.

Hi even after installing .NET Framework 4.5.2, it was missing from Visual Studio 2013’s Target framework.

Had to download and install it from the following location

http://www.microsoft.com/en-us/download/details.aspx?id=42637

Hope it helps..

Dll not getting copied to bin directory in Visual Studio

Hi,

In one of our projects we were referencing custom assemblies that were part of the same solution. However those assemblies were not getting copied to the bin directory of the project upon building the project.

We had also set property Copy Local as true for those class library projects.

The reason for this was as we were adding those assemblies in GAC.

VS.NET will copy the dll to the bin directory if your system cannot find the dll in the GAC.”

After removing those assemblies from GAC it added those dlls in the bin.

The helpful thread

http://stackoverflow.com/questions/3548731/visual-studio-2010-add-reference-is-copying-dll-to-bin-directory

Hope it helps..

Serialize and Deserialize InfoPath form and extract the attachment using WebClient in C#

Hi,

Suppose this is our InfoPath form

First we will generate the class for the above InfoPath form

https://nishantrana.me/2014/07/13/generate-a-c-class-from-an-infopath-form/

The sample code


myFields invoice = null;
WebClient webclient = new WebClient();
webclient.UseDefaultCredentials = true;

using (Stream fileStream = webclient.OpenRead("http://server:5000/formlibrary/FirstForm.xml?NoRedirect=true"))
{
if (fileStream != null)
{
XmlTextReader reader = new XmlTextReader(fileStream);

reader.Read();
reader.MoveToContent();

XmlSerializer xser = new XmlSerializer(typeof(myFields));
myFields invoiceInfopath = (myFields)xser.Deserialize(reader);
byte[] b = invoiceInfopath.field2;

// filename
int nameBufferLen = b[20] * 2;
byte[] fileNameBufffer = new Byte[nameBufferLen];

for (int i = 0; i < nameBufferLen; i++)
{
fileNameBufffer[i] = b[24 + i];
}

char[] charFileName = UnicodeEncoding.Unicode.GetChars(fileNameBufffer);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length - 1);

// attchment
byte[] fileContent = new byte[b.Length - (24 + nameBufferLen)];

for (int i = 0; i < fileContent.Length; i++)
{
fileContent[i] = b[24 + nameBufferLen + i];
}

FileStream fs = new FileStream(@"C:\test\" + fileName, FileMode.Create);
fs.Write(fileContent, 0, fileContent.Length);
fs.Close();
}

Hope it helps.

How to Read the InfoPath content and extract the attachment using XMLDocument in C#

The InfoPath form:-

The namespace and the field1 that is node that contains the attachment

Sample Code:


XmlDocument myDoc = new XmlDocument();
myDoc.Load(@"C:\test\form.xml");

// specify the name space
XmlNamespaceManager ns = new XmlNamespaceManager(myDoc.NameTable);
ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-07-12T15:29:09");

// the node that contains the attachment
XmlNodeList nl = myDoc.SelectNodes("/my:myFields/my:group1/my:group2/my:field1", ns);
foreach (XmlNode n in nl)
{
string s = n.InnerText;
byte[] b = Convert.FromBase64String(s);
int nameBufferLen = b[20] * 2;

// file name buffer to get the filename with extension
byte[] fileNameBufffer = new Byte[nameBufferLen];

for (int i = 0; i < nameBufferLen; i++)
{
fileNameBufffer[i] = b[24 + i];
}
char[] charFileName = UnicodeEncoding.Unicode.GetChars(fileNameBufffer);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length - 1);

// byte array for the remaining content
byte[] fileContent = new byte[b.Length - (24 + nameBufferLen)];

for (int i = 0; i < fileContent.Length; i++)
{
fileContent[i] = b[24 + nameBufferLen + i];
}

FileStream fs = new FileStream(@"C:\test\" + fileName, FileMode.Create);
fs.Write(fileContent, 0, fileContent.Length);
fs.Close();

}

Hope it helps..

 

 

 

 

Generate a C# class from an InfoPath form

To generate a C# class from change the extension to .cab from .xsn.

Open and copy the content of cab file to a separate folder

Open the Visual Studio Developer Command Prompt and run the XSD.exe to generate the class

 

Hope it helps…

The current identity “domain\username” does not have write access to ‘C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files’.

To reslove this error,

i first tried registering ASP.NET 2.0

1. Open a command prompt.
2. Navigate to ‘C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\’
3. Run this: aspnet_regiis.exe -i

Followed by adding the “domain\username” to the “IIS_IUSRS” group.

This resolved the error.

New features in C# language

Auto-Implemented Properties

Suppose this is our Person class with two properties First Name and Last Name.

public class Person
    {
        private string firstName;
        private string lastName;
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName= value;
            }
        }  
        public string LastName
        {
            get
            {
                return lastName ;
            }
            set
            {
                lastName = value;
            }

        }
    }

 

Here we are not putting any extra logic while using get and set blocks for the properties, so the above code could be written as following using Auto-Implemented Properties.

 public class Person
    {
        public string FirstName { get;set;}
        public string LastName { get; set; }

    }

 

Things to remember :-

Both get and set accessor are required.

To make it read only we can declare set accessor as private

 public string LastName { get; private set; }
 
 

Object Initializers 

To initialize our Person object normally we would be writing the following code

  Person person = new Person();
  person.FirstName = “Nishant”;
  person.LastName = “Rana”;

 

However now we could do this in much simpler manner

Person person = new Person() { FirstName = “Nishant”, LastName = “Rana” };

 

Using Object initializers we can assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.

 

 

 

Extension Methods

Using Extension methods we can add methods to existing types without creating a new derived type, recompiling or modifying the original type.

Suppose this is how we have defined our Person class with one method named GetFirstName.

public class Person
    {
        public string FirstName { get;set;}
        public string LastName { get; set; }

        public string GetFirstName()
        {
            return FirstName;
        }
    }

 

Now we will add method to this existing Person Type named GetLastName

 public static class PersonExtension
    {
        public static string GetLastName(this Person p)
        {
            return p.LastName;
        }
    }

Here we need to follow these rules

  • We need to declare our class as static.
  • We need to define the method as static and the first parameter is preceded by this keyword and specifies which type the method operates on.

Now the code that calls both these methods. GetLastName function would be called as if it was an instance method and not an static one.

   Person person = new Person() { FirstName = “Nishant”, LastName = “Rana” };
   string firstName = person.GetFirstName();
   string lastName = person.GetLastName();
           
 
 

Collection Initializers

Here instead of writing the following code to intialize the collection

   List<Person> lstPerson = new List<Person>();
   Person person1 = new Person() { FirstName = “Nishant”, LastName = “Rana” };
   Person person2 = new Person() { FirstName = “Ashutosh”, LastName = “Pandey” };
            lstPerson.Add(person1);
            lstPerson.Add(person2);
we could use the following syntax, without making use of add method
 
List<Person> lstPerson=new List<Person>()
            { 
                {new Person { FirstName=“Nishant”, LastName=“Rana”}},
                {new Person {FirstName =“Ashutosh”, LastName=“Pandey”}}                
            };

 

 

Implicitly type local variables

Now we can declare variables using var keyword (implicitly) without explicitly defining their type.

 string FullName = “Nishant Rana”;
or
var FullName = “Nishant Rana”;
 
 
var myStringArray = new[] { “a”, “b”, “c” };
 
The above two lines of code are equivalent.
Here compiler infer the type of variable from the expression on the right side. It could be built-in type, user-defined type, anonymous type or any other type defined in .NET framework.
 
 

Anonymous Types

Anonymous types are class types that contain one or more public read only properties.

var v = new { FirstName = “Nishant”, LastName = “Rana” };

The above line of code creates a anonymous class having two public read only properties named FirstName and LastName.

Things to remember :-

  • The compiler gives name to the anonymous types but our application won’t be able to access it.
  • It cannot be cast to any type except “Object’”.

Anonymous methods and Lambda expressions

Suppose this is our simple delegate

 public delegate void MySimpleDelegate();

 

And this is the function which it points to

  public void MySimpleFunction()
        {
            Response.Write(“Simple function”);
        }

 

And this is how we would be using the delegate

  MySimpleDelegate mySimpleDelegate = MySimpleFunction;
   mySimpleDelegate();

 

Now here instead of defining the MySimpleFunction separately we can make use of anonymous methods

  MySimpleDelegate mySimpleDelegate = delegate
            {
                Response.Write(“Simple function”);
            };
mySimpleDelegate();

 

Infact we can also use lambda expression over here

MySimpleDelegate mySimpleDelegate = () => Response.Write(“Simple function”); 
mySimpleDelegate();
 
 
Here       ()           –>  no input parameter
and        =>          –> lamda operator
 
 
 
Optional parameters
 

We can declare an optional parameter by specifying default value as a part of declaration

public void GetTotal(int a = 0, int b = 0)
        {
            MessageBox.Show((a + b).ToString());
        }

We can call the above method in any of the following way

 
            GetTotal(1, 1);
            GetTotal(1);
            GetTotal();

 

Named Arguements

Name the arguments to be passed
            GetTotal(a: 1, b: 2);
            GetTotal(b:2, a: 1);
This will throw error named argument must appear after fixed arugments have been specified
 
           GetTotal(a:1, 2);

Dynamic Type

New dynamic type that allows us to do things that are resolved during run time.

         dynamic dString = GetName(“a”,”b”);
         dynamic dInt = 12;

 

Bye.

Microsoft LightSwitch: 15 Reasons Non-Programmers Should Try It Out – Application Development from eWeek

Check out this informative article !

Microsoft LightSwitch: 15 Reasons Non-Programmers Should Try It Out – Application Development from eWeek.

Sign an already built assembly

Hi,

These are the steps to be followed to sign an already built assembly

Open Visual Studio Command prompt.

Generate a KeyFile:

sn -k keyPair.snk

Obtain the MSIL for the provided assembly:

ildasm myAssembly.dll /out:myAssembly.il

Rename/move the original assembly

Create a new assembly from the MSIL output and your assembly

ilasm myAssembly.il /dll /key= keyPair.snk

Bye..