Search

just show me the code

Wednesday, November 5, 2008

populating a database in ruby

Ruby in has a ton of cool database processes. There are Gems you can include that will randomly populate your database with test data. This free screencast shows how it is done.
http://railscasts.com/episodes/126-populating-a-database

Tuesday, November 4, 2008

Linq to xml

Linq (Language Integrated Query) works on xml too

<content>
  <gallery Name="All Videos">
    <video VideoClip="20081014.flv" Title="Orlando, Florida" Copy="Feb 12, 2009"/>
    <video VideoClip="20081121.flv" Title="Atlanta, Georgia" Copy="Feb 12, 2009"/>
  </gallery>
</content>



protected void LoadGallaryList()
{
    string xmlFile = this.Server.MapPath("~/Flash/_xml/video.xml"); 
    XDocument xmlDoc = XDocument.Load(xmlFile); 
    var videos = from v in xmlDoc.Descendants("video") // .Elements("video")
                 select new 
                 {
                     VideoClip = v.Attribute("VideoClip").Value,
                     Title = v.Attribute("Title").Value,
                     Date = v.Attribute("Copy").Value,
                 }; 
    foreach (var v in videos)
    {
        lblCurrent.Text += "title:" + v.Title + " VideoClip:" + v.VideoClip + " Date:" + v.Date + "<br/>";
    }
}

output:
title:Orlando, Florida VideoClip:20080914.flv Date:Feb 12, 2009
title:Atlanta, Georgia VideoClip:20081121.flv Date:Feb 12, 2009

Monday, November 3, 2008

jQuery in ASP.NET on Alt.NET

Alt.Net podcast is now on my regular playlist. This one episode gives a pretty good insight on how microsoft is changing with the times. Listen here.

Sunday, November 2, 2008

ASP.NET MVC Storefront Starter Kit Videos

I have been watching these videos on asp.net presented by Rob Conery. Rob does an awesome job of showing you his thought process. You can check them out here.
The videos cover
  • ASP.Net MVC framework
  • The Repository Pattern (sort of)
  • Pipes and Filters
  • Linq To Sql
  • Routing
  • TDD (test driven development)
  • Mocking
  • Dependency Injection

Saturday, November 1, 2008

TableMaker

http://www.bagism.com/tablemaker/
copy sql results to this page to create an html table like this
142 BISMARCK, ND BIS 3 Central States
66 CEDAR RAPIDS, IA CDR 3 Central States
129 COLUMBIA, MO CBM 3 Central States
15 Denver, CO DEN 3 Central States
114 DES MOINES, IA DSM 3 Central States
143 FARGO, ND FAR 3 Central States
104 FORT SMITH, AR FSM 3 Central States
59 GRAND ISLAND, NE GDI 3 Central States
138 GREAT BEND, KS GRB 3 Central States
16 Kansas City, KS KCY 3 Central States
133 LA CROSSE, WI LAC 3 Central States
105 LITTLE ROCK, AR LRA 3 Central States
139 MASON CITY, IA MAC 3 Central States
92 MILWAUKEE, WI MWK 3 Central States
17 Minneapolis - St. Paul, MN MSP 3 Central States
113 AUSTIN, TX AUS 4 Gulf-States

Friday, October 31, 2008

Omniture Tracking with Flash

/* START: Omniture ActionSource Configuration*/
import com.omniture.AS2.ActionSource;
var s;
function configActionSource() {
    s = new ActionSource();
    /* Specify the Report Suite ID(s) to track here */
    s.account = "XXXX";
    /*You may add or alter any code config here: see documentation for more variables
    */
    s.pageName = "Sample App d1.2: State 1";
    s.pageURL = "";
    s.charSet = "ISO-8859-1";
    s.currencyCode = "USD";
    /* Turn on and configure ClickMap tracking here */ 
    s.trackClickMap = true;
    s.movieID = "sample_dynamic_object_1_2";
    /* Turn on and configure debugging here */
    s.debugTracking = true;
    s.trackLocal = true;
    /* WARNING: Changing any of the below variables will cause drastic changes
    to how your visitor data is collected. Changes should only be made
    when instructed to do so by your account manager.*/
    s.dc = 112;
    /* onLoad Transaction*/
    s.delayTracking = 500;
    // half second delay for tracking after load
    s.track();
}
configActionSource();
/* END: Omniture ActionSource Configuration*/

open up fiddler and watch your tracking


Thursday, October 30, 2008

Keep the scrollbar

html { height: 100%; margin-bottom: 1px; }

if you have a centered and the page is too short for a scroll bar, when it gets longer the scroll bar appears and you page shifts the left a few pixels. This post will help you avoid that problem.
http://www.hicksdesign.co.uk/journal/forcing-scrollbars-now-even-better

bind a gridview column to a method

bind a attribute of the ItemTemplate to a method

