Connected Data Access

The quickest way to interface with a database from a web-based application is using direct data connections. Rather than using DataSets, DataAdapters, DataTables and all that confusing stuff, go straight to the data! The following is an easy way to perform all the data manipulation you need.

1.) Declaire a connection to you database in your web.config file.

<connectionStrings>
<add name="myConnectionString" connectionString="Data Source=SERVER\SQLINSTANCE;Initial Catalog=databaseName;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
</connectionStrings>

More information on Connection Strings can be found here:
http://msdn2.microsoft.com/en-us/library/bf7sd233.aspx

2.) Declare your connetion string in your code-behind page.

Dim connStr As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString

3.) Declare a new SqlConnection using the connStr

Dim conn As New System.Data.SqlClient.SqlConnection(connStr)

4.) Declare a new SqlCommand. The first element of the constructor is your SQL statement, the second element is the SqlConnection, conn

Dim cmd As New System.Data.SqlClient.SqlCommand("TRUNCATE TABLE [customer]", conn)

5.) Use a using statement to instantiate your connection. This will automatically create and destroy your connection when used.

Using conn
conn.Open()
cmd.ExecuteNonQuery()
End Using

That’s it! Using this method your data access and manipulation will be much quicker and cleaner!

It’s also very easy to use this method to bind standard ASP controls to data from a database using connected data access.

If your SQL was: “SELECT [name], [date_of_birth], [eye_color] FROM [people]“, you can bind that to DropDownLists, GridViews, Repeaters, FormViews, etc.

DropDownLists:
Using conn
conn.Open()
dim r as Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
While r.Read
DropDownList1.Items.Add(r(0).ToString)
End While
End Using

GridViews:
Using conn
conn.Open()
GridView1.DataSource = cmd.ExecuteReader()
GrieView1.DataBind()
End Using

Master Pages and CSS StyleSheets

A common problem that often arises from using Master Pages is the confusion about how to reference a CSS StyleSheet from a page within a Master Page. The problem is that the <head> section of the markup is located in the Master Page, so the reference to the StyleSheet cannot be made… seemingly. The trick is to add some code to the Page Load event of the page that need access to the StyleSheet.

This little snippet of code comes in handy during these situations:


Dim link As New HtmlLink
link.Href = "LocationOfMyStyleSheet.css"
link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString(), "stylesheet")
Page.Header.Controls.Add(link)

MagicAjax

If you want to implement a quick and easy AJAX solution to your ASP.net 2.0 web application, I highly recommend MagicAjax.

http://www.magicajax.net/

This is a standard ASP web control that can be used just like the asp:panel control. The best thing about this panel, though, is that everything inside the MagicAjax panel automatically uses AJAX! Gridviews, DropDownLists, Calendars, ListBox, whatever, it all works without page refreshes. Quick. Easy. Effective.

1.) Download the latest version of MagicAjax

http://http//www.magicajax.net/Default.aspx/tabid/29

2.) Create the folder

C:\Program Files\Microsoft Visual Studio 8\magicajax

and copy the contents of the download to this folder.

3.) Right-click on the toolbox in Visual Studio and select “choose items…”

4.) In the .NET Framework Components tab select “Browse” and select this file

C:\Program Files\Microsoft Visual Studio 8\magicajax\bin\MagicAjax.dll

from the folder you just created. Press OK.

Now you have MagicAjax in your Toolbox. When you want to add the control to a web page:

1.) Modify your web.config file to contain:

<httpModules>
<add type="MagicAjax.MagicAjaxModule, MagicAjax" name="MagicAjax" />
</httpModules>

2.) Add a Register tag to the top of the page you want the control on (or let MagicAjax add it for you if you drag-and-drop the control)

<%@ Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %>

3.) Add the control to your code using:

<ajax:ajaxpanel id="MyAjaxPanel" runat="server">

4.) Add whatever controls you want inside this panel, and BAM! AJAX!

Of course there are plenty configuration options for this control. For some good further reading, visit http://www.magicajax.net/site/docs/Configuration.html

ReportViewer Control Issue

The ReportViewer is a great little asp.net 2.0 control. It lets you run MSSQL Reporting Services Reports from a remote server – even with Microsoft SQL Express Edition, which surprised me. The control is fairly simple to use; drag-and-drop the control and set it up for Remote Processing mode and away you go. The problem I had, and the problem many other folks seemed to be having was on the deployment of a web app with a ReportViewer control. When the application gets published to the IIS server, a weird error would get thrown. Something to the effect of:

The file '/Path/Default.aspx' has not been pre-compiled, and cannot be requested.

This page may not even be the page with the ReportViewer control in it. The error exists for multiple pages. The fix for this is pretty simple. A little program ships with Visual Studio 2005 and needs to be installed on the IIS server running the ReportViewer. The program is located here:

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages\ReportViewer\ReportViewer.exe

Install this program onto your IIS server, and everything should work out just fine. Some good articles that talk about the ReportViewer control are in the MSDN library.

http://msdn2.microsoft.com/en-us/library/ms251671(VS.80).aspx