Saturday, October 26, 2013

Key Concepts of Entity Data Model

The Entity Data Model (EDM) uses three key concepts to describe the structure of data: entity type, association type, and property. These are the most important concepts in describing the structure of data in any implementation of the EDM.
Entity Type
In a conceptual model, An entity represents a specific object (such as a specific customer or order). Each entity must have a unique entity key within an entity set.
Association
In a conceptual model, an association represents a relationship between two entity types (such as Customer and Order).
Property
Entity types contains properties that define the structure and characteristics of Entities. For example, a Customer entity type may have properties such as CustomerId, Name, and Address.

What are Advantages and Disadvantages of Entity Framework

There are several advantages and disadvantages of using Entity Framework.
Advantages

  • No need to write the Data access layer code. Entire Data access layer code will be generated by the Entity  Designer (.edmx file). 
  • For small applications, no need to write Stored procedures for the CRUD opeartions (Insert,Update,Delete and modify). The Designer will provide the implementation. Developer need to consume those methods.
  • Reduces development effort and Unit Testing effort.
Disadvantages
  • As the queries are dynamic there might be a hit in the performance of the application.

What is Microsoft ADO.NET Entity Framework

The Microsoft ADO.NET Entity Framework (EF) 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.

The Entity Framework works with Microsoft SQL Server and 3rd party databases through extended ADO.NET Data Providers, providing a common query language against different relational databases through either LINQ to Entities or Entity SQL.

Thursday, May 23, 2013

Set and Get Textbox Value inside ascx Control from ASP.NET Page

Setting and getting Textbox value inside user control (ascx control) from aspx page is a usual practice in both simple and complex projects.
In this tutorial, we will
  • Create New User Control.
  • Add User Control in aspx Page.
  • Set Textbox Value inside User Control
  • Get Textbox value From User Control

Create New User Control

  1. Right Click on Project, Click Add New Item.
  2. A pop up window will be opened.
  3. Select Web User Control from list, change its Name to 'myControl' and click Add
  4. Two files will be added with name 'myControl.ascx' and 'myControl.ascx.cs'
Replace the code in 'myControl.ascx' file with following code.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="myControll.ascx.cs" Inherits="Controls_myControll" %>
<table>
    <tr>
        <td>
            Name :
        </td>
        <td>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Age :
        </td>
        <td>
            <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Profession :
        </td>
        <td>
            <asp:TextBox ID="txtProfession" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>

Add following code in 'myControl.ascx.cs' File.
    public string Name
    {
        get { return txtName.Text.ToString(); }
        set { txtName.Text = value; }
    }
    public string Age
    {
        get { return txtAge.Text.ToString(); }
        set { txtAge.Text = value; }
    }
    public string Profession
    {
        get { return txtProfession.Text.ToString(); }
        set { txtProfession.Text = value; }

    }

Add User Control in aspx Page

  1. Create New aspx Page with name ‘PageForWebControl’.
  2. Open Design View and drag and drop user control on it. Some code will be added in 'PageForWebControl.aspx' page automatically. 
Page will look like this.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageForWebControl.aspx.cs" Inherits="PageForWebControl" %>

<%@ Register src="Controls/myControll.ascx" tagname="myControll" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        <uc1:myControll ID="myControll1" runat="server" />   
    </div>
    </form>
</body>
</html>

Set Textbox Value inside User Control

Add following code snippet in Page Load Event of 'PageForWebControl.aspx' page to set Values of Textbox  inside User Control.
// Setting Values in ASCX Control
        myControll1.Name = "Waqas Ali";
        myControll1.Age = "24";
        myControll1.Profession = "Computer Scientist";

Get Textbox value From User Control

Add following code snippet in Page Load Event of PageForWebControl.aspx page to get values of Textbox from User Control.
        string _name = string.Empty;
        int _age = 0;
        string _profession = string.Empty;
// Getting Values from ASCX Control
        _name = myControll1.Name;
        _age = Convert.ToInt32(myControll1.Age);
        _profession = myControll1.Profession;

Now you are all done. Run Project and test your values.

Wednesday, May 22, 2013

Difference Between string and String in C#.NET

There is no different between string and String (System.String). string is an alias for System.String in C#.Net.
They compile to the same code, so no difference at execution time. The complete list of aliases in C#.Net is

object:System.Object 
string:System.String
bool:System.Boolean
byte:System.Byte
sbyte:System.SByte
short:System.Int16
ushort:System.UInt16
int:System.Int32
uint:System.UInt32
long:System.Int64
ulong:System.UInt64
float:System.Single
double:System.Double
decimal:System.Decimal
char:System.Char