May 28, 2013

Short Q/A's

This post will grow dynamically with the passage of time. It contains Short Q/A's related to some little queries.


Q1.  Why data is not submit on first time button click?

Ans. 
This is a normal behaviour of Asp.Net, you have to click somewhere else after entering the text before you hit the submit button. A solution is to provide instructions that user should use TAB key to enter data. That way the text in entered on hitting the TAB key and form then will be submit on first time.


Q2.  What is difference between ID and ClientID in jQuery/JavaScript?

Ans. 
ID: The ID generated in the final HTML is not guaranteed to remain the same as the one in your aspx source. When you'll put the controls inside naming containers the ID will have prepended one or several parent ids to ensure its uniqueness. 
ClientID: The ClientId property will always give you the final form of the ID attribute as it ends up in the HTML so it's always recommended to use that in your javascript.


Q3.  How to handle exception without Try Catch block?

Ans. 
You can get the exception on Application_Error method in Global.asax method. But keep in mind that it's not an alternative way you can use instead of try catch methods to handle exceptions. This will fire whenever an unhandled exception occur within your application. Other than that, you can't handle the exception here. 

void Application_Error(object sender, EventArgs e)
{
   Exception ex = Server.GetLastError();
}


Q4.  How to get last inserted ID from Sql Server?

Ans.  
     1) SELECT @@IDENTITY
     2) SELECT SCOPE_IDENTITY()
     3) SELECT IDENT_CURRENT(‘TableName’)

All of the above three will get the identity value but in different approches.


  • @@IDENTITY will return the last generated identity value produced on a connection, without based on the table that produced the value. While @@IDENTITY is limited to the current session, it is not limited to the current scope. This means that if we insert some record in Table1 which has a trigger on the insert and the trigger inserts a record in some other table2 then the @@IDENTITY will return the identity value inserted in Table2.
  • SCOPE_IDENTITY() will return the last IDENTITY value produced on a connection and by a statement in the same scope, without based on the table that produced the value. So we can say that this function is some identical to @@IDENTITY with one exception like @@IDENTITY will return the last identity value created in the current session, but it will also limit it to your current scope as well. So that means it will return identity value inserted in Table1.
  • IDENT_CURRENT will reutrn returns the last IDENTITY value produced in a table, Without based on the connection that created the value, and Without based on the scope of the statement that produced the value. IDENT_CURRENT is not limited by scope and session, so it will retrieve the last generated table identity value.


Q5.  What is difference between Override and new in OOPs?

Ans.
The override modifier extends the base class method, and the new modifier hides it. 
Override: Override concept comes when you have parent class and child class sceneriao.
new: New key word is used when you want to create an object for any class.