Create and Fill a database table for US States

June 22, 2009 13:38 by XeroOne

It seems like every web project we develop requires the use of a States DropDownList.  Rather than manually binding an <asp:dropdownlist /> with individual <asp:ListItem /> tags, we use a database table of all US States containing both the state name, and the state abbreviation. We can then run a "Select All" query on this table and bind the results to the States drop down list.

Here's the creation script for the database table:

CREATE TABLE [dbo].[US_States](
    [StateID] [int] IDENTITY(1,1) NOT NULL,
    [StateName] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [StateAbbreviation] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_US_States] PRIMARY KEY CLUSTERED 
(
    [StateID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

This creates a database table named US_States with three fields: StateID (auto-incremented primary key), StateName (the full state name spelled out), StateAbbreviation (the two character state code).

Next we need to fill the table with all the US States. Here's the script:

insert into US_States (StateName,StateAbbreviation) values ('ALABAMA','AL');
insert into US_States (StateName,StateAbbreviation) values ('ALASKA ','AK');
insert into US_States (StateName,StateAbbreviation) values ('ARIZONA','AZ');
insert into US_States (StateName,StateAbbreviation) values ('ARKANSAS','AR');
insert into US_States (StateName,StateAbbreviation) values ('CALIFORNIA','CA');
insert into US_States (StateName,StateAbbreviation) values ('COLORADO','CO');
insert into US_States (StateName,StateAbbreviation) values ('CONNECTICUT','CT');
insert into US_States (StateName,StateAbbreviation) values ('DELAWARE','DE');
insert into US_States (StateName,StateAbbreviation) values ('DISTRICT OF COLUMBIA','DC');
insert into US_States (StateName,StateAbbreviation) values ('FLORIDA','FL');
insert into US_States (StateName,StateAbbreviation) values ('GEORGIA','GA');
insert into US_States (StateName,StateAbbreviation) values ('HAWAII ','HI');
insert into US_States (StateName,StateAbbreviation) values ('IDAHO','ID');
insert into US_States (StateName,StateAbbreviation) values ('ILLINOIS','IL');
insert into US_States (StateName,StateAbbreviation) values ('INDIANA','IN');
insert into US_States (StateName,StateAbbreviation) values ('IOWA','IA');
insert into US_States (StateName,StateAbbreviation) values ('KANSAS','KS');
insert into US_States (StateName,StateAbbreviation) values ('KENTUCKY','KY');
insert into US_States (StateName,StateAbbreviation) values ('LOUISIANA','LA');
insert into US_States (StateName,StateAbbreviation) values ('MAINE','ME');
insert into US_States (StateName,StateAbbreviation) values ('MARYLAND','MD');
insert into US_States (StateName,StateAbbreviation) values ('MASSACHUSETTS','MA');
insert into US_States (StateName,StateAbbreviation) values ('MICHIGAN','MI');
insert into US_States (StateName,StateAbbreviation) values ('MINNESOTA','MN');
insert into US_States (StateName,StateAbbreviation) values ('MISSISSIPPI','MS');
insert into US_States (StateName,StateAbbreviation) values ('MISSOURI','MO');
insert into US_States (StateName,StateAbbreviation) values ('MONTANA','MT');
insert into US_States (StateName,StateAbbreviation) values ('NEBRASKA','NE');
insert into US_States (StateName,StateAbbreviation) values ('NEVADA ','NV');
insert into US_States (StateName,StateAbbreviation) values ('NEW HAMPSHIRE','NH');
insert into US_States (StateName,StateAbbreviation) values ('NEW JERSEY','NJ');
insert into US_States (StateName,StateAbbreviation) values ('NEW MEXICO','NM');
insert into US_States (StateName,StateAbbreviation) values ('NEW YORK','NY');
insert into US_States (StateName,StateAbbreviation) values ('NORTH CAROLINA','NC');
insert into US_States (StateName,StateAbbreviation) values ('NORTH DAKOTA','ND');
insert into US_States (StateName,StateAbbreviation) values ('OHIO','OH');
insert into US_States (StateName,StateAbbreviation) values ('OKLAHOMA','OK');
insert into US_States (StateName,StateAbbreviation) values ('OREGON ','OR');
insert into US_States (StateName,StateAbbreviation) values ('PENNSYLVANIA','PA');
insert into US_States (StateName,StateAbbreviation) values ('PUERTO RICO','PR');
insert into US_States (StateName,StateAbbreviation) values ('RHODE ISLAND','RI');
insert into US_States (StateName,StateAbbreviation) values ('SOUTH CAROLINA','SC');
insert into US_States (StateName,StateAbbreviation) values ('SOUTH DAKOTA','SD');
insert into US_States (StateName,StateAbbreviation) values ('TENNESSEE','TN');
insert into US_States (StateName,StateAbbreviation) values ('TEXAS','TX');
insert into US_States (StateName,StateAbbreviation) values ('UTAH','UT');
insert into US_States (StateName,StateAbbreviation) values ('VERMONT','VT');
insert into US_States (StateName,StateAbbreviation) values ('U.S. VIRGIN ISLANDS','VI');
insert into US_States (StateName,StateAbbreviation) values ('VIRGINIA','VA');
insert into US_States (StateName,StateAbbreviation) values ('WASHINGTON','WA');
insert into US_States (StateName,StateAbbreviation) values ('WEST VIRGINIA','WV');
insert into US_States (StateName,StateAbbreviation) values ('WISCONSIN','WI');
insert into US_States (StateName,StateAbbreviation) values ('WYOMING','WY');

This data was obtained from the US Postal Service. It does contain additional US territories that the USPS ships to. If you don't want to include this data in your drop down list, then simply delete the rows from the database, or from the script prior to running it.

Once the data is stored in the database, the only thing left to do is bind it to the dropdownlist.  

Happy coding!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:       General | SQL Server
Links:   Permalink | Comments (0) | Comment RSSRSS comment feed
Actions:   Email this article | del.icio.us | Digg it! | StumbleUpon | DZone it! | reddit | /. | Kick it!

Creating ASP.NET Membership Tables

May 21, 2009 12:33 by XeroOne

The ASP.NET Membership provider makes it a peice of cake to add multiple user security to your web application.  There are a few steps involved to setting it up that aren't exactly obvious.  This article will explain how to creating the database tables and stored procedures in Microsoft SQL Server.

Open up a command prompt

Navigate to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\   

(the final directory may vary depending on your installation version of the .NET framework) 

The next step can be done in one of two ways.  You will be running the  AspNet_RegSql.exe utility.

To run in Wizzard mode simply run aspnet_regsql.exe and then follow the prompts to create your database

I prefer to automate this process.  To do so, all you need to do is provide the parameters to create the tables. They are as follows:
 
  • -E = Authenticates using the Windows credentials of the currently logged-in user. 

  • -S localhost = runs against the local installation of SQL Server.

  • -d database = specify the database name to run against

  • -A all = the A option stands for "add", and the all parameter means install all features (Membership, Role management, Profile, Web Parts personalization, Web events)

So the final command should come out looking like:

aspnet_regsql.exe -E -S localhost -d database -A all

 

That's it.  Refresh your database and you should see all the new tables and stored procedures that were automatically created.  

To read the documentation on using the aspnet_regsql.exe utility visit: http://msdn2.microsoft.com/library/x28wfk74(en-us,vs.80).aspx

The next step is using these features.  I'll cover that in our next article.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:       SQL Server | ASP.NET Membership
Links:   Permalink | Comments (0) | Comment RSSRSS comment feed
Actions:   Email this article | del.icio.us | Digg it! | StumbleUpon | DZone it! | reddit | /. | Kick it!

About the Author

XeroOne Systems
This blog is dedicated to the various topics surrounding web development, specifically using ASP.NET, C#, MS SQL, HTML & CSS, XML, and the many JavaScript frameworks currently available (MooTools, JQuery, Scriptaculous, Prototype, Ext.Js, etc..)
Periodically we will share our knowledge and experience through this blog. We may post code samples, tips and tricks, shortcuts and workarounds, our reviews of new web technologies, and (from time to time) unrelated anecdotes.
Please contact us if you have an idea or suggestion you'd like us to write about. If you like what you've read, be sure to subscribe to our blog using your favorite RSS reader.

Latest Comments

Popular Tags