Why is OpenSubKey() returning null on my 64-bit system?


Today i was debugging the code, it was written like this


 string _retValue = "";
            Microsoft.Win32.RegistryKey _rKey;
            Object _rKeyObject;
            string REG_KEY_PATH_PROFILE = "SOFTWARE\\Wow6432Node\\Mycompany\\e-business\\Platform\\profile\\";
            try
            {
                _rKey = Microsoft.Win32.Registry.LocalMachine;
                _rKeyObject = _rKey.OpenSubKey(REG_KEY_PATH_PROFILE).GetValue(keyName);
                _retValue = Convert.ToString(_rKeyObject);
                return _retValue;
            }

It was returning null value every time, then i came to know i am building the application on 64 bit, To make the work around we can change project setup like this given below

Advertisements

Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC) (Part 3)


Method 1: Using System.Runtime.Serialization.Json Namespace

Method 2: Using System.Web.Script.Serialization Namespace

Method 3:

In this method we will use the Newtonsoft.Json dll

This is one of the most popular open source dll, we install it from Nuget package manager or from google download it and add to our project

We can write code as given below


using Newtonsoft.Json;
using System;

namespace WebApplication1
{
    public partial class Demo3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EmpDemo objEmp = new EmpDemo
            {
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"
            };

            string jsonData = JsonConvert.SerializeObject(objEmp);
            Response.Write("<b>Converting to Json </b> " + "</br>");
            Response.Write("</br>");
            Response.Write(jsonData);
            Response.Write("</br>");

            var objEmp1 = JsonConvert.DeserializeObject<EmpDemo>(jsonData);
            Response.Write("</br>");
            Response.Write("Converting Json to .Net Object:");
            Response.Write("</br>");
            Response.Write("Id: " + objEmp1.Id + "</br>");
            Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
            Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");
        }

        public class EmpDemo
        {
            public int Id { get; set; }
            public string EmpName { get; set; }
            public string EmpAddress { get; set; }
        }
    }
}

Note: donot forget to include the Newtonsoft.Json namespace in your code. This approach can be used in Asp.net or asp.net mvc application.

Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC)(Part 2)


different-ways-of-doing-serialization-and-deserialization-in-net-asp-netasp-net-mvc (Part 1)

Method 2 approach

we will use System.Web.Script.Serialization namespace of .net framework

Step 1: Create the EmpDemo Class and write the code in code behind file like given below


using System;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    public partial class Demo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EmpDemo objEmp = new EmpDemo
            {
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"
            };

            JavaScriptSerializer js = new JavaScriptSerializer();

            Response.Write("<b>Converting to Json </b> " + "</br>");
            Response.Write("</br>");
            string jsonData = js.Serialize(objEmp);
            Response.Write(jsonData);
            Response.Write("</br>");

            var objEmp1 = js.Deserialize(jsonData);
            Response.Write("</br>");
            Response.Write("<b> Converting Json to .Net Object: </b>");
            Response.Write("</br>");
            Response.Write("Id: " + objEmp1.Id + "</br>");
            Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
            Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");
        }
    }

    public class EmpDemo
    {
        public int Id { get; set; }
        public string EmpName { get; set; }
        public string EmpAddress { get; set; }
    }
}

Summary: This approach will be suitable in asp.net or asp.net mvc application.

Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC)(Part1)


Nowadays so many times we will get requirement to do serialization and deserialization process in .net application.
Firstly we will know what this process is,

Serialization: It is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.

Deserialization: It is the reverse process of Serialization.

In asp.net we can achieve it using so many ways

1. Using System.Runtime.Serialization.Json Namespace

2. Using System.Web.Script.Serialization; Namespace

3. Using Newtonsoft.Json; Open Source dll

Method 1 : using System.Runtime.Serialization.Json Namespace

In approach we will firstly create the C# object then convert into Json object and revert back to its original state

Step1: Create the C# Class like this and use System.Runtime.Serialization namespace as given below

using System.Runtime.Serialization;

