Sunday, December 18, 2011

Difference between TRUNCATE, DELETE and DROP commands


DELETE


The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.


 DELETE FROM emp;



TRUNCATE


TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.Truncate is also delete the records but it is not delete the table space which is created by the data base.


TRUNCATE TABLE emp;


DROP


The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back.We can not recover the table before Oracle 10g. But Oracle 10g provide the command to recover it by using the command (FLASHBACK)
Drop command will delete the entire row also the structure.But truncate will delete the contenets only not the strucure, so no need to give specifications for another table creation.



 DROP TABLE emp;



Friday, December 16, 2011

Comparing ListView with GridView,DataList and Repeater




Supported Funcationalities
Control Paging Data Grouping Provide Flexible Layout Update,Delete Insert Sorting
ListView supported supported supported supported supported supported
GridView supported Not supported Not Supported supported Not Supported supported
DataList Not supported supported supported Not supported Not supported Not supported
Repeater Not supported Not supported supported Not supported Not supported Not supported






Control - Capabilities

GridView - Read/Edit
DataList - Read/Edit
Repeater - Read Only
DetailsView - Read/Edit/Create
FormView - Read/Edit/Create


Feature                Repeater  DataList       Grid View
Table layout         No           No                Yes
Style properties    No           Yes             Yes   




factorial logic


j=1;  

    for (i=1; i<=VALUE; i++)    

        j=j*i;  

    printf("The factorial of %d is %d\n",VALUE,j);

how to find 2nd highest salary



select Max(Salary) from tblSalaryDetails  where salary   Not IN(select Max(Salary) from tblSalaryDetails )


for 6 ,7 and other

select max(salary) from EmployeeDetails

where Salary NOT IN
(
SELECT TOP 5 (SALARY) from EmployeeDetails ORDER BY Salary Desc)

SQL - Difference between Truncate & Delete

1. TRUNCATE is a DDL (Data Definition Language) command and DELETE is a DML (Data Manipulation Language) command.

2. You can use WHERE clause with DELETE but not with TRUNCATE .

3. You cannot rollback data in TRUNCATE but in DELETE it is possible to rollback data.

4. A trigger doesn't get fired in case of TRUNCATE whereas Triggers get fired in case of a DELETE command.

5. TRUNCATE is faster than DELETE. TRUNCATE is faster than DELETE due to the way TRUNCATE "removes" rows. Actually, TRUNCATE does not remove data, but rather deallocates whole data pages and removes pointers to indexes. The data still exists until it is overwritten or the database is shrunk. This action does not require a lot of resources and is therefore very fast.

6. TRUNCATE resets the Identity counter if there is any identity column present in the table where DELETE does not reset the identity counter.

7. You cannot TRUNCATE a table that has any foreign key constraints. You will have to remove the contraints, TRUNCATE the table, and reapply the contraints.

8. DELETE and TRUNCATE operations are both logged. DELETE is a logged operation on a per row basis and TRUNCATE command logs the deallocation of the data

Wednesday, December 7, 2011

Calling web service using javascript.

