Amazon Ad

Saturday, 12 July 2014

Creating Fault Injections For ASP.NET MVC Web API

Hi Guys,

I was working on a new project which was having a challenge to create fault injection in a running Web API.

Step 1 : Create a model Class name "User"

public class User
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
    }


Wednesday, 9 July 2014

Merge Git Branch

Hi guys,

Today i am going to tell you the commands to merge your branch with your live branch. Assuming that you have two branches a. Test b. Live. Where Test branch has all the development files where as "Live" branch has the data of the live website. Here is how we can merge these two branches.

1. git pull origin test
2. git checkout live (in case the branch is not in local rep. then use git checkout -b live)
3. git branch (To check which branch is current branch. The live branch will be your current branch)
4. git add .
5. git commit -m "message"
6. git status
7. git merge test (Merge the live branch with test branch)
8. git pull origin live
9. git push origin live
10. git pull origin live

In case you want to pull all the live contents on to your local repository :
git reset --hard origin/test
This tells it to fetch the commits from the remote repository, and position your working copy to the tip of its master branch. The above command will replace all the local files from "test" branch with all the files on the Github remote branch "test".

Thanks
Ritesh

Thursday, 3 July 2014

Selectize.js Dependent Dropdowns For Country And City

Hi Guys,

I was working with selectize.js and a problem occurred when i was using select/dropdown list, I was working in PhP. Here is the code for country dropdown which successfully loads city on selecting country from country dropdown.

<div class="form-group mb10">

        <div class="row">

            <div class="col-sm-6 mb5">

                <label class="control-label">Country</label>

                <div class="has-icon pull-right">

                    <?php echo CHtml::dropDownList('ClientProjects[country]','',CHtml::listData(States::model()->findAll(array('order'=>'name ASC')),'id', 'name'),array('class'=>"form-control pr10",'prompt' =>'Select Country','id'=>"country",'ajax'=>array(

'type'=>'POST',

'url' => CController::createUrl('/globaldata/getCity'),

'data'=> array('country'=>'js:this.value'),

'success'=>'function(data){loadcity1(data);}'

)

));?>

                 </div>

            </div>

How to avoid reentering of github user and password when pull or push

Hi Guys,

I was facing a problem when i was working on a project on github. The pull and push git commands were asking me my github username and password when i pushed or pulled the branch. Following are the steps :

Step 1 : Remove the already added origin if any by using this command

>git remote rm origin

Step 2 : Now we will add an origin with username and password, This will get stored in the git file and won't ask the username and password again.

>git remote add origin https://username:password@github.com/organizationname/repositorypath

where username is your github user name and password is the password of your github user. In case you cannot find the organizationname and repositorypath, You can find this by copying the https clone URL of the repository. i.e incase your your URL is "https://github.com/riteshtandon23/MyCodio.git" having username as "test" and password as "test123" then your git command will be


>git remote add origin https://test:test123@github.com/riteshtandon23/MyCodio.git

Step 3 : Give a command to pull the branch on local

>git pull origin master

It won't ask you any login or password same will happen with the push command.

> git push origin master

Hope it helped you.a

Thanks
Ritesh

Monday, 26 May 2014

jQuery ajax POST and saving value in local javascript variable


Hi Guys,

I was working on an ASP.NET page and found a strange problem. Whenever i
was calling the jquery ajax POST request, The data from webservice was
available in the success function but not after $.ajax request code. This
was strange i as wanted to access the data after calling the ajax POST
request.

Here is the problem

var localdata;
$.ajax({
data:"{'id':'1'}",
type:"POST",
dataType:"json",
contentType:"application/json",
url:"http://www.test.com/test.svc/HelloWorld",
success:function(data){localdata=data;alert("Inside Success "+localdata);},
error:function(a,b,c){}
});
alert("After ajax "+localdata);

After executing the above it gave me "Inside Success Hello", But the second
alert message gave me "After ajax undefined". This was due to the fact
that the first call was made inside success which was asynchronous, Whereas
the second alert was not asynchronous and didn't wait until the
$.ajax POST request executed.

To solve the same, I made it synchronous as i had to store the result of
webservice in a local variable, Here is the code

var localdata;
$.ajax({
data:"{'id':'1'}",
type:"POST",
dataType:"json",
        async:false, //setting this made it a synchronous call
contentType:"application/json",
url:"http://www.test.com/test.svc/HelloWorld",
success:function(data){localdata=data;alert("Inside Success "+localdata);},
error:function(a,b,c){}
});
alert("After ajax "+localdata);

