Monday, November 28, 2011

Sending SMTP Mail using a Stored Procedure

 
 
IF EXISTS (SELECT NAME FROM sysobjects 
    WHERE name = 'sp_SMTPMail' AND type = 'P')
    DROP PROCEDURE sp_SMTPMail
GO
 
Create Procedure sp_SMTPMail
  @SenderName varchar(100),
  @SenderAddress varchar(100),
  @RecipientName varchar(100),
  @RecipientAddress varchar(100),
  @Subject varchar(200),
  @Body varchar(8000),
  @MailServer varchar(100) = 'localhost' 
  /* This Should be UR MailServer*/

AS 
BEGIN
 
  SET nocount on
 
  declare @oMail int --Object reference
  declare @resultcode int
 
  EXEC @resultcode = sp_OACreate 'SMTPsvg.Mailer', @oMail OUT
 
  if @resultcode = 0
    BEGIN
     EXEC @resultcode = sp_OASetProperty @oMail, 'RemoteHost', @mailserver
     EXEC @resultcode = sp_OASetProperty @oMail, 'FromName', @SenderName
     EXEC @resultcode = sp_OASetProperty @oMail, 'FromAddress',
                        @SenderAddress
     EXEC @resultcode = sp_OAMethod @oMail, 'AddRecipient', NULL, 
                        @RecipientName, @RecipientAddress
     EXEC @resultcode = sp_OASetProperty @oMail, 'Subject', @Subject
     EXEC @resultcode = sp_OASetProperty @oMail, 'BodyText', @Body 
 
     EXEC @resultcode = sp_OAMethod @oMail, 'SendMail', NULL

     EXEC sp_OADestroy @oMail
    END 
 
  SET nocount off
END
GO

SQL Server blocked access to procedure 'sys.sp_OACreate'



           Error: SQL Server blocked access to procedure 'sys.sp_OACreate' of component              Ole Automation Procedures' because this component is turned off as part of the
           security configuration for this server. 

           A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', see "Surface Area Configuration" in SQL Server Books Online. 
I got this error today. 
Way to solve it is to go into 

SQL 2005 surface area configuration -> For Features -> OLE Automation -> Enable OLE automation. 

OR u can use the following commands.

sp_configure 'show advanced options', 1
GO 

RECONFIGURE
GO 

sp_configure 'Ole Automation Procedures', 1
GO 

RECONFIGURE
GO

Friday, September 9, 2011

SQL Server 2005 Locking Hints

ROWLOCK : Use row-level locks when reading or modifying data.
PAGLOCK : Use page-level locks when reading or modifying data.
TABLOCK : Use a table lock when reading or modifying data.
DBLOCK : Use a database lock when reading or modifying data.
UPDLOCKUPDLOCK reads data without blocking other readers, and update it later with the assurance that the data has not changed since last read.
XLOCKUse exclusive locks instead of shared locks while reading a table, and use hold locks until the end of the statement or transaction.
HOLDLOCKUse a hold lock to hold a lock until completion of the transaction, instead of releasing the lock as soon as the required table, row, or data page is no longer required.
NOLOCKThis does not lock any object. This is the default for SELECT operations. It does not apply to INSERT, UPDATE, and DELETE statements.

Wednesday, January 19, 2011

RegularExpressionValidator working with FileUpload

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="FileUpload1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="^.*\.(doc|DOC|pdf|PDF)$"></asp:RegularExpressionValidator>
<asp:Button ID="Button5" runat="server" Text="Button" />