Many a times there was this pressing need where I had to call a web service method and get the output and use it in javascript. There were many options available like making use of javascript to call web service method or using some third party javascript library or by making use of webservice behavior (HTML Component (HTC) file) or making use of AJAX and calling an ASPX page which in turn calls a webservice in the code behind and returns the output as XML or if you are using ASP.NET 2.0 then you can make use of “ScriptManager”. We will see all the above ways of invoking a webservice one by one except making use of third party javascript library. As there are many third party javascript libraries available in the internet so we will not concentrate on any as each have different ways of implementation.  Lets see each of the methods in action one by one.
Using javascript to invoke web service methods
One of the simplest and easiest way is to invoke web service methods is by making use of “XMLHttpRequest” objects in javascript. Before invoking the web service method using javascript lets see the web service methods which we are going to be invoked using javascript. Below are two sample methods which we are going to be invoked using javascript.
public class TestService : System.Web.Services.WebService
{
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string CombineName(string firstName, string midleName, string lastName)
        {
            return string.Concat(string.Concat(firstName, " - ", midleName), " - ", lastName);
        }
}
The above two methods are pretty straight forward, one returns a string and the other takes three string arguments and combines them and returns as a string. We will see how to call a web service method without parameters and with parameters. Lets first see how to call the “HelloWorld” web method which doesn’t take any parameter. Below is the javascript code for invoking the “HelloWorld” web method.
function webServiceCallWithoutParameters()
{
        var xmlHttpReq = new XMLHttpRequest();
        xmlHttpReq.open("POST", "http://localhost/TestService.asmx/HelloWorld", false);       
        xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpReq.send();       
        alert(xmlHttpReq.responseXML.childNodes[1].text);
}
In the above javascript function we are making use of “XMLHttpRequest” object to invoke web service method. We are making use of the “open” method which takes three arguments, first argument says whether the method used is “POST” or “GET”, second argument is the url for the web service and the third argument is to specify whether the “XMLHttpRequest” should be synchronous or asynchronous. The url for a .NET web method always ends with the name of the web method preceded by the url of the web service as shown in the above e.g. Once we have set the url and the method used to call the web service, in the next line we are setting the “Content-Type” for the request using the “setReqeustHeader” method of the “XMLHttpReqeust” object. Its very important to set the “Content-Type” of the request to “application/x-www-form-urlencoded” when you are trying to invoke a web service method, if the “Content-Type” is not set to “application/x-www-form-urlencoded” the following error will be thrown when you try to pass arguments to a web service method.
status: 500
statusText: Internal Server Error
responseText: System.InvalidOperationException: Request format is invalid: . at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
The status property of the “XMLHttpReqeust” object will have a value of 500, the “statusText” will be having “Internal Server Error” as the message and the “responseText” will have the type of error. To solve the problem set the “Content-Type” for the request as shown in the above e.g.
Now let’ see how to pass parameters to a web method.
var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "http://localhost:3150/TestService.asmx/CombineName", false);
xmlHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttpReq.send("firstName=Sandeep&midleName=mid&lastName=p r");
alert(xmlHttpReq.responseXML);
In the above javascript function we are invoking the “CombineName” web method which takes three parameters. Everything else is the same as the previous javascript function except for the “send” method of the “XMLHttpReqeust” object. In the “send” method we are passing the parameters separated by the ampersand (&) symbol. To pass parameter to a web method you can use the “send” method of the “XMLHttpRequest” object. The parameters and their values should be separated by the “equal to” (=) symbol and each set of parameters should be separated by the ampersand symbol.
After this the next obvious question would be how to send complex objects to a web methods. There is no straight forward way of doing this, one can make use of Javascript Object Notations (JSON). You can find lots of e.g. on how to pass JSON over the net. Also you can use “WebService” behavior (HTC file) to send and receive complex objects or one can use “ScriptManager” to send and receive complex objects. We will see “HTC file” and “ScriptManager” usage in subsequent blogs. Receiving a complex object is pretty easy. When you invoke a web method and if that web method returns a complex object then the object will be serialized into a XML and the XML can be accessed through the “responseText” or “responseXML” property of the “XMLHttpRequest” object. Lets see with an e.g. Below is the web method code which returns a “Car” complex object.
[WebMethod]
public Car GetCarObject()
{
     return new Car { Model = "Ferrari", Color = "Ferrari Red", CC = 2555 };
}
//Car Class is pasted below.
public class Car
{
    public string Model
    {
        get;   set;
    }
    public int CC
    {
        get;   set;
    }
    public string Color
    {
        get;   set;
    }
}
The above web method is pretty straight forward. It creates an instance of a “Car” class and returns the same. The “Car” class is also pasted. The javascript method to invoke the above web method which returns “Car” complex object is pasted below.
function getComplexObject()
{
    var xmlHttpReq = new XMLHttpRequest();
    xmlHttpReq.open("POST", "http://localhost:3150/TestService.asmx/GetCarObject", false);
    xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttpReq.send();
    alert(xmlHttpReq.responseText);
}
The output returned in the form of the XML is pasted below.

http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  Ferrari
  2555
  Ferrari Red
As you can see in the above XML the complex object returned by the web method is serialized as XML. That's about invoking web methods using javascript

Friday, December 2, 2011

how set startup controller in mvc3

 In global.asax
 
routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home" , action = "Index",
            id = UrlParameter.Optional } // Parameter defaults
        );

How to set startup page for debugging in asp.net mvc aplication?

Go to your project's properties and set the start page property.
  1. Go to the project's Properties
  2. Go to the Web tab
  3. Select the Specific Page radio button
  4. Type in the desired url in the Specific Page text box
  





Thursday, December 1, 2011

New HTML 5 input types in ASP.Net 4.5 Developer Preview

Microsoft has released developer previews for Visual Studio 2011 and .Net framework 4.5. There are lots of new features available in the developer preview. One of the most interested things for web developers is the support introduced for new HTML 5 form controls.
The following are the list of new controls available in HTML 5
  • email
  • url
  • number
  • range
  • Date pickers (date, month, week, time, datetime, datetime-local)
  • search
  • color
Describing the functionality for these controls is not in the scope of this article. If you want to know about these controls, refer the below URLs
http://msdn.microsoft.com/en-us/magazine/hh547102.aspx
http://www.w3schools.com/html5/html5_form_input_types.asp
ASP.Net 4.5 introduced more possible values to the Text Mode attribute to cater the above requirements. Let us evaluate these. I have created a project in Visual Studio 2011 developer preview, and created a page named “controls.aspx”. In the page I placed on Text box control from the toolbox
clip_image001[4]
Now select the control and go to the properties pane, look at the TextMode attribute.
clip_image002[4]
Now you can see more options are added here than prior versions of ASP.Net. I just selected Email as TextMode. I added one button to submit my page. The screen shot of the page in Visual Studio 2011 designer is as follows
clip_image003[4]
See the corresponding markup


   

        Enter your email:
           

   

Now let me run this page, IE 9 do not have the support for new form fields. I browsed the page using Firefox and the page appears as below.
clip_image004[4]
From the source of the rendered page, I saw the below markup for my email textbox
Try to enter an invalid email and you will see the browser will ask you to enter a valid one by default.
clip_image005[4]
When rendered in non-supported browsers, these fields are behaving just as normal text boxes. So make sure you are using validation controls with these fields.
See the browser support compatability matrix with these controls with various browser vendors.
clip_image006[4]
ASP.Net 4.5 introduced the support for these new form controls. You can build interactive forms using the newly added controls, keeping in mind that you need to validate the data for non-supported browsers.