Wednesday, October 24, 2012

How to Log Error to File in VBScript

 I am an OO developer, but sometime i need to lend a hand to help out the development in VBScript or VB.

Though VBScript is an aging language for me. I still need to pick it up and use it in the
development.

the Error Logger will give you great help in the development and production since the user will come up to you and say the application is not working or not properly working. you will first go to the error log folder and check if anything had been logged to the file.

the following a very simple one that i created in VB to allow user log the error when it is
being raised in the function.


// the constant for read write, append properties for file Open
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

//function to handle logging the error to file. the parameter FunctionName is the source
// causing the issue. the second paramenter Error is like Exception in C# stored all the information
//related to the error.

function LogErrorToFile(FunctionName,Error)
   Dim fso, tf,logFilePath
   logFilePath="C:\ErrorLogFolder\MyErrorFileName"& FormattedCurrentDate&".Log"
   Set fso = CreateObject("Scripting.FileSystemObject")
    if fso.FileExists(logFilePath) Then
        Set tf = fso.OpenTextFile(logFilePath, ForAppending, TRUE)
   else
        Set tf = fso.CreateTextFile(logFilePath, True)
   end if
   tf.WriteLine("************************************************" & Now() & "********************************************************")
   tf.WriteLine("Function: " & FunctionName )
   tf.WriteLine("Error Numnber: " & CStr(Err.Number))
   tf.WriteLine("Source: " & Err.Source)
   tf.WriteLine("Detail Error Message:" & Err.Description)
   tf.WriteLine("****************************************************************************************************************************") 
   tf.WriteBlankLines(1)
   tf.Close
   Set fso =nothing
End function

//helper function to format to the current date to
//yyyy_MM_dd
Function FormattedCurrentDate
  currentDate = CDate(Date)
  currentDay = DatePart("d", currentDate )
  currentMonth = DatePart("m", currentDate )
  currentYear = DatePart("yyyy", currentDate )
  If currentDay < 10 Then
    currentDay = "0" & currentDay
  End If
  If currentMonth < 10 Then
    currentMonth = "0" & currentMonth
  End If
  FormattedCurrentDate= currentYear & "_" & currentMonth onth & "_" & currentDay
End Function

the example to see you how to use the Error Logger inside the function


function MyFunction
          on Error Resume Next
          // your logic here
          if Err.Number <> 0 then 
           LogErrorToFile "MyFunction",Err
       end if

end function

Wednesday, October 17, 2012

How to use javascript to trigger the server side event

put the following javascript code in the Javascript file

 $(function () {
                 var dates = $("#txtDateFrom, #txtDateTo").datepicker({
                     defaultDate: "+1w",
                     changeMonth: true,
                     changeYear: true,
                     numberOfMonths: 1,
                     onSelect: function (selectedDate) {
                         var option = this.id == "txtDateFrom" ? "minDate" : "maxDate",
     instance = $(this).data("datepicker"),
     date = $.datepicker.parseDate(
      instance.settings.dateFormat ||
      $.datepicker._defaults.dateFormat,
      selectedDate, instance.settings);
                         dates.not(this).datepicker("option", option, date);
 
                        
                         if (this.id == "txtDateFrom") {
                             $("#txtDateFrom").change();
                         }
                       
                         else if (this.id == "txtDateTo") {
                             $("#txtDateTo").change();
                         }
                     }
                 });
           });

use the html element change event to trigger the server side. we fill the date from or date to textbox,
the textbox change event will be fired in the client.we must force the change event to fire since we do not use keyboard to input the date, instead we selected the date from the slide out calendar. as a result the textbox is unable to capture the content change.

$("#txtDateFrom").change();

in aspx page

<asp:textbox id="txtDateFrom" runat="server" AutoPostBack="true" 
OnTextChanged="txtDateFrom_onTextChanged"></asp:textbox>
 
in the code behind file we need to implement the evnet
 
 protected void txtDateFrom_onTextChanged(object sender, EventArgs e)
 {
         //your logic to implement here
 }