Tipsy little box

Just another weblog

Page referrer November 29, 2010

Filed under: Tips and Tricks — evacion @ 6:44 pm

Request.ServerVariables["HTTP_REFERER"]

 

Add Item to Array August 26, 2010

Filed under: ASP.NET, C#,Tips and Tricks — evacion @ 5:05 pm

There is no Add method for basic arrays.
//Add an item to the end of an existing array
string[] ar1 = new string[] {“I“, “Like“, “To“}

// create a temporary array with an extra slot
// at the end

string[] ar2 = new string[ar1.Length + 1];

// add the contents of the ar1 to ar2 at position 0
ar1.CopyTo(ar2, 0);

// add the desired value
ar2.SetValue(“Code.”, ar1.Length);

// overwrite ar1 with ar2 the contents of ar1 should now be {“I”, “Like”, “To”, “Code.”}
ar1 = ar2;

Or:
Array.Resize(ref ar1, ar1.Length + 1);
Array.Copy(new object[] { “Code.” }, 0, ar1, ar1.Length – 1, 1);

 

Slow SQL Server Delete April 8, 2010

Filed under: Microsoft SQL,Tips and Tricks — evacion @ 10:30 am

If you have referential integrity set up between your tables, you need to ensure that all foreign key columns are indexed – otherwise when you delete a record from a primary key table, referential integrity checks will cause table scans on the referring foreign key tables, and scans are terribly slow compared to index lookups.

 

Running Days March 18, 2010

Filed under: Tips and Tricks — evacion @ 5:03 pm

 

Get just the time portion of a datetime value January 4, 2010

Filed under: Microsoft SQL,Tips and Tricks — evacion @ 2:38 pm

DECLARE @day DATETIME
SET
@day = ’12/12/2009 23:59:00′

SELECT CONVERT( VARCHAR(5), @day, 108 )

 

Faster way to get the date without time January 4, 2010

Filed under: Microsoft SQL,Tips and Tricks — evacion @ 2:31 pm

DECLARE  @day DATETIME
SET
@day = ’12/12/2009 09:25:00′

SELECT dateadd(dd,datediff(dd,0,@day),0)

used in : http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64755

There is other info about date converions here.
Date/Time Info and Script Links
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762

 

DataTable – Serializable January 20, 2009

Filed under: Tips and Tricks — evacion @ 5:22 pm
Tags:

Problems using an XML Web service that returns a DataTable
Symptons:
When you browse to the URL of an XML Web service that returns a DataTable from one of its Web methods, you may receive the following error message:
System.Data.DataRelation cannot be serialized because it does not have a default public constructor.
You may also see an error message similar to the following when you try to set a Web reference to this XML Web service in the Visual Studio .NET integrated development environment (IDE):
Internal Server Error. Unable to request “http://localhost/Webservice1/Service1.asmx?WSDL”. The server responded with error code “ProtocolError”.
With the .NET Framework 1.1 Service Pack 1 (SP1), you may receive the following error message:
System.NotSupportedException: Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface.
You may also receive a blank page instead of any of these error messages if Show friendly HTTP error messages is enabled in Microsoft Internet Explorer. By default, the Show friendly HTTP error messages option is enabled.

If you try to add a Web reference to an XML Web service that returns a DataTable, you may receive the following error message:
The document at the url http:///vdir/service1.asmx was not recognized as a known document type.

Cause
The DataTable, DataRow, DataView, and DataViewManager objects cannot be serialized and cannot be returned from an XML Web service. To return less than a complete DataSet, you must copy the data that you want to return to a new DataSet.

Solution:
To resolve this issue, return a DataSet instead of a DataTable. DataSet objects can contain one or more DataTable objects.
Datatable is not returned through webservice beacause if you could, you could use for other non .net applications–and it a matter of policy I guess. But from the webservice prospective this should be considered a poor practice as well as from a service-orientation perspective. One of the tenets of service-orientation is that services share contract and schema … and nothing else. Passing a DataSet between Web services violates this tenet, and the result is that it will only work so long as both endpoints are always .NET platforms. (Microsoft products).
A better solution would be to pass entity data that is domain- or business-specific. Not a DataTable, but a set of data that has an actual XSD schema generated in the WSDL for the Web services so that it can be used by clients on any platform. (Microsoft doesnt want folks to use any other platform and so any support thereof is removed)

 

Removing Duplicate Records using SQL Server 2005 November 19, 2008

Filed under: Tips and Tricks — evacion @ 12:05 pm

Duplicate records can occur numerous ways, such as loading source files too many times, keying the same data more than once, or from just bad database coding. Having a primary key on your table (and you always should have one) can will in the removal of the duplicate records, but even a primary key it is never a fun task to have handed to you to complete.

I will demonstrate how you can use a common-table expression (CTE) in 2005 to easily remove duplicate entries from a table:

;WITH PendingCalculationCTE(EmployeeId, Start, Finish, [Timestamp], TransactionId, LockFlag, Ranking)
AS(
SELECT EmployeeId, Start, Finish, [Timestamp], TransactionId, LockFlag,
Ranking = DENSE_RANK()
OVER(PARTITION BY EmployeeId, Start, Finish, [Timestamp], TransactionId, LockFlag
ORDER BY NEWID() ASC)
FROM PendingCalculation
)

DELETE FROM PendingCalculationCTE WHERE Ranking > 1

 The script above defines my CTE. I am using a windowing function named DENSE_RANK to group the records together based on the EmployeeId, Start, Finish, [Timestamp], TransactionId, LockFlag fields, and assign them a sequential value randomly. This means that if I have two records with the exact same EmployeeId, Start, Finish, [Timestamp], TransactionId, LockFlag values, the first record will be ranked as 1, the second as 2, and so on.
Because a CTE acts as a virtual table, I am able to process data modification statements against it, and the underlying table will be affected. In this case, I am removing any record from the PendingCalculationCTE that is ranked higher than 1. This will remove all of my duplicate records.

DENSE_RANK – Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.

 

Count rows number from database tables November 12, 2008

Filed under: Tips and Tricks — evacion @ 10:30 am

SELECT [TableName] = so.name, [RowCount] = MAX(si.rows)
FROM sysobjects so, sysindexes si
WHERE so.xtype = ‘U‘ AND si.id = OBJECT_ID(so.name)
GROUP BY so.name
ORDER BY 1 ASC

 

Can’t View CHM Files in IE7 – A FIX September 24, 2008

Filed under: Tips and Tricks — evacion @ 3:36 pm

If you are having problems viewing CHM files (like the new WCF documentation CHM files), you might be seeing a blank IE screen when you click on any of the subject headings contained ni the file. If this is the case, right-click on the CHM file and select Properties from the provided menu. You can then click on the Unblock button at the bottom. This will then allow the CHM file to be viewed using IE as it should be.

 

 
Follow

Get every new post delivered to your Inbox.