Sunday, December 27, 2009

.NET 3.5 Features

I just started on reading .Net 3.5 features and impressed with following great things, and listing for guys who want to start exploring .net 3.5.

1) LINQ - Gives differant ways of writing Data Access code,With new programming style. .Net uses built-in to execute this.

2) JS Intellisense - This was delivered in VS 2008. Same like our Server-Side code (i.e c#), even in JS we have auto-complete of methods and properties available for an JS objects. Explore more on :http://weblogs.asp.net/scottgu/archive/2007/06/21/vs-2008-javascript-intellisense.aspx

3) JS Debugger - Even this delivered in VS 2008. As like server-side debugging we can put breakpoint on any javascript code block and VS will execute that block when page get rendered. Now no need to use 'debugger' keyword on any JS block.This will work ASp.net 2.0 web page edited in VS 2008. Expore more on : http://weblogs.asp.net/scottgu/archive/2007/07/19/vs-2008-javascript-debugging.aspx

4)Debugging .Net Fx Classes - Using VS 2008, we can debug .Net framework class libraries by simple pression F11, With previous version by doing same will take to a meta-data information, but now will take to source code. This is very great feature and give good knowledge of exploring Microsoft development standards. Bit excited with feature .. !!!. Explore more on http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx.

Will keep editing these post by reading feature one-by-one.

Thanks,
Guna.

Monday, November 2, 2009

Converting any File to Byte[] and Byte[] to File.

Below C# code takes filepath, where file located as input parameter and gives byte array of that file.

public byte[] ReadFile(string sPath)
{
FileStream fStream = null;
try
{byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
using (fStream)
{
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
}
return data;
}
catch { throw; }
finally { fStream.Dispose(); }
}


Below code snippets takes Byte array and File extention as input parameter and stores the file in application path under temp folder.

public byte[] ReadFile(string sPath)
{
FileStream fStream = null;
try
{byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
using (fStream)
{
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
}
return data;
}
catch { throw; }
finally { fStream.Dispose(); }
}

Sunday, November 1, 2009

Considering Comments in ASP.Net webpage.

“Even small thing make a big difference”, to prove this, commenting content in ASP.Net webpage page can be done using two methods.


Important and considerable difference between these two comments were, with HTML comments, content commented get rendered when web page requested and result in increasing download size. So its good to use ASP.NET comments.

Wednesday, October 21, 2009

SQL Script to find total records of all tables.

This T-SQL script will return number of rows for each table in a database and total records across all tables. This will be very much useful for SQL admin to watch the data growth in database.

SELECT Row_Number() over (Order by sysindx.rowcnt DESC ) AS SlNo

,sysObj.name AS ‘Table Name’,sysindx.rowcnt AS ‘Row Count’

FROM sysobjects sysObj

inner join sysindexes sysindx

on sysindx.id = sysObj.id

WHERE sysindx.indid IN(0,1)

AND sysObj.xtype = ‘u’

AND sysObj.name NOT IN(’sysdiagrams’)

COMPUTE SUM(sysindx.rowcnt);



Note: This will work only on MSSQL 2005 and later. Because Compute and Row_Number were introduced new in 2005.

Undocumented SQL pwdcompare function.

There will be some occasion, where we need to check SQL authentication programmatically, here is one SQL undocumented function pwdComapare, will return ‘1′ if password match with password hash in SQL server.


This SQL stuff will explains rest.

Declare @loginName as varchar(100)

Declare @Password as varchar(50)

set @loginname = ‘guna’

set @Password = ‘gunapwd123′

IF (select pwdcompare(@Password,Password_hash) from sys.sql_logins

where name = @loginName) = 1

Select ‘Success’

ELse

Select ‘Failure’

Friday, October 16, 2009

Opening Windows Image Viewer programmatically - C#.NET

Below Code open windows defalut Image and photo viewer from .Net

public void OpenImageFile(string FilePath)
{
Process objProcess = new Process();
objProcess.StartInfo.FileName = “rundll32.exe”;
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.Arguments = ” shimgvw.dll ImageView_Fullscreen ” + FilePath;
objProcess.Start();
objProcess.WaitForExit();
objProcess.Close();
}

Thursday, October 15, 2009

Warn update without WHERE clause

Create below SQL Trigger for table need to have this check.
T-SQl Script:
CREATE TRIGGER [dbo].[trgWarnUpdate]
ON [dbo].[Employee]
FOR UPDATE AS
BEGIN
DECLARE @Count int
SET @Count = @@ROWCOUNT;
IF @Count >= (SELECT top 1 rows
FROM sysindexes where name like 'pk%'
and id = object_id('Employee'))
BEGIN
RAISERROR('Updating all records restricted. To update all records disable this trigger.',16,1)
ROLLBACK TRANSACTION
RETURN;
END
END
GO


Note:Rename Emplyoee with respective SQL table. Assuming table has primary key and same get checked.

SQL Script:No of days in a month.

Below T-SQL script return no. of days for given date.

Declare @InputDate as datetime

Set @InputDate = ‘15-oct-2009′
Set @InputDate = cast(cast(month(@Inputdate) as char) + ‘-01-’ + cast(year(@InputDate) as char) as datetime)Select day(dateadd(d,-1,dateadd(M,1,@InputDate))) as ‘Days’