<asp:TemplateField HeaderText="Deleted Ind" SortExpression="DeletedInd"> 
    <ItemTemplate>
        <asp:CheckBox ID="cbDeletedInd" runat="server" 
            Checked='<%# this.GetNullableBool(Eval("DeletedInd"))  %>' 
            Enabled="false" />
    </ItemTemplate>
</asp:TemplateField>


put the method in your codebehind (this could be a bind utility method)

protected bool GetNullableBool(object o)
{
    bool bReturn = false;
    Nullable<bool> b = o as Nullable<bool>;
    if (b != null)
    {
        if (b == true)
        {
            bReturn = true;
        }
    }
    return bReturn;
}


this method returns false if the bool is false or null
otherwise it returns true

now we use the RowDataBound to format that row


protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
    CheckBox c = (CheckBox)e.Row.FindControl("cbDeletedInd");
    if (c != null && c.Checked)
    {
        e.Row.ForeColor = System.Drawing.Color.LightGray;
    } 
}


results: (the deleted rows are now grey)


creating change scripts in SQL Server Management Studio

when you change the schema of a database, Management Studio makes it easy to create your change script.

first make your change.
then click the generate script button





now you can save it to a sql script file.



this is what the file looks like. notice when you reorder the columns it creates a temporary table copies all the data. drops the original table, then creates the new table with the right column order. the copies the data back to the new table.


/*
   Thursday, October 30, 20088:49:47 AM
   User: 
   Server: stpdev
   Database: ODFL
   Application: 
*/
 
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.tblEmailType
    DROP CONSTRAINT DF_tblEmailType_LeadDays
GO
ALTER TABLE dbo.tblEmailType
    DROP CONSTRAINT DF_tblEmailType_SendToNonRegistered
GO
ALTER TABLE dbo.tblEmailType
    DROP CONSTRAINT DF_tblEmailType_PostEvent
GO
ALTER TABLE dbo.tblEmailType
    DROP CONSTRAINT DF_tblEmailType_active
GO
ALTER TABLE dbo.tblEmailType
    DROP CONSTRAINT DF_tblEmailType_time_stamp
GO
CREATE TABLE dbo.Tmp_tblEmailType
    (
    EmailTypeID int NOT NULL IDENTITY (1, 1),
    Name nvarchar(50) NOT NULL,
    LeadDays int NULL,
    ExactTargetTemplate nvarchar(255) NULL,
    SendToRegistered bit NOT NULL,
    PostEvent bit NOT NULL,
    active bit NULL,
    MyNewColumn nchar(10) NULL,
    time_stamp smalldatetime NULL
    )  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_tblEmailType ADD CONSTRAINT
    DF_tblEmailType_LeadDays DEFAULT ((0)) FOR LeadDays
GO
ALTER TABLE dbo.Tmp_tblEmailType ADD CONSTRAINT
    DF_tblEmailType_SendToNonRegistered DEFAULT ((0)) FOR SendToRegistered
GO
ALTER TABLE dbo.Tmp_tblEmailType ADD CONSTRAINT
    DF_tblEmailType_PostEvent DEFAULT ((0)) FOR PostEvent
GO
ALTER TABLE dbo.Tmp_tblEmailType ADD CONSTRAINT
    DF_tblEmailType_active DEFAULT ((1)) FOR active
GO
ALTER TABLE dbo.Tmp_tblEmailType ADD CONSTRAINT
    DF_tblEmailType_time_stamp DEFAULT (getdate()) FOR time_stamp
GO
SET IDENTITY_INSERT dbo.Tmp_tblEmailType ON
GO
IF EXISTS(SELECT * FROM dbo.tblEmailType)
     EXEC('INSERT INTO dbo.Tmp_tblEmailType (EmailTypeID, Name, LeadDays, ExactTargetTemplate, SendToRegistered, PostEvent, active, time_stamp)
        SELECT EmailTypeID, Name, LeadDays, ExactTargetTemplate, SendToRegistered, PostEvent, active, time_stamp FROM dbo.tblEmailType WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_tblEmailType OFF
GO
ALTER TABLE dbo.tblUserEventEmail
    DROP CONSTRAINT FK_tblUserEventEmail_tblEmailType
GO
DROP TABLE dbo.tblEmailType
GO
EXECUTE sp_rename N'dbo.Tmp_tblEmailType', N'tblEmailType', 'OBJECT' 
GO
ALTER TABLE dbo.tblEmailType ADD CONSTRAINT
    PK_tblEmailType PRIMARY KEY CLUSTERED 
    (
    EmailTypeID
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.tblUserEventEmail ADD CONSTRAINT
    FK_tblUserEventEmail_tblEmailType FOREIGN KEY
    (
    EmailTypeID
    ) REFERENCES dbo.tblEmailType
    (
    EmailTypeID
    ) ON UPDATE  NO ACTION 
     ON DELETE  NO ACTION 
 
GO
COMMIT


Tuesday, October 28, 2008

printing using javascript

This script will bring up the print dialog on load of a web page.
<body class="" onLoad="self.print()"> 
thanks to Ian for the tip.

Contributors