Infolink

 

Search This Blog

Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Nov 10, 2012

UpdateProgress in Asp.Net

One of the problems with Ajax, is the fact that since it's asynchronus and in the background, the browser will not show you any status. With fast servers and fast methods, this is not a big problem, but whenever you have a method which takes up a bit of time, the user is very likely to get impatient.

Fortunately, ASP.NET AJAX solves this problem for us as well, with a nice control called UpdateProgress. It will use your own template to show that an asynchronus method is working. Have a look at the following example, which will show the control in action. It will be explained afterwards.

Oct 19, 2012

ASP.NET Trace

Trace.Write helps diagnose problems in ASP.NET. It determines what is actually running in an ASP.NET application. We review the basics of tracing in ASP.NET. We use the C# language to easily benchmark sites.

First we must add the trace markup to our Web.config file. The sample values in the markup I show can and should be changed by you. I encourage you to flip the attribute values and see what they do as much as possible. Here's how to add the markup.

Open Web.config file. Your ASP.NET project should have a Web.config file. If it doesn't, add one through the Website > Add New Item.... menu. This is an XML file used by ASP.NET to store website configuration settings.

Oct 17, 2012

C# KeyNotFoundException

A KeyNotFoundException was thrown. This is likely caused by an invalid usage of the Dictionary collection. As always we want a quick way to fix the problem.

Example

First, here we see some code that looks correct, but actually has a severe flaw. The problem is that you cannot look up a key that doesn't exist in the Dictionary and try to assign your variable to its value.

The KeyNotFoundException here is thrown on the final line of try-block. The string "test" doesn't exist in the collection.

Program that throws KeyNotFoundException [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    try
    {
        //
        // Create new Dictionary with string key of "one"
        //
        Dictionary<string, string> test = new Dictionary<string, string>();
        test.Add("one", "value");

        //
        // Try to access key of "two"
        //
        string value = test["two"];
    }
    catch (Exception ex)
    {
        //
        // An exception will be thrown.
        //
        Console.WriteLine(ex);
    }
    }
}

Output

System.Collections.Generic.KeyNotFoundException:
    The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()

Fix

Here we look at how you can fix this exception by using the TryGetValue method on the Dictionary constructed type. Note that could use ContainsKey instead of TryGetValue, but the below code preserves the intention of the previous code.

Program that does not throw [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
    Dictionary<string, string> test = new Dictionary<string, string>();
    test.Add("one", "value");
    //
    // Use TryGetValue to avoid KeyNotFoundException.
    //
    string value;
    if (test.TryGetValue("two", out value))
    {
        Console.WriteLine("Found");
    }
    else
    {
        Console.WriteLine("Not found");
    }
    }
}
Output

Not found

You always have to use the if statement when testing values in the Dictionary, because there is always a possibility that the key won't exist.

We saw how to raise and catch the KeyNotFoundException during runtime. We then saw how to avoid causing the exception. We discussed alternatives, such as TryGetValue and ContainsKey, and looked at a program that does not have this problem.

What is Entity Framework?

asfasfThe Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write. Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects. The Entity Framework’s ORM implementation provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.

Oct 15, 2012

RAISERROR statement in SQL server

RAISERROR statement is used to return error messages to the business applications that executes SQL statements. The usual errors returned by the SQL server may not make much sense to the business applications users hence we overwrite it by using RAISERROR to display meaningful messages. The statement uses same format as a system error or warning messages generated by SQL server.

You can return a user-defined error message by using RAISERROR.  The general syntax is as below

Oct 14, 2012

Creating a UserControl

we will be building a UserControl for displaying information about a community user.
First of all, let's add a UserControl to our project. In your Visual Studio or Visual Web Developer, you should be able to right click on your project and select Add new item.. A dialog will pop up, and you should select the Web User Control from the list of possible things to add. Let's call our UserControl UserInfoBoxControl, with the filename of UserInfoBoxControl.ascx. Make sure that you have checked the checkbox which places code in a separate file, the so-called CodeBehind file.

You should now have a UserInfoBoxControl.ascx and a UserInfoBoxControl.ascx.cs in your project. The first is where we put our markup, and the second is our CodeBehind file. Now, if UserInfoBoxControl.ascx is not already open and selected, do so now. You will see only one line of code, the UserControl declaration. As mentioned, this control will be displaying information about a user, so let's get started adding some markup to do so:

ASP.net Ajax

ASP.net Ajax is Microsoft's free framework for creating Ajax (Asynchronous Javascript and XML) web applications. At its simplest, it lets ASP developers do this - or add Ajax controls to their existing applications - without leaving their familiar drag-and-drop environment, and without having to understand Javascript or asynchronous communication with the host. According to the O'Reilly Network, it protects developers from "the underlying gibberish that makes it [Ajax] all work".
Related Posts Plugin for WordPress, Blogger...