Tuesday, March 14, 2017

Difference Between Get and POST

Difference between "Get”, "POST". HTTP stands for Hypertext Transfer Protocol and used for communication from client to server - and vice versa. While working on web development, in daily routine, you must play with HTTP methods it may be Get or Post. Between web and server, a browser will be the client and our hosted application will be server. For Example, when a browser sends an HTTP request to the application(server), in response our application/server returns the status of the request and may have some content also. 

GET: mostly a GET is used to get or retrieve the data from the server. it should not have any side effects. In GET we must embed our data in the URI of request that is the only way. before performing any operation on the data from GET we should first verify. GET request can also be cached by browser and remain in browser history. So, avoid using GET Request when performing some sensitive data operations.
syntax for GET request:

$.get(URL,callback);

Simple call to GET data and alert:

$.get("tutorial_test.aspx", function(data, current_status){
        alert("Here Data: " + data + "\nStatus of Request: " + current_status);
    });

We can set other parameters like: cache parameter false when using ajax GET request like:

$.ajax({ type: "GET", url: "http://domain.com/returnData.html", cache: false, success: function(data){ $("#divResults").append(data); } });

POST: POST method is used to send data to the server. normally in WebApi or Web Services POST is used to insert some record or bunch of data to the database or any file. In POST request, we specify that which type of data we are going to send to the server and then server process the data accordingly. POST request response is not cached by the browser because our POST may have some side effect to the server. POST request should have HTTP Request Body having data to be submitted and in response we can also have response body.
syntax for POST request:

$.post(URL,data,callback);

First and important parameter is URL we want to send request. Secondly, we send our data object which contains data to be processed. In last we have a callback function through which we can check the response and status of the request. Now let have a little example with data, how we will pass data to POST request:

$.post("tutorial_test_post.aspx",
    {
        name: "Final Coding",
        city: "Dubai"
    },
    function(data, Currstatus){
        alert("Response Data: " + data + "\nStatus Received is : " + status);
    });

Monday, March 13, 2017

Turn off Custom Errors in Web config Asp .Net

Custom Error mode off is very common error when you are new to asp.net development. after deploying web to your server you may experience unexpected error Runtime Error to see detail turn off the customErrors in web.config file. in below image we have show the error snapshot.

This type of errors can occur anytime due to some reason it may be there is an extra semicolon in your code but you can not see the details of error whats happening until you set customerrors mode=off in your application web.config file. after setting CustomErros mode to off we can easily see on the web page whats problem and where it occurs which line number and page also.
Basically in Asp.net we have three customErrors mode to check the details and trace the exact problem. these modes tell the browser that it should show full details of errors or not or what message will be shown instead.
Off Mode:

This is used to show the error message details either you are running your application on local or server.

On Mode:

In case of error mostly website show their own 404 page and some suitable error message. when customErrors mode is on then we can set a customError page to display in case of error.
RemoteOnly: RemotelyOnly is the default mode also and it is used to display error message on a remote server only not on local.

What is Web.Config File?

Web.config is a configuration file which contains application's settings/configurations. For a single application only one web.config file is used but if we have to set some configuration for a specific directory then we can make different config file for that directory. For a separate web.config file we have to create same file and we can put some redirection rules for that directory or we can set compression, etc. basically web.config file is xml based which have different tags for particular part of application. we can store database info which includes connections, Set the session expiry time, enable compression for whole application, put Error Handling rules and Security etc. below given some tag of web.config file.

<configuration>
  <configSections>
  </configSections>

  <appSettings>
  </appSettings>

  <connectionStrings>
    <add key="ConnectionName" value="remoteserver=127.1.1.1;uid=sa;pwd=****;database=TestDb" />
  </connectionStrings>

</configuration >

User Controls VS Custom Controls

Today in this tutorials we will learn about user controls and custom controls. So let's start from user controls.

User Controls:-

A user control also can be used a normal server control. Basically user controls are made by user as per their requirements. If a user need to show a news section on all page and should be same on all pages then we can make a user control and put this on all pages. If there is a change in control user will change in control file and it will be replicated to all pages.it is also a best example of code re-usability.

In Asp.net we have System.Web.UI.UserControl class which is used to create user controls.

User control extension is .ascx. A user control cannot have HTML, body or form tags and at the top there is control directive instead of page as used in normal asp.net pages.

Custom Controls:-

Customer control are basically written by user or derived from any third party. Customer control becomes an individual assembly when deployed and after compilation it becomes a DLL (Dynamic Link Library). These can be used as other asp.net server controls. These are used in asp.net web forms and a custom client control used in asp win forms applications. There are different ways to create custom control like:
  1. Derive a customer control from asp.net control
  2. We can compose 2 or 3 asp.net control to derive a new custom control
  3. We can make custom control from base control class of asp.net

Foreach Loop in C#

In this tutorial we are going to learn about foreach loop in c#. So let's start learning
  1. In for loop we use initialization, termination and increment or decrement to loop over the data but in foreach loop we are not required any initialization, condition or increment and decrement. It is a looping constructs which ignore/remove the errors which occur due to index handling.
  2. Mainly it performs its operation on collections like Array, Lists etc. and process inside values one by one.
  3. There are many ways to exit or stop the loop like if we want to break the loop we can use break keyword, if want to move on next iteration continue keyword, to exit the loop we can use return, throw or goto statement.
Syntax:-

foreach(string emp_id in employees)
{

}
Example:-

string[] employees = new string[3]; // declare the array 
    //Add data to array of employees
    array[0] = "Doe-1452";
    array[1] = "Joh-2365";
    array[2] = "Gen-2589"; 
    //show data using foreach
    foreach (string emp_id in employees)
    {
        Console.WriteLine(emp_id + " Entered to Office");
    }
    //Output
    Doe-1452 Entered to Office
    Joh-2365 Entered to Office
    Gen-2589 Entered to Office