namespace WebApplication1
{
    [DataContract]
    public class Emp
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string EmpName { get; set; }

        [DataMember]
        public string EmpAddress { get; set; }
    }
}

Step 2:

write the code in code behind file of asp.net as given below

using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace WebApplication1
{
    public partial class Demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnMethod1_Click(object sender, EventArgs e)
        {
            //Serilization process using  System.Runtime.Serialization

            Emp objEmp = new Emp()
            {
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"
            };

            DataContractJsonSerializer objJS = new DataContractJsonSerializer(typeof(Emp));
            string json = string.Empty;
            using (MemoryStream objMS = new MemoryStream())
            {
                objJS.WriteObject(objMS, objEmp);
                objMS.Position = 0;
                using (StreamReader sr = new StreamReader(objMS))
                {
                    json = sr.ReadToEnd();
                    Response.Write("<b>Converting to Json </b> " + "</br>");
                    Response.Write("</br>");
                    Response.Write(json);
                    sr.Close();
                    objMS.Close();
                }
            }

            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                Emp objEmp1 = (Emp)objJS.ReadObject(ms);
                Response.Write("</br>");
                Response.Write("</br>");
                Response.Write("<b>Converting Json to .Net Object</b>");
                Response.Write("</br>");
                Response.Write("</br>");
                Response.Write("Id: " + objEmp1.Id + "</br>");
                Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
                Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");
            }
        }
    }
}

Step 3: Now run the application you will see the process of serialization and deserialization.

Summary: This approach will be more suitable in WCF application.

How to show the active file in Visual Studio?


While working on big project we will navigate so many file of solution explore, but if we have to know where am in this solution explorer, then it will be difficult to find out.

But if we will change the setting in Visual studio then it will be very easy to find out the exact file path

Tools – Options – Projects and Solutions – Track Active Item

What are the difference between Object, Var and dynamic keyword in C# ?


When I was giving the interview I was getting this question in all interview. So I am sharing this short answer for everyone.

This all keywords looks like used for same purpose and there are some differences in each one.

Overview of Nunit Testing Framework


what is the Nunit ?

If you are new to Nunit, please go through this post How to configure Nunit in Visual Studio

It is the unit test case framework based on .Net Framework. Which is widely used by .net developer to write the unit test case. It is open source and free to use by any person. You don’t have to pay any money for this tool.

why should we use Nunit framework instead of MS-UnitTest Framework ?

These are the few important point to use Nunit instead of MS-Test

1. It is matured and stable unit test case framework
2. It gives good performance as compare to MS-Test
3. It has good community support
4. It is the popular Open Source project
5. Suite attribute - can aggregate tests and execute them separately. It will be useful for large projects.
6. NUnit has frequent version updates - MS-Test has only one per VS version.
7. It takes less memory to execute the test case as compare to MS-Test.

what are the important attributes of Nunit Framework?

There are following important attributes of NUnit Framework
a> TestFixtureAttribute

>>It is used to indicate the class which contains the test method.

b> Description

>> as the name suggest, it gives the description of class or test method

using System;
  using NUnit.Framework;

  [TestFixture, Description("Fixture description here")]
  public class SomeTests
  {
    [Test, Description("Test description here")] 
    public void TestMethod()
    { /* ... */ }
  }
}

c. SetUP
>> It is generally used for declaration of object before running the unit test case.

d. TearDown : It is generally used for cleaning the object at end of the test case.

d. Test : This attribute generally keep on top of method name. It is used for writing the unit test case related code for check the expected functionality of application.

using System;
  using NUnit.Framework;

  [TestFixture]
  public class SuccessTests
  {
    [SetUp] 
    public void Init()
    { /* ... */ }

    [TearDown]
     public void Cleanup()
    { /* ... */ }

    [Test] 
    public void Add()
    { /* ...... */ }

There are so many other attribute available in Nunit. If you are looking to learn more about it. Please check the below URL

Nunit Attributes document URL