Wednesday, March 2, 2011

Transact SQL Constraints

1. Primary Key


Primary keys are the unique identifiers for each row. They must contain unique values. A primary key column can’t contain NULL values. A table can have a maximum of one primary key.

2. Unique Key


You can only have a single primary key defined on a table. If you wish to enforce uniqueness on other non-primary key columns, you can use a UNIQUE constraint. A unique constraint, by definition, creates an alternate key. Unlike a PRIMARY KEY constraint, you can create multiple UNIQUE constraints for a single table and are also allowed to designate a UNIQUE constraint for columns that allow NULL values.

3. Foreign Key


Foreign key constraints establish and enforce relationships between tables and help maintain referential integrity, which means that every value in the foreign key column must exist in the corresponding column for the referenced table. Each foreign key is defined using the FOREIGN KEY clause combined with the REFERENCES clause.

4. Check Constraints


The CHECK constraint is used to define what format and values are allowed for a column.

5. Default constraints


A default constraint specifies a value to be applied to a column whenever a value is not supplied in an INSERT.





Tuesday, March 1, 2011

Introduction to SQL Server Schemas

Schemas


A schema is nothing more than a named, logical container in which you can create database objects.

A schema is a collection of database objects that is owned by a single person and forms a single namespace.

As of SQL Server 2005 and 2008, users are separated from direct ownership of a database object (such as tables, views, and stored procedures). This separation is achieved by the use of schemas, which are basically containers for database objects. Instead of having a direct object owner, the object is contained within a schema, and that schema is then owned by a user.

One or more users can own a schema or use it as their default schema for creating objects.

CREATE SCHEMA

CREATE SCHEMA schema_name [AUTHORIZATION owner_name ]

Schema name is the name for schema and owner name is database user name.

Eg:

CREATE SCHEMA Teacher 


AUTHORIZATION teacher



CREATE SCHEMA Student

AUTHORIZATION student



Friday, November 12, 2010

Solving Request.Files.Count is 0 in mvc

Solving problem while uploading files in ASP.NET MVC 2

You should set both name and id attributes for fileupload control.

You should set enctype attribute of form element into multipart/form-data

Try your self uploading file.

See the following Example for more details

<form method="post" enctype="multipart/form-data" action="MyController/Upload">
    <input type="file" class="file" id="File1" name="File1" />
    <input type="submit" id="Submit1" class="btn80" value="Upload" />
</form>



public ActionResult Upload()
{
    foreach (string upload in Request.Files)
    {
        var file = Request.Files[upload];
        string filePath = "C:\\Temp\\" + DateTime.Now.Ticks.ToString() + file.FileName;
        file.SaveAs(filePath);
                  
    }
    return View();
}

Thursday, October 28, 2010

Javascript callback with parameter

Javascript delegates with parameter:

Here is one example for calling a javascript function using a delegate ( function pointer ) .

While calling a method using delegate, you should use the first parameter as 'this' (or an object), after that you can pass additional parameters.

For more details see the following example.

Eg:

pass a function as parameter.

do_AjaxPostWithData(url,function (str) { });


the following function calls the delegate with a parameter.
function do_AjaxPostWithData(actionUrl, callback) {

callback.call(
 
this,context.responseText);

}

Wednesday, September 29, 2010

Defining Structural Contracts

Defining Structural Contracts
Data Contracts:

Data contracts are the contractual agreement about the format and structure of the payload data (that is, the SOAP body) in the messages exchanged between a service and its consumer.
Data contracts are the preferred WCF way to enable serialization of complex types included in operation signatures, either as parameters or as return values.

Using a data contract, you can specify exactly how the service expects the data to be formatted as XML. The data contract is used by a data contract serializer in WCF client applications to describe how to serialize the data for parameters into XML, and by a data contract serializer in the service to deserialize the XML back into data values that it can process. Values returned by a service are similarly serialized as XML and transmitted back to the client application, which deserializes them.


DataContract Attribute
The DataContract Attribute, defined in the System.Runtime.Serialization namespace, is used to declare a type as a Data contract.
It can be applied to enums, structs, and classes only.


DataMember Attribute
The DataMember Attribute, also part of the System.Runtime.Serialization namespace, is applied only to members and is used to declare that the member should be included in the serialization of the data structure.


EnumMember Attribute
The final Data contract–related attribute to consider is the EnumMemberAttribute. As the name suggests, it is used to declare that a given element of an enum that has been declared with a DataContractAttribute should be considered as part of the Data contract.

Eg: Create a WCF Service and write the following code in the IService.cs file.

[ServiceContract]
public interface IService
{
    [OperationContract]
    Employee GetEmployee();
}

[DataContract]
public class Employee
{
    string name, job;
    [DataMember]
    public string Name
    {
        set { name = value; }
        get { return name; }
    }
    [DataMember]
    public string Job
    {
        set { job = value; }
        get { return job; }
    }
}

Write the following code in the Service.cs

public class Service : IService
{
    public Employee GetEmployee()
    {
        Employee emp = new Employee
        {
            Name = "Jobin John",
            Job = "Software Eng"
        };
        return emp;
    }
}


Add a new website to Solution. Place a button and write the following code.

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            EmployeeServiceReference.ServiceClient client = new WebApplication1.EmployeeServiceReference.ServiceClient();

            EmployeeServiceReference.Employee emp = client.GetEmployee();
            Response.Write("Name : " + emp.Name);
            Response.Write("
Job : " + emp.Job); }

Returning Views in MVC Application

Create an ASP.NET MVC 2 empty application. Add a controller named HomeController and write the following action method.

public ViewResult Index()
        {
            return View();
        }


 

ActionResult Class:

All methods within a controller are Action methods, and returns ActionResult object.

The above function returns ViewResult
object; which is a subclass of ActionResult

Adding View:

    Right click in the method and select add view option. Name the View as "Index" and add the view.

Run the application. Hence you have created an MVC application which returns a View.