It operates on collections of items, for instance arrays or other built-in list types. It does not use an integer index. Instead, it is used on a collection and returns each element in order. In the foreach-statement, you do not need to specify the loop bounds minimum or maximum. It traverse whole collection.

General Syntax:

foreach( Datatype item in items )
{
Statement;
}

Example: To add the number from 1 to 10 by using foreach loop and sum the number by series of 1+2+3+...+10.

Both the concepts are same only. For each is used for traversing items in a collection. It usually maintain no explicit counter and no use of index. They essentially say "do this to everything in this set", rather than "do this x times".

For loop is classified as an iteration statement. The statements in the for loop repeat continuously for a specific number of times. It as counter and integer index (ex: a[i], i represent the index value and change it by increment operation on each iteration).

Update

To Update a database entry, first retrieve the item and edit it directly in the object model. After you have modified the object, call SubmitChanges on the DataContext to update the database.

The following example, Let retrieve a Student detail from tbl_Student table who are all from Location Pondy. Then change Location = “Pondy” to “Puducherry”. Finally SubmitChanges is called to send the changes to database.

Tuesday, July 9, 2013

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

LINQ Providers:

LINQ to SQL

LINQ to XML

LINQ to Dataset

LINQ to Objects

LINQ to SQL:

LINQ to SQL is a component of .NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language.

By using LINQ to SQL, you can use the LINQ technology to access SQL databases just as you would access an in-memory collection.

Step 1: The Below picture will explain how to add LINQ to SQL in your project

Open Visual Studio and goto New Project and select the Windows Form Application.

In Solution Explorer, right click the application name -> Add -> New item.

Step 2: After open the "Add New Item Wizard" select the LINQ to SQL classes file in Data Category.

Step 3: The Object Relational Designer allows you to visualize data classes in your code. By click Server Explorer to add the table.

Step 4: Connect Database using Server Explorer at Right side or click the link enable text.

Step 5: Connect to Database using Server Explorer.

1.Default DataSource is SQL Server(Sqlclient).

2.Type the server name.

3.Select authentication type based on sql server

4.Select the Database.

5.(Optional) Verify the connection using Test Connection.

6.Press OK to make a connection with database and our project.

Step 6: Drag the table from server explorer to Dataclasses file.

Step 7: Double click the Form in solution explorer and place a Datagridview control from Toolbox to form. Write the below code in Form Load event.

LINQ with SELECT Statement and without Where Clause

namespace linq_example

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

//Create DataContext object by using LINQ file DataClasses.

{

DataClasses1DataContext db = new DataClasses1DataContext();

//Linq query using IQueryable for Select statement query.

IQueryable<tbl_Student> student =

from stud in db.tbl_Students

select stud;

//set Datasource to DataGridView for bind the data.

dataGridView1.DataSource = student;

}

}

}

The Above code used the concept of linq to sql with Select statment and bind the data into datagridview.

Monday, July 8, 2013

Several ASP.NET application services rely on a provider to manage storing and retrieving data from a data source. Each provider is specific to the data source. ASP.NET includes a SQL Server provider for the following ASP.NET features:

Membership (the SqlMembershipProvider class).

Role management (the SqlRoleProvider class).

Profile (the SqlProfileProvider class).

Web Parts personalization (the SqlPersonalizationProvider class).

Web events (the SqlWebEventProvider class).

The above features are we need to define in asp.net 'Web.config' file.

Web.config is the main settings and configuration file for an ASP.NET web application. The file is an XML document that defines configuration information regarding the web application.This file stores the information about how the web application will act. The web.config file contains information that controls module loading, security configuration, session state configuration, and application language and compilation settings. Web.config files can also contain application specific items such as database connection strings.

ASP.NET membership gives you a built-in way to validate and store user credentials. ASP.NET membership therefore helps you manage user authentication in your Web sites. You can use ASP.NET membership with ASP.NET forms authentication by using with the ASP.NET login controls to create a complete system for authenticating users.

ASP.NET role management helps you to manage authorization, allowing you to specify which resources various users in your application are allowed to access. Role management lets you treat groups of users as a unit by assigning users to roles such as manager, sales, member, and so on. The primary purpose of establishing roles is to give you an easy way to manage access rules for groups of users. You create users and then assign the users to roles (in Windows, to groups). A typical use is to then create a set of pages that you want to restrict to certain users.

The ASP.NET SQL Server registration tool (Aspnet_regsql.exe) is used to create a Microsoft SQL Server database that is used by the SQL Server providers in ASP.NET. The tool is also used to add or remove options from an existing database.

You can run Aspnet_regsql.exe without any command-line arguments to run a wizard that will walk you through specifying connection information for your SQL Server installation, and installing or removing the database elements for the membership, role management, profile, Web Parts personalization, and health monitoring features.