[Home]Personal Website Starter Kit - Adding A Links Table - Part 1 - Table Driven Links

Contents | (Visit Preferences to set your user name.) | Related To Personal Website Starter Kit - Adding A Links Table - Part 1 - Table Driven Links | RecentChanges | Preferences | Index | Login | Logout

Featured: Featured Stories | Picture Gallery

What Causes Heartburn | June 19 2005 Art Bell Show
Google
Chat11.com Web Bible11.com MyBibleCenter.com
Cover of ISBN 0834003465 Cover of ISBN 0785201688
Cover of ISBN 081297476X The Bush Survival Guide - 250 Ways to Make It Through The NExt Four Years Without Misunderestimating the Dangers Ahead, and Other Subliminable Stategeries
Links:

Personal Website Starter Kit : Adding a Links Table : Adding A Users View Of The Links

Also known as the Personal Web Site Starter Kit...

Subjects > Computers > Microsoft > Visual Studio 2005 Beta > Personal Website Starter Kit

Part 1 - User View Of The Links

Adding Links And Categories Tables To The Database

Visual Studio 2005 comes with some nice tools for dealing with databases from within the development environment. Right now I am running Visual Studio 2005 within a Virtual PC session that is running Windows 2003. I have not installed Microsoft Office in that Virtual PC, and so I only have the Visual Studio tools to deal with databases. This is ok because it seems that Microsoft Access can not deal with the .MDF files that are supplied with this starter kit.

To open the databases that are supplied with this starter kit, go to the Solution Explorer window, and open the App_Data directory. Double click on the ASPNETDB.MDF file, or the Personal.mdf file. This will cause the Server Explorer window to open. You will then have a tree view to deal with databases so you can inspect the tables, stored procedures, and more. It is interesting to browse the table definitions of the ASPNETDB.MDF file.

Since this application has a Personal.mdf data file for Albums and Photos, this is probably also a good place to create the links table. Right click on the "Table" entry below Personal.mdf in the Server Explorer. Select "Add New Table" from the context menu. Go to the Properties view and change the Name for this table from Table1 to Links.

Enter these fields:

When done, go to the file menu and select "Save Links". This will actually create the table in the database.

Now create a second table called LinkCategories?Create:

Save this table also.

Insert the following sample data into these tables:

TO BE SHOWN: How to paste in a SQL data dump….

Create The Business Object For Links And Categories

We need to create a business object to get data from the categories table. See the MSDN help topics "Retrieving Data Using The ObjectDataSource?Create Control", and "Walkthrough: Data Binding to a Custom Business Object".

We will use one business object class to handle both our Links and Categories tables. The App_Code / PhotoManager?Create.cs file is the file that handles the business object class for the Albums and Photos.

Go to the App_Code directory in the solution explorer, and right click to select "Add New Item". Choose the Class template. For the Name type LinkManager?Create.cs. Be sure the language is Visual C#.

We also need to create the object types for our categories and links. Under App_Code, create a new file called LinkObjectTypes?Create.cs We need to add some additional components for our class to use, so add these usings at the start with this list:

using System.Collections; using System.Collections.Generic; using System.Data.SqlClient?Create; using System.IO;

Enter the following code for the categories object into LinkObjectTypes?Create.cs....

TODO

Now enter this code for the constructor of the LinkManager?Create in LinkManager?Create.cs....

TODO

Now lets create a procedure to return the list of categories. Enter this code for GetCategories?Create in the LinkManager?Create.cs file...

TODO

We also need to create a stored procedure for the ExecuteReader?Create.

CREATE PROCEDURE dbo.GetCategories?Create

	@IsPublic?Create bit
AS
	SELECT * 
	FROM LinkCategories?Create 
	RETURN

Select Build Solution from the Build menu to make sure everything is ok. Correct any syntax errors.

Adding an Object Data Source To Our Web Page

Now we are ready to add an ObjectDataSource?Create to the links page.

Now that we have an ObjectDataSource?Create, lets drop a DataList?Create onto the page. Go to the Toolbox, go to the Data items, and drag and drop a DataList?Create onto the bottom of the page. In the tooltip dialog, choose the ObjectDataSourceCategories?Create.

Go to Links.aspx, insert a datalist and format the item template for the data:

TODO

Now you can test the work done so far. Run the links.aspx page and see the list of categories. Frequently stopping to check what is happening is a good practice, it helps you find errors as early as possible.

Check the following

