Introduction to LINQ: FileList DataSource in ASP.NET and C#


Server Intellect


Introduction to LINQ: FileList DataSource in ASP.NET and C#

This tutorial was created with Microsoft's LINQ Community Technology Preview release, which can be downloaded from here

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

LINQ can be used to treat a whole host of things as a data source. For example, we can use a folder directory as a data source, or a list of running processes, or a list of controls on a web form. Where DLINQ is used for databases, and XLINQ is used for XML files, LINQ is used for objects; LINQ can create data sources from objects.

In this short example, we will show how we can use LINQ to grab a list of files from a folder, and display them in a DropDownList. Before LINQ, this would have taken more lines of code, and would have taken longer to implement. LINQ does not really bring anything new to programming, but makes dealing with data sources a lot easier and quicker to implement.

We create a new LINQ ASP.NET Web Site in ASP.NET. This will create our Web.config and add the relevant DLLs into the bin folder in order for us to implement LINQ into our web form.
The Web.config will look something like this:

<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharp3CodeProvider, CSharp3CodeDomProvider"/>
</compilers>
</system.codedom>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

This example was created as a web application, so note the assembly references at the top of the page. We will also need System.IO:

using System.Query;
using System.Data.DLinq;
using System.IO;

First, we add the Controls to the web form. We will be adding a Button and a DropDownList:

<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Get List of Files" OnClick="Button1_Click" /><br />
<asp:DropDownList ID="FileList" runat="server" DataTextField="FileName" DataValueField="FullPath" />
</div>
</form>

Next, we will add the code to the click event of the button. The code will retrieve a list of files from the root directory, and then the

using System;
using System.Data;
using System.Data.DLinq;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Query;
using System.IO;

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
this.FileList.DataSource = from file in Directory.GetFiles("/")
select new
{
FileName = Path.GetFileName(file),
FullPath = file
};

this.FileList.DataBind();
}
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
gabriel said:

hi

thanks about your example, but this don't work, a error have this, and say that message:

Compiler executable file C:\Archivos de programa\LINQ Preview\Bin\csc.exe cannot be found.

Details of the exeption: System.InvalidOperationException: Compiler executable file C:\Archivos de programa\LINQ Preview\Bin\csc.exe cannot be found.

Posted 10/20/2008 at 10:57 AM

Leave a Comment