Filtering Data in Web Forms Model Binding
To enable filtering, we’ll update our GetProducts() method to take a keyword parameter like below:
public IQueryable<Product> GetProducts([QueryString]string keyword)
{
IQueryable<Product> query = db.Products;
if (!String.IsNullOrWhiteSpace(keyword))
{
query = query.Where(p => p.ProductName.Contains(keyword));
}
return query;
}
If the keyword is empty or null, then the method returns all of the products in the database. If a keyword is specified then we further filter the query results to only include those product’s whose names contain the keyword.
Understanding Value Providers
One of the things you might have noticed with the code above is the [QueryString] attribute that we’ve applied to the keyword method argument. This instructs the Model Binding system to attempt to “bind” a value from the query string to this parameter at runtime, including doing any type conversion required.
These sources of user values are called “Value Providers”, and the parameter attributes used to instruct the Model Binding system which Value Provider to use are called “Value Provider Attributes”. The next version of Web Forms will ship with value providers and corresponding attributes for all the common sources of user input in a Web Forms application, e.g. query string, cookies, form values, controls, viewstate, session and profile. You can also write your own custom value providers. The value providers you write can be authored to work in both Web Forms and MVC.
By default, the parameter name will be used as the key in the value provider collection, so in this case, a value will be looked for in the query string with the key “keyword”, e.g. ~/Products.aspx?keyword=chef. This can be easily overridden by passing the desired key in as an argument to the parameter attribute. In our example, we might like the user to be able to use the common “q” query string key for specifying the keyword:
public IQueryable<Product> GetProducts([QueryString("q")]string keyword)
Events and Delegates in c#
1. What’s a delegate?
A delegate object encapsulates a reference to a method.
2. What’s a multicast delegate?
A delegate that has multiple handlers assigned to it. Each assigned handler (method) is called.
3. What’s the implicit name of the parameter that gets passed into the class’ set method?
Value, and it’s datatype depends on whatever variable we’re changing.
4. How do you inherit from a class in C#?
Place a colon and then the name of the base class.
5. Does C# support multiple inheritance?
No, use interfaces instead.
6. When you inherit a protected class-level variable, who is it available to?
Classes in the same namespace.
7. Are private class-level variables inherited?
Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited.
8. Describe the accessibility modifier protected internal.?
It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).
9. C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?
Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.
10. What’s the top .NET class that everything is derived from?
System.Object.
COM Interop in c# 4.0
// Code simplified for this example
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;
object foo = "MyFile.txt";
object bar = Missing.Value;
object optional = Missing.Value;
Document doc = (Document)Application.GetDocument(ref foo, ref bar, ref optional);
doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional);
There are (at least) three problems with the code above. First, you have to declare all your variables as objects and pass them with the ref keyword. Second, you can't omit parameters and must also pass the Missing.Value even if you are not using the parameter. And third, behind the scenes, you are using huge (in file size) interop assemblies just to make one method call.
C# 4.0 will allow you to write the code above in a much simpler form that ends up looking almost exactly like 'normal' C# code. This is accomplished by using some of the features already discussed; namely dynamic support and optional parameters.
// Again, simplified for example.
using Microsoft.Office.Interop.Word;
var doc = Application.GetDocument("MyFile.txt");
doc.CheckSpelling();
asp.net interview questions and answers
How do we do paging in gridview?
Make Allowpaging=true also specify the PageIndex as some value
Namespace and Assembly?
Namespace is a logical design-time naming convenience, whereas an assembly establishes the name scope for types at run time.
Diff b/w dataset.clone and dataset.copy
dataset.clone copies just the structure of dataset (including all the datatables, schemas, relations and constraints.); however it doesn’t copy the data. On the other hand dataset.copy, copies both the dataset structure and the data.
Diff b/w Const and readonly?
A const can not be static, while readonly can be static.
A const need to be declared and initialized at declaration only, while a readonly can be initialized at declaration or by the code in the constructor.
A const’s value is evaluated at design time, while a readonly’s value is evaluated at runtime.
Diversities between static or dynamic assemblies ?
Assemblies can be static or dynamic.
Static assemblies can include .NET Framework types (interfaces and classes), as well as resources for the assembly (bitmaps, JPEG files, resource files, and so on).
Static assemblies are stored on disk in portable executable (PE) files.
You can also use the .NET Framework to create dynamic assemblies, which are run directly from memory and are not saved to disk before execution.You can save dynamic assemblies to disk after they have executed.
How can you show the number of visitors of your app
By havving an application variable and Incrementing it in every session start.
TO Achieve it use the Global.asax file's Application_Start and Session_Start Events.
C# interview questions with answers
What are the different method parameter modifiers in C#?
out
ref
params
What are the different Iteration Constructs in C#?
for loop
foreach/in loop
while loop
do/while loop
What is the use of ?? operator in C#?
This operator allows you to assign a value to a nullable type if the retrieved value is in fact null.
What is the CIL representation of implicit and explicit keywords in C#?
The CIL representation is op_Implicit and op_Explicit respectively.
Which operators in C# provides automatic detection of arithmetic overflow and underflow conditions?
'checked' and 'unchecked' keywords provide automatic detection of arithmetic overflow and underflow conditions.
What is the use of stackalloc keyword in C#?
In an unsafe context it is used to allocate C# array directly on the stack.
Which keyword in C# is used to temporarily fix a variable so that its address may be found?
fixed keyword
What are the different C# preprocessor directives?
#region , #endregion :- Used to mark sections of code that can be collapsed.
#define , #undef :-Used to define and undefine conditional compilation symbols.
#if , #elif , #else , #endif :- These are used to conditionally skip sections of source code.
What is the use of GetInvocationList() in C# delegates?
GetInvocationList() returns an array of System.Delegate types, each representing a particular method that may be invoked.
C# delegate keyword is derived form which namespace?
C# delegate keyword is derived form System.MulticastDelegate.
What will be the length of string type variable which is just declared but not assigned any value?
As variable is not initialized and it is of reference type. So it's length will be null and it will throw a run time exception of "System.NullReferenceException" type, if used.