Also, running with debugging can be very helpful to find errors, but not for the reasons you might expect. Running with debugging causes errors to be flagged much earlier in the process of running the .NET code, eliminating spurious unhelpful errors at a later point. For example, set the Title to be an int. This will cause problems for the reader, because it wont be able to type case the Title to an int. Run the links.aspx without debugging, and then run with debuging.

Displaying The Links For A Given Category

Now let's add the code for displaying the links of a category.

TODO

First lets add the Link object to LinkObjectTypes?Create.cs:

TODO

Now we need to add a procedure to return the list of links from the LinksManager?Create:

TODO

Do a File -> SaveAll?Create to make sure that the system knows about the new procedure added to LinksManager?Create. Do a build solution, and correct any errors that appear. If you have errors in the LinksManager?Create or LinkObjectTypes?Create filoes, then the step below where you select a business object might not show all the business objects you would expect.

Now we'll add another DataSource?Create to the Links.aspx page:

Now we need to add a conditional section to the Links.aspx file. Click to source view.

In front of the current DataList1?Create object, add this code: <%

	CatID?Create = this.Request.Params.Get("CatID?Create");
	if (CatID?Create != null) // Null (not "") if there is no CatID?Create in query string (url)
	{
%> Request listing of category <%= CatID?Create %>.
<a href=Links.aspx>Back...</a>
<%
	}
	else
	{
%>

And immediately following the DataList1?Create object, add this: <%

	}
>

This causes the list of categories to be displayed when we have no CatID?Create in the URL. But if there is a CatID?Create, then the page displays the category ID selected. Go ahead do Debug -> Start Without Debugging to try the page. Try clicking on the links at the bottom to select different categories and see what hapepns.

Formatting The Item Template For The Links

Now lets go back to design mode on Links.aspx and add our data list to display the links, and format the item template for the links:

TODO

Go ahead and run the webpage. Select Debug -> Start With Debugging

Now lets fix up the template for the item listings. We will want to make the title of each link be a link to the website, and also remove some of the fields.

The default ItemTemplate?Create looks something like this:

<ItemTemplate?Create>

	<asp:Label ID="TitleLabel?Create" runat="server" Text='<%# Eval("Title") %>' ></asp:Label>
Description: <asp:Label ID="DescriptionLabel?Create" runat="server" Text='<%# Eval("Description") %>'> </asp:Label>
CatID?Create: <asp:Label ID="CatIDLabel?Create" runat="server" Text='<%# Eval("CatID?Create") %>'></asp:Label>
Approved: <asp:Label ID="ApprovedLabel?Create" runat="server" Text='<%# Eval("Approved") %>'></asp:Label>
Url: <asp:Label ID="UrlLabel?Create" runat="server" Text='<%# Eval("Url") %>'></asp:Label>
Domain: <asp:Label ID="DomainLabel?Create" runat="server" Text='<%# Eval("Domain") %>'></asp:Label>

</ItemTemplate?Create>

Replace it with the following code:

	<ItemTemplate?Create>

	
<a href="<%# Eval("Url") %>"> <asp:Label ID="TitleLabel?Create" runat="server" Text='<%# Eval("Title") %>' ></asp:Label></a>

		<asp:Label ID="DescriptionLabel?Create" runat="server" Text='<%# Eval("Description") %>'>
		</asp:Label>
		
	</ItemTemplate?Create>

Fixing A Display Problem With Long Titles

Did you notice how the title of the Personal Web Site Starter Kit link was too wide for it's area, how the title overlaps the description? Let's fix that by adding a function that adds line break tags.

