Wednesday, February 6, 2013

Disable Dates in Calendar Extender & Calendar Control

CalenderExtenders don't have OnDayRender Event, It is specially for Calendar Control. For CalenderExtender we can use some JS function:

CalenderExtenders:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

 <title></title>

   <script type="text/javascript">

     function checkDate(sender, args) {

       // 6 days        
       var ms = new Date().getTime() + 6 * 86400000; 

        var weekFar = new Date(ms);

          if (sender._selectedDate < weekFar) {           

              //getTime() gives milliseconds  
              //and 86400000 is the number of 
              //milliseconds in a day. 
             alert("You can't select a day 
                     earlier than a week!");

 
           // set the date back to a week from today
            var ms1 = new Date().getTime() 
                         + 7 * 86400000; 

            var weekFar1 = new Date(ms1);
            sender._selectedDate = weekFar1; 
            sender._textbox.set_Value(sender.
                 _selectedDate.format(sender._format))

          }

      }

    </script>
</head>

<body>

    <form id="form1" runat="server">

     <asp:ToolkitScriptManager 
        ID="ToolkitScriptManager1" runat="server">
                </asp:ToolkitScriptManager> 

  

         <asp:TextBox ID="txtDateNeeded
              runat="server" Width="5em">
              </asp:TextBox>   

          <asp:CalendarExtender   
             ID="CalendarExtender1
             OnClientDateSelectionChanged="checkDate"
             TargetControlID="txtDateNeeded"  
             runat="server"/>

</form></body></html>

Calendar Control:
<asp:Calendar ID="Calendar1" runat="server"
      Caption="" CaptionAlign="Top" CellPadding="2"
      CellSpacing="2" ShowTitle="true"
      OnDayRender="Calendar1_DayRender
      Font-Names="verdana"
      Font-Size="Small">
      <DayHeaderStyle BackColor="CadetBlue" 
      HorizontalAlign="center
      VerticalAlign="Middle" />
      <WeekendDayStyle BackColor="gray">
      </WeekendDayStyle>
      <SelectedDayStyle BackColor="BurlyWood"
      ForeColor="DarkBlue" Font-Names="Calibri"
      Font-Bold="true" />
</asp:Calendar>

'Code behind
'calendar disable certain dates
Protected Sub Calendar1_DayRender(sender As Object, e As DayRenderEventArgs)

 If e.Day.Date <= DateTime.Now.AddDays(6) Then
       'disable the date a week from today
       e.Cell.Enabled = False
       e.Day.IsSelectable = False
       e.Cell.ToolTip = "This date is not available"
   End If
End Sub

Friday, August 24, 2012

Get Email Addresses and Send Email SQL

Let say, you have a form to order books and 'Customer' table to save the information with 'Customer Name', 'Email', 'Shipped Date', 'Received' columns.

I want to write an auto email query in SQL and would like to run by SQL agent so that the customer would get an e-mail reminder if they forget to return the books in 3 weeks from the shipped date.
Create Procedure [dbo].[AutoEmail] AS 
BEGIN 
    SET NOCOUNT ON 
            
     DECLARE @EmailList varchar(MAX)
     
     SELECT  @EmailList = COALESCE(@EmailList + ';', '') + Email
        FROM    Customer
            WHERE   (Email IS NOT NULL)
                   AND
                   (received = 0)  'bit value
                   AND
                   (ShippedDate < DATEADD(day, -21, GETDATE()))
                   
       SELECT  @EmailList
            
                
     exec msdb.dbo.sp_send_dbmail
@profile_name = 'Your Profile Name',
        @recipients='abc@hotmail.com',       
        @blind_copy_recipients = @EmailList,
        @subject = 'Books return time reminder',        
        @execute_query_database='Database Name',
        @body = 'Hello, this is a reminder email, please send us back
                           the books which you received 3 weeks ago. If you 
                           have already sent them please disregard this 
                           message.'
End
   
 

Wednesday, July 11, 2012

SQL Select result displays Horizontally Separate by comma

Let say you have a table structure and data as follow:


Id     Grade
1       3
1       9


then you would need a SQL select output as


Id   Grade
1     3, 9


then do this:

DECLARE @grade_list VARCHAR(MAX

SELECT @ grade_list  = CASE WHEN @ grade_list  IS NULL THEN CONVERT(VARCHAR,Grade) 
ELSE @ grade_list  + ', ' + CONVERT(VARCHAR,Grade) END
FROM Table1where Id = 1
SELECT @grade_list as Grade


or you could do like this:

SELECT Id,
 SUBSTRING
 (
  SELECT  (', ' + Grade)
  FROM table1 t2 
  WHERE t1.Id = t2.Id
  ORDER BY t1.Id, t2.Id
  FOR XML PATH('')), 3, 1000)
FROM table1 t1
GROUP BY Id


Happy Coding!!

Wednesday, March 28, 2012

How to force a pdf file to download ASP.NET

If you try to open pdf files on a browser, those are automatically open but if you need to force the browser to directly download the pdf instead you have to add file header and give the full path. see the example:


<span style="color:red">Click <a href="download.aspx"
target="_blank"><u> here </u></a> to download resume.</span>



then on the page load method of the download.aspx page, write this:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=documents/resume.pdf")
Response.ContentType = "application/pdf"      Response.WriteFile(Server.MapPath("~/documents/resume.pdf"))
Response.[End]()
End Sub


Happy Coding!!