Archives

Archives / 2008 / February
  • Custom MembershipProvider ConnectionStringName Usage

    By Nannette Thacker

    This is an issue that seems to confuse a lot of new developers. How do you use the "connectionStringName" defined within the membership provider area of the web.config file within a custom membership provider? Big mouthful, eh? Okay, let's break it down.

    Within the web.config file, you may define your connection strings. For our example, below I am setting up a connection string to a SQL Server database within my project's App_Data folder, but it could be a connection string to a remote database on a database server as well.

  • Importing an Excel Spreadsheet Using Typed DataSets and TableAdapters

    By Nannette Thacker

    In this tutorial, learn how to import data from an Excel Spreadsheet to a Database by creating a tiered application architecture using Visual Studio's Typed DataSets and TableAdapters. The application consists of an ASP.NET page that enables a user to upload an Excel spreadsheet, view the data in the spreadsheet, and import the data from the spreadsheet into the application's database. A Zip file with the complete source code, Excel Spreadsheet, and SQL Server Database is available for download.

    The article series hosted on 4guysfromrolla.com begins with Importing an Excel Spreadsheet Using Typed DataSets and TableAdapters: Building the Database.

    Download the Application in ZIP Format

    May your dreams be in ASP.NET!

    Nannette Thacker

  • Creating a CSS Hyperlinked Image for Themes and Using ResolveClientUrl

    By Nannette Thacker

    In ASP.NET, you may define multiple themes to be used within your web applications. In this example, let's say you have a theme based around the color purple, and another theme based around the color blue. When using your purple theme, you have a special image, let's say a logo, that is designed with purple colors. But when using the blue theme, you want your logo to change to one that uses blue colors. This tutorial will show you how to setup your code within your master page so that you will pull this logo image from your cascading style sheet, rather than hard coding it within your master page or web form.

    Normally, when defining a hyperlinked image, one would simply type an anchor tag and an image tag like so:

  • OnClick and Handles Caution: Events Firing Twice

    By Nannette Thacker

    If you're new to ASP.NET, let me offer you a word of caution in regard to using the onclick event within your controls and the Handles key word within your procedure declarations. To demonstrate this, let's create a web form and add these two controls:

  • Retrieving Values from a DataReader

    By Nannette Thacker

    There are several ways to retrieve values from a DataReader.

    The first method is by using the index and looping through the fields in order as retrieved within the Select statement. Starting with the 0 index, you may use the GetValue() Function.

    The following code snippets are in VB. A C# snippet is provided at the bottom.

    reader = objCommand.ExecuteReader()
    While reader.Read()
    
    If Not reader.GetValue(0) Is DBNull.Value Then _
        lastname = reader.GetValue(0)
    If Not reader.GetValue(1) Is DBNull.Value Then _
        firstname = reader.GetValue(1)
    
    End While
    reader.Close()
    However, this makes the code harder to read, and if a field is added or removed from the query, it could break your code. So let's look at how to retrieve the values by field names.

    If your reader is using an OleDbDataReader, we use the reader.Item function.

    If Not reader.Item("lastname") Is DBNull.Value Then _
        lastname = reader.Item("lastname")
    If Not reader.Item("firstname") Is DBNull.Value Then _
        firstname = reader.Item("firstname")
    If your reader is using a SqlDataReader, we use the reader.GetItem function.

    If Not reader.GetItem("lastname") Is DBNull.Value Then _
        lastname = reader.GetItem("lastname")
    If Not reader.GetItem("firstname") Is DBNull.Value Then _
        firstname = reader.GetItem("firstname")
    Here is a C# example:

    if (reader["lastname"] != DBNull.Value)
        lastname = reader["lastname"].ToString();
    A reader of my column, Zac, suggests that reader("lastname") instead of reader.Item("lastname") will also work.

    He is correct. For the DataReader class implementation, Item is the default property and may be left out. You may access an indexer by using the () operator on the base class object or you may access an indexer by using the Item property.

    Another reader of my column, Marneus, pointed out the GetOrdinal option as well. He said "there is a performance hit, each time you use the reader("lastname"), it has to check the meta datas to get the column number."

    Readers may refer to this document on the GetOrdinal()

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getordinal.aspx

    Please note this comment on efficiency:

    "Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call GetOrdinal within a loop. Save time by calling GetOrdinal once and assigning the results to an integer variable for use within the loop."

    From the article, here is the usage:

    ' Call GetOrdinal and assign value to variable.
    Dim customerID As Integer = reader.GetOrdinal("CustomerID")
    
    ' Use variable with GetString inside of loop.
    While reader.Read()
        Console.WriteLine("CustomerID={0}", reader.GetString(customerID))
    End While
    Another reader, AndrewSeven, pointed out the cost for hitting the reader twice for each field. He suggests:

    object val = reader["lastname"];
    if (val != DBNull.Value)
    {
       lastname = val;
    }
    And also comments "If your concern is readability, you should probably encapsulate the null test so you can write:

    lastname = GetValueFromReader(reader,"lastname");"
    Travis responded with this suggestion: "Just do this, it handles nulls nicely unlike .ToString():"

    lastname = Convert.ToString(reader["lastname"]);
    So I tested his theory by creating columns that would not allow null values, and imported columns with null values. In VB it is written like so:

    lastname = Convert.ToString(reader("lastname"))
    firstname = Convert.ToString(reader("firstname"))
    That is much cleaner! Thanks everyone for your great input!

    May your dreams be in ASP.net!

    Nannette Thacker

  • Slick Rank Checker and Lake Quincy Ads

    I noticed as I peruse the web that more and more of the articles websites I visit are covered with ASP.NET web ads. I wanted some for my site, Shining Star Software, so did a search on the asp.net blogs and found mention of the Lake Quincy ads. I thought I'd give them a try so applied.

    I was asked my site's rank. Well, I don't track my site's rank with any tools, so I found this slick tool that will tell you your site's rank on Google and Alexa: