Saturday, April 30, 2005 10:49:00 PM UTC :: Filed Under ASP.NET | VB.NET

In case you didn’t already know, you can run SQL scripts right within Visual Studio.NET which is much nicer than having to fire-up Query Analyzer all of the time.  This tutorial assumes you already know what a Database project is in Visual Studio.

Instructions:

The first thing you must do is create a Database Reference in your database project. In the Visual Studio Solution Explorer, open the database project and right click on the Database References folder.  Select New Database Reference… and the following window should pop-up:

If the database you’d like to connect to is not listed, click Add New Reference… and you will be prompted to setup a connection to the database of your choice.  Items are added to this list via the Server Explorer

You can add several database references to a project.  I would suggest making a reference to your local, development SQL database and one to the live web server’s SQL database.

Once the database you’d like to connect to is listed in the Add Database Reference list, select it and click OK.  You notice that this reference is now listed under the Database Reference section in the Solution Explorer:

If you have several databases listed, you can right-click on the one you want as the default (usually your local SQL database) and select Set as Project Default.

To run a SQL Script against a selected database, right-click one of the SQL scripts stored in the database project and select Run On…  You’ll be prompted with a window like this:

Select the database you’d like to run the script against, click OK, and the script runs!  Hopefully you’ll find this a useful and time-saving tip J

Saturday, April 30, 2005 10:22:56 PM UTC :: Filed Under ASP.NET

Don’t you hate it when you complain about a program not being able to do something, then you realize the program could do exactly what you wanted but you were too dumb to know how to use it correctly?   Well, for me, this is the case with Visual Studio.NET’s Copy Project utility.   I’ve tried it before in the past, but never got it to work right so I just manually copied projects from my development machine to the live web server.  Duh.

When configured correctly, the Copy Project utility copies whatever you want to the live web server, the most useful feature being the ability to copy over only the essential files required for running the app.

Instructions:

In the Visual Studio Solution Explorer, select the project that you would like to copy project, then browse to Project > Copy Project…

You should be prompted with a window that looks like this:

For the Destination project folder, it should be the full URL path to your site’s virtual directly.  That may be http://localhost/someproject or a fully qualified domain name like  http://my.domain.com/... It all depends if you’re copying to a virtual directory on your dev machine or a web site on a live web server.  Obviously you should insert the path to the file-share on your machine that connects to the live web server.  Apparently a mapped drive or UNC path will work.

Select Only files needed to run this application from the Copy section.   This prevents .vb, .resx, and other non-essential files from being copied to the web server.

You may be prompted with a warning stating that the Virtual directory could not be created… just click OK.  You will then be prompted with this:

Check Apply to all items and let VS.NET copy all the files to the live web server.

Make sure the web.config file is correctly configured for use on the web server.  The Copy Project utility will not copy a user.config file over (assuming you excluded it from your VS.NET project), so use the user.config on your local machine and leave the web.config configured for the live server settings. The Copy Project seems to remember the last settings you used, so if you’re constantly working on the same program, you won’t have to keep reconfiguring the Copy Project tool.

Tuesday, April 26, 2005 7:06:24 PM UTC :: Filed Under ASP.NET

The Problem:

You just built a user control (.ASCX) that is a header template for your web site.  The user control contains some ‘root sensitive’ information, like the path to an image, the path to a linked JavaScript file, the path to a linked CSS file, etc.   You’d like to use relative file paths, but you already know that you can’t prefix the file path with “/” because your developing the site in a Virtual Directory on your Windows XP machine.  Prefixing a relative URL with “/” will take you to the root of the web site, not the root of the Virtual Directory.

So, what do you do to make sure the file paths always work no matter where your user control is placed in the directory structure?

One option is to append <%= Request.ApplicationPath %> before all of your URLs to automatically insert the name of the Virtual Directory into the path.  (This is used in the Microsoft iBuySpy Portal solution.)  For example, if you wanted to link to your Style Sheet in the header, the HTML would look like this:

<link href="<%= Request.ApplicationPath %>/CSS/Screen.css"
type="text/css" rel="stylesheet">

However, once you move your site from your development machine to a live web server where your site will most likely be located in the root directory, the above code will fail.  Well, it will work, but the path to the Style Sheet will end-up being:

//CSS/Screen.css

Note the double forward slashes.  Request.Application path returns “/” when ever the site is at the root directory.  Some browsers will choke on a URL with two forward slashes in it.

Ok, so that was lame.   Are we just screwed?

The Solution: (Well, the best one I’ve found so far)

When inserting images into a User Control, it’s probably easiest to just use the ASP.NET Image Control.  If you prefix the ImageURL in an Image Control with “~/”, the Image Control will automatically find the root directory for you.

That’s all fine-and-dandy, but what if you’re not working with an image?  Well, after much research, I found-out the secret method being used by the image control is ResolveURL().  With ResolveURL(), you can insert the path to any file and get the root directory.  For example, let’s go back to our Style Sheet example from above.  If you replace the Request.ApplicationPath with ResolveURL, your problem is solved:

<link href="<%=ResolveURL("~/CSS/Screen.css") %>"
type="text/css" rel="stylesheet">

Note that you’ll still need to prefix the URL with “~/”.   Granted, this isn’t the prettiest solution, and image won’t show-up in design time (I hate that), but at least it works!

Tuesday, April 05, 2005 11:46:49 PM UTC :: Filed Under ASP.NET | VB.NET

It seems easier than it is… looping through an array of items (collection) and removing items you don’t want.  However, when you remove an item from a collection while looping through it, the size of the collection changes and the loop may fail… depending on how you are looping.