Thanks
Ritesh Tandon

Thursday, 1 May 2014

Creating Asynchronous Thread in Global.asax in MVC

Hi guys,

I was facing a problem of creating asynchronous thread in Global.asax in MVC. I was able to create a thread which runs asynchrounsly without affecting the application. Here is how how i did it in global.asax.cs file. It also requires two namespaces 1. System.Threading 2. System.Threading.Tasks, Please do add them.

Step 1: Create a async Task, here in this example its MyThread.

private async Task<int> MyThread()
        {
            while (true)
            {
                string result = await Operations();
                await Task.Delay(5000);
            }
            return 1;
        }

Step 2: Create another async Task performing all the operations.

public async Task<String> Operations()
        {
            return "Hello";
        }
Step 3: Inside Application_Start call the thread.

protected async void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            AllSync();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

Thanks
Ritesh Tandon

Monday, 21 April 2014

Creating Dependent DropDownList in MVC without jQuery.



Hi Guys,

I was given a task to generate dependent values in DropDownList without using jQuery. However this approach is not recommended but incase you still want to generate the dependent dropdownlist values based upon the parent dropdownlist value, Here are the steps to do so.

In my example i am using an example of Category, SubCategory and Product. Based on the category dropdown, subcategory dropdown list is generated and the product is then saved based on the category and subcategory values. In this example i have taken both category and subcategory reference in product table, However it can be done by taking only reference of subcategory table.

Step 1: Generate models
public class Category
    {
        [Key]
        public virtual int CategoryId { get; set; }
        public virtual string CategoryName { get; set; }
        public List<SubCategory> SubCategories { get; set; }
        public List<Product> Products { get; set; }
    }

Wednesday, 16 April 2014

How to get client's MAC Address from browser


Hi Guys,

I was having problem of finding out MAC address from browser. I finally achieved the same by using help from a blog and jQuery. The current code is tested on local LAN and it gives the MAC address based on the local LAN ip address.

Here is how i achieved it


Monday, 24 March 2014

Create In Browser Compiler For C#,Java,C,C++,Python, Ruby and many more using CodeMirror and IDEOne

Hi Folks!

I was given a task to create a website which can compile the code, the code can be c#,java,c,c++,ruby,python and other languages. I found a very good API(IDEONE), really simple to implement and gives fast result. I also used codemirror, which is a great code editor for different languages and provides real time environment like keyword recognition.

I built the same in asp.net, Please follow the steps.

Tuesday, 18 March 2014

MongoDB With ASP.NET

Hi Guys,

Today i am going tell you how to use MongoDB with ASP.NET

1. Create reference of MongoDB in c#

a. Download MongoDb from http://www.mongodb.org/downloads
b. Extract the zip file into c:\mongodb folder
c. Create another folder in c: drive with name "data"
d. Inside "data" folder create a new folder with name "db".
e. go to command prompt and execute the file c:\mongodb\mongod, this will run mongodb service.
f. run another command prompt window and execute c:\mongodb\mongo
g. Some commands
    i. show dbs (To show all the databases)
    ii. use [dbname] (to switch to the mentioned database) , This also creates a new database incase its not there when a collection(table) is created.
    iii. db.users.insert({name:"test",age:"32",department:"Computer"});
   The above command will create a table "users" in the database ([dbname]) name provided by the user.
    iv. db.users.findAll()
   The above command will show all the records in users table.

Wednesday, 5 March 2014

How To Pass List<> Into Web API WebMethod

Hi,

I was struggling with passing List<> type parameter in Web API webmethod. Here is how i was able to resovle it.

1. Here is my model class

public class Users
    {
        [Key]
        public int UserId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

2. public List<Users> TestUsers([FromBody]List<Users> users)
        {
            try
            {
                return users;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

3. Here is my json which was passed using curl command, Here is the complete command

curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d "[{\"Email\":\"test@test.com\",\"FirstName\":\"Ritesh\",\"LastName\":\"Tandon\",\"UserType\":\"Normal\"},{\"Email\":\"test12@test.com\",\"FirstName\":\"Vedansh\",\"LastName\":\"Tandon\",\"UserType\":\"FB\"}]" "http://localhost:3760/api/Users/TestUsers"

This is how i was able to pass List<> type object in Web API. You can also use HttpClient class for passing json array.

Thanks
Ritesh

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...