Go to Links.aspx.cs and add the following function inside the definition of the class Links_aspx:

	public string AddLineBreaks?Create(string AsciiText?Create, int MaxChars?Create)
	{
		if (AsciiText?Create.Length < MaxChars?Create)
		{ return AsciiText?Create; }
		string [] split = AsciiText?Create.Split(new Char[] { ' ' });
		string Results="";
		int accumulated = 0; // How much have we put together since the start or the last line break?
		foreach (string s in split)
		{
			if (accumulated + s.Length > MaxChars?Create)
			{
				Results += "
"; accumulated = 0; } if (Results.Length>0) { Results += " "; } Results += s; accumulated += s.Length; } return Results; }

Yes, we probably could have used a regular expression to do the work above, but the above code is very clear and transparent.

And change the call for the TitleLabel?Create to the use this new function:

	<asp:Label ID="TitleLabel?Create" runat="server" 
	Text='<%# AddLineBreaks?Create((string)Eval("Title"),22) %>' >

Try it out to make sure it works.

Showing The Title And Description Of The Selected Category

Next thing we should do is put some text in the left column to say what category of links has been selected. First we'll need to add a lookup function that converts a category ID into the title of the category. This will require adding a new stored procedure, and a new routine to the LinkManager?Create.

Cut these lines of text from Links.aspx:

	

Resources

<a href="Links_Health.aspx">Health Links</a>
Links about health.

Above the DataListLinks?Create add this:

	

<%= CatIDTitle?Create %>
<%= CatIDDescription?Create %>

And above the DataList1?Create add this:

	

Categories

Add a few variables to the Links.aspx.cs:

	public LinkManager?Create linkmanager;
	public string CatIDText?Create;
	public string CatIDDescription?Create;

Now run to see the results.

It's looking pretty good now. Time to get rid of the dummy entries that originally came with the page.

Delete the following text from Links.aspx:

	

Top 5

<a href="http://www.chat11.com/Personal_Website_Starter_Kit">Personal Website
Starter Kit Wiki</a>
Wiki describing the personal website starter kit, problems, fixes, and updates.

Cool Site Designs

<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.

Photo Sites

<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.
<a href="#">Linked site name</a>
A description of the web site goes here.

Try it out... You've now got a very nice categorized listing of links, driven by database table.

This is the "Stage 1 Links Page".

What to do next?

Go on to the next part of this article

Manually Adding Everything Without Drag And Drop Or Wizards

Go down to Stored Procedures, and right click to select Add each of these:

CREATE PROCEDURE dbo.GetLinks?Create

	@IsApproved?Create bit
AS
	SELECT * 
	FROM Links 
	WHERE ([Links].[IsApproved?Create] = @IsApproved?Create)
	RETURN

CREATE PROCEDURE dbo.AddLink?Create

	@Url nvarchar(128),
	@Domain nvarchar(70),
	@Title nvarchar(80),
	@Description nvarchar(255),
	@IsApproved?Create bit
AS
	INSERT INTO [Links] ([Url],[Domain],[Title],[Description],[IsApproved?Create]) 
		VALUES (@Url,@Domain,@Title,@Description,@IsApproved?Create)
	RETURN

CREATE PROCEDURE dbo.GetCategories?Create

	@IsPublic?Create bit
AS
	SELECT * 
	FROM LinkCategories?Create 
	RETURN

CREATE PROCEDURE dbo.AddCategory

	@Title nvarchar(80),
	@Description nvarchar(255),
		@Identity int OUT
AS
	INSERT INTO [LinkCategories?Create] ([Title],[Description]) 
		VALUES (@Title,@Description)
		SET @Identity = SCOPE_IDENTITY()
	RETURN

Now we have to create our links to these new database. Go to Links.aspx source view, and insert this text at the end:

	


Check out [Facts About Trees]


Contents | (Visit Preferences to set your user name.) | Related To Personal Website Starter Kit - Adding A Links Table - Part 1 - Table Driven Links | RecentChanges | Preferences | Index | Login | Logout
Edit this bookstore.mybiblecenter.com page | View other versions
Last edited March 21, 2008 6:30 pm (diff)
Search:
Sign up for PayPal and start accepting credit card payments
instantly.
Bobsgear - Get A Free Enterrpise Wiki Space!
Review: The Bobsgear Project was started to develop a variety of Confluence plugins. This installation of the Confluence Enterprise wiki includes flexible attachments, many Confluence plugins, personal blogs, interesting articles, and more. Bobsgear already has spaces related to politics, art and photography wiki, technical issues wiki, ediscovery wiki, health, Christian theology and Sabbath School wiki, the bible, book reviews, and quotations. Bobsgear allows free signup, and invites anyone to create a free hosted Confluence wiki space.


NEW USERS CLICK HERE! for a quick introduction to Wiki.

 

 Interested in Does Four Inches Of Ocean Rise Attributed To Global Warming Make A Difference?
2646 total hits since 9/2007
Recently accessed pages: About Rita AmphibianWiki Dantz Retrospect 6.0 Agreement Dealing With Inventory Assemblies In Quick Books Food Additive Regulations Amended Formaldehyde Libby Holman List Of Phobias That Start With Q Mars May Calendar Meg Ryan People People In Profit Investigated By Bank Negara Of Malaysia PerSpective Perimeter Real Time Strategy Game Software License Periodic Antenna Perl Memory Usage Personal Website Starter Kit Ricotta Cheese SEM - Submit And Index Pages Science Science/TopAd1 Search Engine Starpond Set Strength Of Mother Of Pearl Troy Donahue Web Portal Software

Elapsed:0