Thursday, April 28, 2011

Making asp fileupload control inner text box readonly in VS2008

one of the easy way is in the file upload control put the following attribute....
id="someid" runat="server" contenteditable="false"
this will not visible in the intellisence but it works...

ASP.NET Regenerate .designer.cs

In this simple post, I want to share a quick tip with Visual Studio.  You probably encountered this problem before: you are designing an ASPX or ASCX form in Visual Studio and then for some reason an element you just added in the ASPX/ASCX is not available in the code behind.

If you are lucky you know exactly what you changed in the page to cause this problem and you fix it.  However, if you’re opening a file that hasn’t been worked on in a long time (maybe someone else did the most recent modification), it becomes difficult to know what exactly cause the .designer.cs automatic generation fail. There are a few common scenarios for this such as invalid HTML, missing or duplicate declarations, or unresolved references. Finding the core issue by hand is difficult because the designer does not let you know what the specific issue is.

Small Tip

To regenerate the .designer.cs file for a ASPX/ASCX, first delete the .designer.cs file, then right click on the ASPX/ASCX and click “Convert to Web Application”.  This will probably give you an error message that will help you find the root of the problem.  I noticed that there’s a difference in the error message you might get here versus when the file is opened in Design mode in Visual Studio.

For example, we had a .ASCX file where the .designer.cs file was not being generated when we modified it.  We could not figure out what the error was until we discovered this tip because Visual Studio did not give us any feedback.  Once I deleted the .designer.cs and ran “Convert to Web Application” on the file, I was prompted with an error message that informed me that we had a <%@ Register /> declaration in our ASCX that was also declared in our web.config file (and this was creating a conflict even if they both pointed to the same location).  Visual Studio could benefit from better error reporting for this particular scenario.

Monday, April 25, 2011

Textbox to allow only special (#,',-)/ Alphanumeric characters using Javascript

 function EnableAlphaNumericSpecial(event) {
        // 65 - 90 = smaller case 'a',  32 - space, 48 - 58 - numeric - (0 - 9),  39 - ',35-#, 45- -

        var keyCode = event.charCode;              

        if (keyCode == undefined)
                keyCode = event.keyCode;

        if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (keyCode >= 48 && keyCode <= 57) || (keyCode == 32) || (keyCode == 39) || (keyCode == 45) || (keyCode == 35) || (keyCode == 0))
                event.returnValue = true;
            else {    //check if it is a IE browser or Firefox browser.
                if (window.event)
                    event.returnValue = false;
                else
                    event.preventDefault();
            }
    }


Thursday, April 21, 2011

Send email in a tabular format using SQL Server database mail


I start by creating a temporary table and populating it with some sample data, it is a list of top Tennis players along with their Rank, Name, Ranking Points and Country. This temporary table is used only in the example, in real time the temp table would not be required, instead you would use the actual database table which contains the data. 

The next section is responsible for converting the SQL Server table data that we created into a local variable "@xml".  In the select statement, each of the columns is going to be displayed as table data and rows in the email. 

In another local variable "@body" we add the required HTML tags and text that is displayed in the email, in this case "Tennis Rankings Info". Then we include the column headers of the table inside the email.  Remember that these table headers (column names) can be changed as per your convenience.  In the example code, the column names of the temp table have been used as table headers. In you wish to display "Player" instead of "Player Name", you can do that by changing the table header tag to <th> Player </th>. Finally the closing HTML tags are added. 

Once the HTML has been formatted I send the email using the system stored procedure sp_send_dbmail found in the msdb database and finally I drop the temporary table. 

In order to use the code below, against your database table, you will have to replace the table name and column names of your table wherever necessary. Also, you will need to change the mail profile name and email address that you want to use.

Here is the sample code.
CREATE TABLE #Temp 
( 
  [Rank]  [int],
  [Player Name]  [varchar](128),
  [Ranking Points] [int],
  [Country]  [varchar](128)
)


INSERT INTO #Temp
SELECT 1,'Rafael Nadal',12390,'Spain'
UNION ALL
SELECT 2,'Roger Federer',7965,'Switzerland'
UNION ALL
SELECT 3,'Novak Djokovic',7880,'Serbia'


DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)


SET @xml = CAST(( SELECT [Rank] AS 'td','',[Player Name] AS 'td','',
       [Ranking Points] AS 'td','', Country AS 'td'
FROM  #Temp ORDER BY Rank 
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))


SET @body ='<html><body><H3>Tennis Rankings Info</H3>
<table border = 1> 
<tr>
<th> Rank </th> <th> Player Name </th> <th> Ranking Points </th> <th> Country </th></tr>'    

 
SET @body = @body + @xml +'</table></body></html>'


EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL ALERTING', -- replace with your SQL Database Mail Profile 
@body = @body,
@body_format ='HTML',
@recipients = 'bruhaspathy@hotmail.com', -- replace with your email address
@subject = 'E-mail in Tabular Format' ;


DROP TABLE #Temp
 
Email Format
 

Wednesday, April 13, 2011

How to close/deactivate your account

http://ask-leo.com/how_do_i_close_my_windows_live_hotmail_account.html

Monday, April 11, 2011

Useful System level tables or Stored Procedure

Finding a  particular table is being used in the stored procedure

 --Searching for Empoloyee tableSELECT NameFROM sys.proceduresWHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Employee%'

--Searching for Empoloyee table and RateChangeDate column togetherSELECT NameFROM sys.proceduresWHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Employee%'AND OBJECT_DEFINITION(OBJECT_ID) LIKE '%RateChangeDate%

