javascript: typeerror (intermediate value) is not a constructor

It's been long time since i have written a blog post as i am busy with the project deliverable. So, here i am with one more issue which i faced today while writing some basic javascript code.

The error is 

javascript: typeerror (intermediate value) is not a constructor

i came across this issue when i am returning some object from a function. Following is the similar javascript code that i have written

function buildContact() {
        return new { name: "John", mobile: "123-456-7890", country: "US" };
    } 

this is the javascript function which was showing the mentioned error. 

Cause:

I used new which can be only used with a Function as operand. In this case i am using object as a constructor which is actually not. 

Fix:

Here we have two options to fix this error. 

1. To return object directly as shown below
function buildContact() {
        return { name: "John", mobile: "123-456-7890", country: "US" };
    }      

2. Create a constructor and use the constructor to create new object as shown below

function Contact(name, mobile, country) {
        this.name = name;
        this.mobile = mobile;
        this.country = country;
    }

    function buildContact() {
        return new Contact("John", "123-456-7890", "US");
    }

in this case we are returning the new Contact() by using constructor. 

In this way you can fix the error javascript: typeerror (intermediate value) is not a constructor.

Please do comment if you know better way to fix this error. Thanks in advance. 

To know more common javascript errors visit: Javascript 


Read more...

Add new data row to data table Error: 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

We can not directly create a new DataRow() . When we try to do so, it throws this exception

System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

So in order to create new row in data table use the following method

DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(row);
reference link : https://msdn.microsoft.com/fr-fr/library/9yfsd47w.aspx

In this way you can create a new data row and add it to data table.
Read more...

GIT Error : Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches

Today i came across a weird issue when i was trying to switch from my stage branch to main branch. Visual Studio was showing following error:

Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details.


Visual studio states that we cannot switch to other branch when there are uncommitted changes. But in my case i don't have any pending/uncommitted changes. So, as suggested in the error message i had a look at the output window.
Following is the error shown in my output window.

It states that there are some changes in the ResourcesController.cs file. But i din't have any changes in that file.

Fix for the Issue:

So, to fix the issue i just deleted the above file and did undo (From Team Explorer) to add it again. I tried to switch branch now and it WORKED!!
This way i solved the error "cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details."
This is the solution which worked for me. But, it may not be the ideal solution and no guarantee that it works for all. But it worked for most of the people.

Update (Fix 2):

If the above solution did not work, try to change the branch directly from Git Extensions. Just open the Git Extensions and select the branch from branch drop down.


If you find any other way to fix this problem please do comment below so that it may help others.
Read more...

How to call Asp.Net MVC controller action method using JQuery

In this article i  am going to explain how to call controller actions using JQuery in Asp.Net MVC.

Get data from MVC Controller action method:


You can either use $.get or $.ajax functions to call mvc controller action methods using Jquery.

Example:

Assume you have following mvc controller and action method.

public class EmployeeController : Controller
    {
        public string GetEmployee()
        {
            var emp = new Employee() { Id = 1, Name = "John", Designation = "SE" };
            return Newtonsoft.Json.JsonConvert.SerializeObject(emp);
        }
    }

Above Employee Controller has one action method i.e., GetEmployee() which is returning serialized Employee data.

Now use the following JQuery code to call the above mvc action method

Method 1: Using $.get()

$.get("/Employee/GetEmployee", function (data) {
        var employee = JSON.parse(data);
        alert(employee.Name);
    });

Method 2: Using $.ajax()

$.ajax({
        url: "/Employee/GetEmployee",
        success: function (data) {
            var employee = JSON.parse(data);
            alert(employee.Name);
        }
    });

Note: I am using JSON.parse because controller action is returning serialized data. So we have to parse it to convert that to normal object.

Post data to MVC controller action methods using JQuery:


Using POST we can submit the data to controller actions.

Assume you have following controller action which saves the submitted data to database.

[HttpPost]
public bool SaveEmployee(Employee employee)
        {
            //your code to save employee to database
            //return true if data saved succesfully
         
            return true;
        }

Now use the following JQuery code to post data to controller action method.

Method 1: Using $.post()

//create the employee object
var employee = { Name: "Dave", Id: 2, Designation: "Sales" };

    $.post("Employee/SaveEmployee", employee, function (data) {
        if (data == "True") { //change condition based on your return type
            alert("Employee Saved succesfully");
        }
    });

Method 2: Using $.ajax()

//create the employee object
var employee = { Name: "Dave", Id: 2, Designation: "Sales" };

    $.ajax({
        url: "/Employee/SaveEmployee",
        type: "POST",
        data: employee,
        success: function (data) {
            if(data == "True") //change condition based on your return type
            alert("Employee Saved");
        }
    });

In this way we call Asp.Net MVC controller actions using JQuery.

For more posts on JQuery Visit: JQuery


Read more...

How to make Ajax call with JQuery

In this article am going to explain how to make ajax call with JQuery.

JQuery has $.ajax() method which is used to perform an asynchronous HTTP request.

The syntax of $.ajax() function is as follows

$.ajax(url[, options])

url is a string parameter that you want to reach with AJAX call while options is an object literal containing the configuration for the Ajax request.

JQuery AJAX example:

$.ajax({
  url: "demo.txt",
  success: function(result){
    alert(result);
  }
});

JQuery AJAX post example:

$.ajax({
  type: "POST",
  url: url,
  data: {name:"ranadheer",id:2},
  success: function(data, status,, jQxhr){
     alert(data);
  },
  error: function(jQxhr, textStatus){
     alert(error);
  }
  dataType: dataType
});


This way we can make AJAX calls with JQuery.

For more posts on JQuery visit: JQuery

Read more...