The correct way to loop through a collection is to essentially loop through it backwards, starting with the item with the maximum ID and working down to zero:

    1  For i As Integer = MyList.Count - 1 To 0 Step -1
    2      If MyList.Items(i).Value <> SomeValue Then
    3          MyList.Remove(MyList(i))
    4      End If
    5  Next
Thursday, March 24, 2005 4:38:14 AM UTC :: Filed Under ASP.NET

Have you ever noticed that some pages on Microsoft's web site have an extension of .ASHX (among other things)?

ASHX files are WebHandlers that derive from IHttpHandler. Their advantages are mainly that you can gain access to the HttpContext without all the overhead of loading the Page class if you don't need the UI.

These classes have only two required methods - ProcessRequest, which accepts an HttpContext parameter, and the boolean IsReusable. In short, ASHX files let you create an HTTP endpoint that can return any content type that you want. And, with ASHX, there is no requirement for registration in web.config or machine.config - you just drop them into your project and they are usable "out of the box".

One thing I've noticed is that the C# guys seem to have all sorts of articles on using ASHX files, but the VB.NET guys do not.   So, after a little bit of research, I figured-out how to write an ASHX page in VB.NET.  The code sample below only has the small function of logging a user out of a web site... Maybe it's not the best example, but its all I've got!

    1 <%@ WebHandler Language="VB" Class="MyNamespace.Web.Logout" %>

    2  

    3 Imports System.Web

    4 Imports System.Web.Security

    5  

    6 Namespace MyNamespace.Web

    7     Public Class Logout

    8         Implements IHttpHandler

    9         Public Sub ProcessRequest(ByVal context As HttpContext) _

   10    Implements IHttpHandler.ProcessRequest

   11             FormsAuthentication.SignOut()

   12             Context.Response.Redirect("default.aspx")

   13         End Sub

   14  

   15         Public ReadOnly Property IsReusable() As Boolean _

   16          Implements IHttpHandler.IsReusable

   17             Get

   18                 Return False

   19             End Get

   20         End Property

   21     End Class

   22 End Namespace

Using an ASHX page is just the tip-of-the-iceberg when it comes to using HTTP Handlers.  Scott Mitchell's article, “Serving Dynamic Content with HTTP Handlers“ shows how HTTP Handlers can be used to format code on web pages, prevent people from “hot-linking” to images on your web site, and re-writing URLs to more user-friendly names.  

Using ASHX and Code-Behind

The method described above shows how to use an ASHX as a single file. However, VisualStudio.NET doesn't recognized the file type, hence no Intellisense support :( I don't know about you, but I can't live without Intellisense, and there is hope! An ASHX file can have a code-behind class file just like an ASPX page. The actual ASHX page should only have a line like the following in it:

<%@ WebHandler Language="VB" Codebehind="Logout.ashx.vb" 
Class="GMR.eFelix.Web.Logout" %>

The ASHX code-behind page would be a normal class file.

Monday, March 21, 2005 7:28:16 PM UTC :: Filed Under Misc

Okay, so I stole the following article from MSN’s Women site, but I wanted to keep the information handy in case I need it in the future!

Secrets of Super-Happy Couples

 

Twelve ways to keep your relationship thriving.


Smart Strategies

Why do some couples seem so head-over-heels? It's not that their lives are any easier or more perfect than yours-- but they do know how to keep the daily grind from eroding their relationship. Get some of what they have by incorporating these happy-couple strategies into your love life.

1. Fall in love all over again. Make a conscious decision to be in love. The more you act as if you are in love, the more you will feel like you are.

2. Remember the good times. Treat your partner like you did at the beginning of your relationship. Make a list of all the things you used to enjoy doing together and add any new fantasies to the list. Plan for them and make them happen.

3. Help your partner feel more loved and secure in your love so that he or she can open up to you and express feelings and ideas without fear of being attacked or judged. Compliment, praise, give a hug. Small gestures make the grandest statements.

4. Don't make unilateral decisions. You're a team in many ways, so act like one. Check in and make decisions together about things large and small. Be willing to compromise.

5. Be present. Train your mind to stay in the moment -- not at work, thinking about the new color you want to paint your kitchen, or how it's time to take the dog to the vet.

6. Pay attention to your physical appearance. Take the time to stay in shape and look good for each other. It does matter.

7. Boost your compatibility. Couples in crisis focus on all the ways they are different, whereas those who are in love zero in on their similarities and think their differences are cute. Build compatibility by taking turns planning activities to do together. If you don't like your partner's choice, don't complain; it's your turn next.

8. Do not place blame. Replace blame and criticism with solutions and tenderness. Problem-solve together -- sit close, hold hands, touch each other's face or hair. Be playful. When was the last time you laughed together? Rent a comedy movie to tickle your funny bone.

9. Plan for sex. Spontaneity is great but smart couples know that good sex doesn't just happen. Like everything else, it takes time and planning.

10. Fact-find -- don't mind-read. You may think you know but you can't assume. You may believe he should know, but that's not fair, either. Always clear up misinterpretations and misunderstandings to make sure they don't throw you both off course.

11. Fight fair -- and by appointment only. Schedule a limited time to discuss a problem and confine your comments to that issue only. It's easier to relax and feel free to enjoy each other when you know you won't be ambushed by a litany of complaints and criticisms.

12. Prepare for checkouts. Even in the closest marriage, everyone needs time alone. Don't take it personally and don't make each other feel guilty if you need to spiritually and emotionally regroup. Just be sure to tell each other when you are checking out (max, one day) -- and when you're checking back in.

Navigation
On this page....
Search
Archives
<April 2005>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
Categories
Contact me
Send mail to the author(s) Contact Todd M. Taylor