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...

How to read query string value in javascript

In this article am going to explain how to read query string value using JavaScript.

Use the following function to read query string value from url. You just need to pass the parameter name to this function.

Method 1:

var getQueryString = function ( field) {
    var href = window.location.href;
    var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
    var string = reg.exec(href);
    return string ? string[1] : null;
};

Method 2 :

You can also use the following javascript function to read querystring value.
function getQueryString(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}
}
}

For example assume url as http://mysite.com?myParam1=test1&myParam2=test2

Now to read the query string value from above url use the following javascript code.

var myParam1 = getQueryString('myParam1'); // returns 'test1'
var myParam2 = getQueryString('myParam2'); // returns 'test2'
var testParam = getQueryString('something'); // returns null

Note: If a parameter is present several times (?param=test1&param=test2), you will get the first value


In this way we can read query string value from url in JavaScript


For more posts on javascript visit: JavaScript


Read more...