Tuesday, August 21, 2007

Can't truncate table with Foreign Key

When trying to truncate a table that is referenced by a Foreign Key I always get this message, even if the table referencing the primary table is empty.

Server: Msg 4712, Level 16, State 1, Line 1
Cannot truncate table 'actTouches' because it is being referenced by a FOREIGN KEY constraint.

To get around this problem I simply delete all rows from the table then re seed the identity column with 1.

Delete From [table]
DBCC CheckIdent ([table], RESEED, 1)

How to Insert Values into an Identity Column in SQL Server

Identity columns are commonly used as primary keys in database tables. These columns automatically assign a value for each new row inserted. But what if you want to insert your own value into the column? It's actually very easy to do.

The trick is to enable IDENTITY_INSERT for the table. That looks like this:


INSERT IdentityTable(TheIdentity, TheValue)
VALUES (3, 'First Row')


Here are some key points about IDENTITY_INSERT

* It can only be enabled on one table at a time. If you try to enable it on a second table while it is still enabled on a first table SQL Server will generate an error.
* When it is enabled on a table you must specify a value for the identity column.
* The user issuing the statement must own the object, be a system administrator (sysadmin role), be the database owner (dbo) or be a member of the db_ddladmin role in order to run the command.

Read the full article for more information. For instance the fact that this can modify the identity properties of your column depending on the values you update the table with.

Monday, August 20, 2007

SQL Server linked servers by IP

I struggled for quite a little while trying to execute this:
Select top 1 * From [xxx.xxx.xxx.xxx].[db].[owner].[table]

In SQL2005 Management Studio against a SQL2000 box.

And receiving this:
An error occurred while executing batch. Error message is: Processing of results from SQL Server failed because of an invalid multipart name "xxx.xxx.xxx.xxx.db.owner.table", the current limit of "4" is insufficient.

I was unable to link the servers by name since there was no DNS to resolve it.

I finally discovered that I didn't receive the error if I simply used SQL 2000's Query analyzer. I was a little surprised to find that this issue in backwards compatibility existed in the Management Studio. I have not had a chance to test a SQL 2005 linked server to see if an IP address causes the same issue on it, though I would hope that it wouldn't.

Note: I just discovered that I was able to use OPENQUERY in Management Studio to run my query from one SQL2000 server to a linked SQL2000 server.

SELECT * FROM OPENQUERY([xxx.xxx.xxx.xxx], 'SELECT * FROM [db].owner.[table]') AS tablename

Tuesday, August 14, 2007

DataGrid Default Paging

Paging in ASP.NET //Implement the EventHandler private void GridPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex = e.NewPageIndex; //Bind the DataGrid again with the Data Source DataGrid1.DataSource = dataSet1; DataGrid1.DataBind(); }

What is HanselMinutes?


What is Hanselminutes?

Hanselminutes is a weekly audio talk show with noted web developer and technologist Scott Hanselman and hosted by Carl Franklin. Scott discusses utilities and tools, gives practical how-to advice, and discusses ASP.NET or Windows issues and workarounds.

Monday, August 13, 2007

how to untar/tar

To get the files out of a tarball, you can use the following commands:

tar xvf something.tar

If the tarball has also been gzipped (compressed), you can use the following command:

tar xvfz something.tar.gz

The biggest thing that I always forget when trying to use this command is that it doesn't accept the "-" prefix before its modifiers like most other linux commands.

If you need to create a tar file
tar -cvzf name.tgz subdir
the 'z' is optional and gzips the file.
the 'v' is optional and gives a verbose output.

Wednesday, August 8, 2007

The ASP.NET Page lifecycle

15 Seconds : The ASP.NET Page Life Cycle When a page request is sent to the Web server, whether through a submission or location change, the page is run through a series of events during its creation and disposal. When we try to build ASP.NET pages and this execution cycle is not taken into account, we can cause a lot of headaches for ourselves. However, when used and manipulated correctly, a page's execution cycle can be an effective and powerful tool. Many developers are realizing that understanding what happens and when it happens is crucial to effectively writing ASP.NET pages or user controls. So let's examine in detail the ten events of an ASP.NET page, from creation to disposal. We will also see how to tap into these events to implant our own custom code. The events that get fired are: 1. Object Initialization 2. Load Viewstate Data 3. LoadPostData Processes Postback Data 4. Object Load 5. Raise PostBack Change Events 6. Process Client-Side PostBack Event 7. Prerender the Objects 8. ViewState Saved 9. Render To HTML 10. Disposal

Excerpt from another page I found helpful:
So, dynamically added controls must be programmatically added to the Web page on each and every page visit. The best time to add these controls is during the initialization stage of the page life cycle, which occurs before the load view state stage. That is, we want to have the control hierarchy complete before the load view state stage arrives. For this reason, it is best to create an event handler for the Page class's Init event in your code-behind class, and add your dynamic controls there.
Note You may be able to get away with loading your controls in the Page_Load event handler and maintaining the view state properly. It all depends on whether or not you are setting any properties of the dynamically loaded controls programmatically and, if so, when you're doing it relative to the Controls.Add(dynamicControl) line. A thorough discussion of this is a bit beyond the scope of this article, but the reason it may work is because the Controls property's Add() method recursively loads the parent's view state into its children, even though the load view state stage has passed.
Shortcut Summary:
1. Object Initialization
2. Load Viewstate Data (void LoadViewState( object o ) )
3. LoadPostData Processes Postback Data
4. Object Load (void onload(EventArgs e) )
5. Raise PostBack Change Events
6. Process Client-Side PostBack Event (void RaisePostBackEvent (string eventArgument) )
7. Prerender the Objects
8. ViewState Saved (void RaisePostDataChangeEvent() )
9. Render To HTML
10. Disposal (void OnPreRender( EventArgs e ) )

Tuesday, August 7, 2007

CAST and CONVERT (T-SQL) - datetime syntax



- 0 or 100 (*) Default mon dd yyyy hh:miAM (or PM)
1 101 USA mm/dd/yy
2 102 ANSI yy.mm.dd
3 103 British/French dd/mm/yy
4 104 German dd.mm.yy
5 105 Italian dd-mm-yy
6 106 - dd mon yy
7 107 - mon dd, yy
8 108 - hh:mm:ss
- 9 or 109 (*) Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM)
10 110 USA mm-dd-yy
11 111 JAPAN yy/mm/dd
12 112 ISO yymmdd
- 13 or 113 (*) Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 20 or 120 (*) ODBC canonical yyyy-mm-dd hh:mi:ss(24h)
- 21 or 121 (*) ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h)
* The default values (style 0 or 100, 9 or 109, 13 or 113, 20 or 120, and 21 or 121) always return the century (yyyy).

** Input when converting to datetime; Output when converting to character data.

CONVERT (data_type[(length)], expression [, style])

Monday, August 6, 2007

Javascript shorthand if statement syntax

Javascript Short-Hand IF Within Object Notation : Kinky Solu... var myVar = ( intDayOfWeek == 6 ) ? 3 : 0; Just a quick reminder for myself on how the short hand if statement syntax works for languages like php, javascript, and c#.

Wednesday, August 1, 2007

Password Reveal

A good little password finding utility that works on WinXP. It works by revealing the *****'d text that typically hides the text in a password field. This is not a password cracker.

Warning: It does try and connect to the internet when run for some unknown reason.