Sunday, February 27, 2011

RESEED Table


SQL Command:
DBCC CHECKIDENT(Employees, reseed, 1073741824)

Result/Message:
Checking identity information: current identity value '44', current column value '1073741824'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Sunday, February 20, 2011

Retry Logic (Recursively)

Uploading a file to a webserver.

private string UploadFile(string name, string token, bool retry = true)
{
string publicUrl = null;

try
{
bool success = _service.Save(name, token, out publicUrl);
}
catch (WebException ex)
{
// Server will return a 401 (Unauthorized) response when the authorization token has expired.
// So will create a new token and retry this operation.
if (ex.Response != null && (ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
{
if (retry)
{
_logger.WarnFormat("Server returned 401 (Unauthorized), requesting new token and retrying operation...");

token = UpdateToken();

publicUrl = UploadFile(name, token retry: false); // try only one more time.
}
else
_logger.ErrorFormat("Server returned 401 (Unauthorized) again. File {0} could not be uploaded, please process it manually.", name);
}
else
throw ex;
}

return publicUrl;
}

Thursday, February 17, 2011

XML SQL Input Parameter

XML:



50
Jon
Doe
A


99
Jon
Connor
A


1235
James
Bond
B



SQL:

declare @Clients xml

set @Clients =
'

50
Jon
Doe
A


99
Jon
Connor
A


1235
James
Bond
B

'

declare @clientsTable as table
(
ID int,
Name varchar(20),
LastName varchar(20),
Class char(1)
)

-- IMPORTANT:
-- value in col.value MUST be lowercase, otherwise this INSERT will fail.
INSERT INTO @clientsTable (ID
,Name
,LastName
,Class)
SELECT col.value('id[1]', 'int') AS ID
,col.value('name[1]', 'varchar(20)') AS Name
,col.value('lastname[1]', 'varchar(20)') AS LastName
,col.value('class[1]', 'char(1)') AS Class
FROM @Clients.nodes('//Client') tab(col)


select * from @clientsTable

Result:

ID Name LastName Class
----------- -------------------- -------------------- -----
50 Jon Doe A
99 Jon Connor A
1235 James Bond B

Sunday, February 13, 2011

Google Search By Site

key words site:lorem.ipsum.com

Example:

Wednesday, February 9, 2011

Singleton C#

Non thread-safe

public class MyStorage
{
private static MyStorage instance;

public static MyStorage Instance
{
get {
if (instance == null)
instance = new MyStorage();

return instance;
}
}

private MyStorage()
{
// do stuff
InitStuff();

bool result;
_overwrite = bool.TryParse( ConfigurationManager.AppSettings["Overwrite"], out result) ? result : false;
}

...
}

Friday, February 4, 2011

FileInfo Get File Bytes

1. approach

FileStream fs = myfileInfo.OpenRead();
int bytesCount =(int)fs.Length;
byte[] bytes = new byte[bytesCount];
int bytesRead = fs.Read(bytes, 0, bytesCount);
fs.Close();

2. approach

byte[] bytes = System.IO.File.ReadAllBytes(myfileInfo.FullName);

Wednesday, February 2, 2011

CSS Hacks IE7 IE8 and Chrome

Firefox: Padding Top = 2 pixels
IE7 and IE8: Padding Top = 1 pixels
Chrome: Padding Top = 1 pixels

table.main td#infobox div#container > div#legend {
float: right;
padding-top: 2px;
padding-right: 3px;
*padding-top: 1px; /* IE7 hack*/
padding-top: 1px\0/; /* IE8 hack, this hack must go after the rules for all another browser */
}

/* Chrome hack for the preceding style */
@media screen and (-webkit-min-device-pixel-ratio:0) {
table.main td#infobox div#container > div#legend {
padding-top: 1px;
}
}