Thursday 26 December 2013

Write Error into Log File in VB

Create a general Class:
================
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Globalization
Imports System.Net

Public Class clsErrHandler

    ''' Handles error by accepting the error message
    ''' Displays the page on which the error occured

    Public Sub WriteError(ex As Exception)
        'VARIABLE DECLARATION
        Dim strErrorIn As String, strFileName As String, strErrorMethodName As String, strErrorMessage As String, _
       strLineNo As String, strException As String, strDllInfo As String, strLocationInfo As String

        Dim strFileCheck As String
        Dim arrFileCheck As String()
        Try
            strException = ex.StackTrace.Trim().Remove(0, 3)

            If strException.Contains(":") AndAlso strException.Contains(".") AndAlso strException.Contains("\") Then
                strDllInfo = strException.Substring(0, strException.IndexOf("("c))
                strLocationInfo = strException.Substring(strException.IndexOf(")"c), (strException.LastIndexOf(":"c)) - strException.IndexOf(")"c)).Remove(0, 5)


                'Get the File Type & Name
                strFileCheck = strLocationInfo.Substring(strLocationInfo.LastIndexOf("\"c)).Remove(0, 1)
                arrFileCheck = strFileCheck.Split("."c)

                If arrFileCheck.Length > 2 Then
                    strErrorIn = "Page File"
                Else
                    strErrorIn = "Class File"
                End If
                strFileName = strFileCheck


                'Get the Error Method Name
                strErrorMethodName = strDllInfo.Substring(strDllInfo.LastIndexOf("."c)).Remove(0, 1)

                'Get the Error Meassage
                strErrorMessage = ex.Message

                'Get the Error Line No.
                strLineNo = strException.Substring(strException.LastIndexOf(":"c), (strException.Length - strException.LastIndexOf(":"c))).Remove(0, 6)
            Else
                strFileName = "-"
                strErrorMethodName = ex.ToString()
                strErrorIn = "Application Error"
                strLineNo = "-"
                strErrorMessage = ex.Message
            End If

            'Write the Error In ErroLog File
            WriteToErrorLog(strErrorIn, strFileName, strErrorMethodName, strErrorMessage, strLineNo)

        Catch exp As Exception
            WriteError(exp)
        End Try
    End Sub

    Public Sub WriteToErrorLog(strErrorIn As String, strFileName As String, strErrMethodName As String, strErrMessage As String, _
     strLineNo As String)
        'IP TRACKING START HERE
        Dim strHostName As String = ""
        Dim ipEntry As IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)
        Dim addr As IPAddress() = ipEntry.AddressList
        Dim path As String = "~/ErrorLog/" & DateTime.Today.ToString("dd-MM-yyyy") & ".txt"

        Try
            strHostName = System.Net.Dns.GetHostName()
            'IP TRACKING END HERE

            'LOG FILE CREATE IF NOT EXISTS
            If (Not File.Exists(System.Web.HttpContext.Current.Server.MapPath(path))) Then
                File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close()
            End If

            Using w As StreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path))
                w.WriteLine(Constants.vbCrLf & "Log Entry : ")

                Dim err As String = "Error Path: " & System.Web.HttpContext.Current.Request.Url.ToString()
                err = err & Environment.NewLine
                err = err & "Error Date Time: " & DateTime.Now.ToString(CultureInfo.InvariantCulture)
                err = err & Environment.NewLine
                err = err & "Error Type: " & strErrorIn
                err = err & Environment.NewLine
                err = err & "File Name : " & strFileName
                err = err & Environment.NewLine
                err = err & "Error Method: " & strErrMethodName
                err = err & Environment.NewLine
                err = err & "Error Line: " & strLineNo
                err = err & Environment.NewLine
                err = err & "Error Message: " & strErrMessage
                err = err & Environment.NewLine
                err = err & "Host Name : " & strHostName & ". IP Address : " & addr(addr.Length - 1).ToString()

                w.WriteLine(err)
                w.WriteLine("__________________________________________________")
                w.Flush()
                w.Close()
            End Using
        Catch ex As Exception
            WriteError(ex)
        End Try
       
    End Sub

End Class

Write in Catch
=========
Dim objErrHandler = New clsErrHandler()
            Try
                objErrHandler.WriteError(ex)
            Catch exp As Exception
                objErrHandler.WriteError(exp)           
            End Try

Wednesday 4 December 2013

List of SP or Function containing specific text

SELECT ROUTINE_NAME, ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES
    WHERE ROUTINE_DEFINITION LIKE '%searchtext%'
   -- AND ROUTINE_TYPE='PROCEDURE'

How do I find what stored procedures contain a table in SQL

SELECT DISTINCT so.name  
FROM syscomments sc  
INNER JOIN sysobjects so ON sc.id=so.id  
WHERE sc.TEXT LIKE '%tablename%' 

Tuesday 3 December 2013

SQL Server STR Function and SPACE Function


STR() Function
This function returns character data converted from numeric data.
It takes 3 arguments. First argument as float data,second argument
as integer value specifying the length of the string including decimal
that is to be retrieved and third argument as integer specifying the
number of places to the right of the decimal point.

e.g: SELECT STR(123.45, 6, 1)

SPACE() Function
This function returns a string of repeated spaces.

e.g:select 'Hello'+Space(2)+'World'

Character Count using jQuery

<%@ Page Language="C#" %>

<!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 id="Head1" runat="server">
    <title></title>
    <script src="jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var characterLimit = 140;
        $(document).ready(function () {
            $("#lblremaingCharacters").html(characterLimit);
            $("#meTextarea").bind("keyup", function () {
                var characterInserted = $(this).val().length;
                if (characterInserted > characterLimit) {
                    $(this).val($(this).val().substr(0, characterLimit));
                }
                var characterRemaining = characterLimit - characterInserted;
                $("#lblremaingCharacters").html(characterRemaining);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <label id="lblremaingCharacters" style="color:Red;font-weight:bold"></label><label> characters remaining.</label><br />
        <asp:TextBox ID="meTextarea" runat="server" TextMode="MultiLine"></asp:TextBox>
 
    </div>
    </form>
</body>
</html>