Interview Tips Interview Tips, Interview Questions and Answers

13Apr/110

ViewState is responsible for maintaining data across the Page Post Back

Does ViewState is responsible for maintaining data across the Page Post Back for Postback controls (like textbox, dropdownlist) and non-postbackcontrols(like label)?

It is false. For NonPostback controls it is true but for postback controls, ASP.NET retrieves their values one by one from the HTTP request and copies them to the control values while creating the HTTP response.

Compete in Cloud Code Contests, Win Cash
CloudSpokes is a new dev community, focused exclusively on cloud platforms. Come develop new skills with real world contests and win cash for winning entries. Join us!

18Mar/110

What is the use of command objects in .NET

They are used to connect connection object to Data reader or dataset. Following are the methods provided by command object:-

• ExecuteNonQuery: -

Executes the command defined in the Command Text property against the connection defined in the Connection property for a query that does not return any row (an UPDATE, DELETE, or INSERT). Returns an Integer indicating the number of rows affected by the query.

• ExecuteReader: -

Executes the command defined in the Command Text property against the connection defined in the Connection property. Returns a "reader" object that is connected to the resulting row set within the database, allowing the rows to be retrieved.

• ExecuteScalar: -

Executes the command defined in the Command Text property against the connection defined in the Connection property. Returns only single value (effectively the first column of the first row of the resulting row set any other returned columns and rows are discarded. It is fast and efficient when only a "singleton" value is required

2Mar/110

ASP.NET – HTML Server Controls

HTML server controls are HTML tags understood by the server.

HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control. The id attribute is added to identify the server control. The id reference can be used to manipulate the server control at run time.

Note: All HTML server controls must be within a <form> tag with the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts.

In the following example we declare an HtmlAnchor server control in an .aspx file. Then we manipulate the HRef attribute of the HtmlAnchor control in an event handler (an event handler is a subroutine that executes code for a given event). The Page_Load event is one of many events that ASP.NET understands:

<script runat="server">
Sub Page_Load
link1.HRef="http://www.jack-fx.com"
End Sub
</script>

<html>
<body>

<form runat="server">
<a id="link1" runat="server">Hello world</a>
</form>

</body>
</html>

The executable code itself has been moved outside the HTML.

27Feb/110

ASP.NET Validation Server Controls

CompareValidator     Compares the value of one input control to the value of another input control or to a fixed value
CustomValidator     Allows you to write a method to handle the validation of the value entered
RangeValidator     Checks that the user enters a value that falls between two values
RegularExpressionValidator     Ensures that the value of an input control matches a specified pattern
RequiredFieldValidator     Makes an input control a required field
ValidationSummary     Displays a report of all validation errors occurred in a Web page

25Dec/100

Custom Exceptions in .NET

The .NET Framework provides a rich set of system-defined exception types that can be thrown and caught by the C# developer. However, the list of available exceptions does not cover every eventuality and often it is more appropriate to define custom exceptions for specific error scenarios. This can be achieved by deriving a new exception class from any of the existing types, usually the ApplicationException class.

In the final section of this article we will define a new exception class and demonstrate how it may be thrown and handled. This will be a simple exception with no custom methods or properties. These can be added to the derived class using standard object-oriented programming techniques. However, this is beyond the scope of the C# Fundamentals tutorial and will instead be described in a future object-oriented programming tutorial.

22Dec/100

What is Exception.InnerException in .NET

When an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.

Use the InnerException property to obtain the set of exceptions that led to the current exception.

You can create a new exception that catches an earlier exception. The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.

Suppose that there is a function that reads a file and formats the data from that file. In this example, as the code tries to read the file, an IOException is thrown. The function catches the IOException and throws a FileNotFoundException. The IOException could be saved in the InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine what causes the initial error.

The InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.

15Dec/100

Minimal Exceptions Types in .NET

The absolute minimum a new custom exception class needs to have is a name. Let’s say you are designing the login mechanism for a database application and as part of this job you need to create a custom exception which is thrown if a login attempt fails. A good name for such an exception would be LoginFailedException. An absolute minimum implementation in C# then looks like:

public class LoginFailedException: System.Exception
{
}

As you can see, all you need to do to create a basic custom exception is to derive from the Exception class. There’s only one problem with this definition. Since C# unfortunately doesn’t inherit constructors of base classes, this new type only has the standard constructor with no parameters and is therefore relatively useless. So, we need to add at least one constructor which does something useful:

public class LoginFailedException: System.Exception
{
   // The default constructor needs to be defined
   // explicitly now since it would be gone otherwise.

   public LoginFailedException()
   {
   }

   public LoginFailedException(string message): base(message)
   {
   }
}
9Dec/100

Why Throw Exceptions? (.NET Topic)

During normal processing, it is possible for an error condition to be detected. In older languages the method or subroutine would be exited early and a return code would be used to indicate the error status. When developing software for the .NET framework, this method is possible but does not provide all of the flexibility of exception handling that is provided by C#. It is preferable, therefore, to raise or throw exceptions explicitly when error conditions occurs and allow exceptions to be captured by the next available try / catch / finally block or by the C# runtime system.

It is important that exceptions are thrown only when an unexpected or invalid activity occurs that prevents a method from completing its normal function. Exception handling introduces a small overhead and lowers performance so should not be used for normal program flow instead of conditional processing. It can also be difficult to maintain code that misuses exception handling in this way.