Finding the parameter list of a stored procedure or table

Sp_help '<sql server object name>'

(or)

Select 
A.NAME,B.Name,A.length,A.iscomputed 
From 
Syscolumns A,Systypes B
where id = 
(select id from sysobjects where name='<sql server object name>')
and A.XUSERTYPE=B.XUSERTYPE

Finding the stored procedure text or table column list

Sp_helptext <Object Name/TableName> 


Retrieving the column name and the datatype in sql server

SELECT column_name 'Column Name',
data_type 'Data Type',
CHARacter_maximum_length 'Maximum Length'
FROM information_schema.columns
WHERE table_name = 'liclicense'



Returning the number of rows in a table without COUNT

SET NOCOUNT ON 
DECLARE @recCount int

SELECT   @recCount = rows 
FROM   
sysindexes
WITH (NOLOCK) 
WHERE    id = OBJECT_ID('DSAApplication')  AND indid < 2

print CAST(@recCount AS VARCHAR(20))




Friday, April 8, 2011

Difference between stored procedure and function



Stored Procedure
Function
Cannot be used in the select statement

Can be used within a select to return a single value or to use a set of rowset from the function.
Can use transaction
Cannot use transaction
Cannot be used in join
Can be used in join
Have to use exec to execute the query
Need to use select to execute
Can return only integer value
Can return scalar value - table rows
Uses precompiled execution plan to 
execute the stored procedure 
Doesnot uses it.

Difference between char/varchar/nchar/nvarchar

Fixed-length (char) or variable-length (varchar) character data types.
char[(n)]

Fixed-length non-Unicode character data with length of n bytes. n must be a value from 1 through 8,000. Storage size is n bytes. The SQL-92 synonym for char is character.
varchar[(n)]

Variable-length non-Unicode character data with length of n bytes. n must be a value from 1 through 8,000. Storage size is the actual length in bytes of the data entered, not n bytes. The data entered can be 0 characters in length. The SQL-92 synonyms for varchar are char varying or character varying.

nchar and nvarchar

Character data types that are either fixed-length (nchar) or variable-length (nvarchar) Unicode data and use the UNICODE UCS-2 character set.

nchar(n)
Fixed-length Unicode character data of n characters. n must be a value from 1 through 4,000. Storage size is two times n bytes. The SQL-92 synonyms for nchar are national char and national character.

nvarchar(n)
Variable-length Unicode character data of n characters. n must be a value from 1 through 4,000. Storage size, in bytes, is two times the number of characters entered. The data entered can be 0 characters in length. The SQL-92 synonyms for nvarchar are national char varying and national character varying.


Deleting a duplicate row without primary key.

ID
FirstName
LastName
1
Bob
Smith
2
Dave
Jones
3
Karen
White
1
Bob
Smith
1
Bob
Smith




2005:
DELETE TOP (SELECT COUNT(*) -FROM dbo.Emptest WHERE ID '1'
FROM dbo.customer2 
WHERE ID '1'

Executing T-Sql batch multiple times.


2000:
CREATE TABLE dbo.TEST  
(ID INT IDENTITY (1,1), ROWID uniqueidentifier)
CREATE TABLE dbo.TEST2 
 (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
GO

DECLARE 
@counter INT 

SET 
@counter 

WHILE 
@counter 1000 

BEGIN       

INSERT INTO 
dbo.TEST (ROWIDVALUES (NEWID())
INSERT INTO dbo.TEST2 (ROWIDVALUES (NEWID()) SET @counter @counter 1
  

END


 2005:
Example 1:
CREATE TABLE dbo.TEST  
(ID INT IDENTITY (1,1), ROWID uniqueidentifier)
GO



INSERT INTO 
dbo.TEST (ROWIDVALUES (NEWID()) 

GO 1000
/*executes both insert statement 1000 times.*/



Example 2:
CREATE TABLE dbo.TEST  
(ID INT IDENTITY (1,1), ROWID uniqueidentifier

CREATE TABLE dbo.TEST2 
 (ID INT IDENTITY (1,1), ROWID uniqueidentifier
GO 


INSERT INTO 
dbo.TEST (ROWIDVALUES (NEWID()) 
INSERT INTO 
dbo.TEST2 (ROWIDVALUES (NEWID()) 


GO 1000
/*executes both insert statement 1000 times.*/

Difference between truncate and Delete

Difference between truncate and Delete
Truncate
Delete
Deletes all the records by deallocating the pages which reduces the resource overhead of logging the deletion and no. of lock acquired.

Only one record is entered in the transaction log that is the deallocation of page.
Deletes one row at a time which is entered in the transaction log as LSN(Log sequence no.).
Records removed by the truncate command cannot be restored.
since the transaction log is maintained it can be roll backed or restored.
You cannot specify the where clause in the truncate command.
We can delete specific records.
Truncate cannot be used delete the tables which contains the reference of foreign keys since it doesnot fire event triggers (delete / update trigger).

Truncate resets the identity back to the seed


When deleting a large data the table may hang on to the empty pages requiring manual release using DBCC SHRINKDATABASE (db_name).
DBCC SHRINKDATABASE (db_name)

When truncate cannot be used then the alternate using delete command is
DELETE from "table_name"
DBCC CHECKIDENT("table_name", RESEED, "reseed_value")
Truncate cannot be used in replication or log shipping since it may need transaction log files for maintanence