Website Uptime Monitor

Default Page

<markup code>
< %@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" 
AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:content>
<asp:content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<!--page title-->
<h2> Current website status!
<div style="min-height: 20px;">
<!-- iframe loading content -->
<iframe src="real_time_site_monitor.aspx" frameborder="0" style="width: 900px; 
height: 400px;"></iframe>
</asp:content>
</asp:content>

<code behind>
Imports System.Net
Imports System.Net.Sockets
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                            Handles Me.Load
        If Not Page.IsPostBack Then
            'page will be refereshed at a interval of 10 sec
            Response.AddHeader("Refresh", "10")
        End If
    End Sub
End Class

Site Monitor Page

<markup code>
<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="real_time_site_monitor.aspx.vb" 
Inherits="intranet_monitor" %>
<!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>Intranet Uptime Monitor
    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body style="background-color: #FFF;">
    <form id="form1" runat="server">
    <p><h1>My Website</h1></p>
    <div> 
        <asp:Label ID="LblVer" runat="server"></asp:Label>
        <asp:Label ID="LbltVer" runat="server"></asp:Label> 
     </div>
    </form>
</body>
</html>


<code behind>
Imports System.Net
Imports System.Net.Sockets
Imports System.Net.Mail

Partial Class intranet_monitor
 Inherits System.Web.UI.Page
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                           Handles Me.Load
  '-----------------'
  '     MY SERVER   '
  '-----------------'
  'define http request and url
  Dim httpReqIn As HttpWebRequest = _
      DirectCast(WebRequest.Create("http://www.mysite.com"), HttpWebRequest)
  httpReqIn.AllowAutoRedirect = False

  Try
    Dim httpResIn As HttpWebResponse = CType(httpReqIn.GetResponse(), HttpWebResponse)

    If httpResIn.StatusCode = HttpStatusCode.NotFound Then
    ' Code for moved resources goes here.
    Else
    ' the site is up
     LblInt.Text = "The site is up!<br />"
     LblInt.Attributes.Add("class", "site_status_up")
     LbltInt.Text = "Last load time: <b>" + DateTime.Now.ToString + ""
    End If

    httpResIn.Close()

  Catch ex As WebException
    If Not ex.Message.IndexOf("could not be resolved") > -1 Then
    'Could not resolve hostname.
    'This means the internet connection is down, DNS server doesn't have entry,
    'and/or the website does not exist.
      LblInt.Text = "Site is down!<br />"
      LblInt.Attributes.Add("class", "site_status_down")
      LbltInt.Text = "Last load time: <b>" + DateTime.Now.ToString + ""
    
    'send automated email notification
     Dim SmtpServer As New SmtpClient
     Dim mail As New MailMessage
     SmtpServer.Port = 25
     SmtpServer.Host = "smpt.mywebsite.com" ' - IP address (x.x.x.x)
     mail = New MailMessage()
     mail.From = New MailAddress("downtime@mysite.com")
     mail.To.Add("myemail@vmywebsite.com")
     mail.Subject = "Web server is down"
     mail.Body = "<div>The server appears to be down"
     mail.IsBodyHtml = True
     SmtpServer.Send(mail)
    Else
    End If

   End Try
  End Sub
End Class


Record Count Using Stored Procedure


Imports System.Data.SqlClient
Partial Class mb_app_promo
 Inherits System.Web.UI.Page
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                          Handles Me.Load
   G1.Text = "500"
   Dim connString As String
   connString = ConfigurationManager.ConnectionStrings("myConnString").ToString
   Dim conn As New SqlConnection(connString)
     Try
       conn.Open()
       Dim cmd As New SqlCommand("myStoredProcedure", conn)
       Dim count As Integer = CInt(cmd.ExecuteScalar())

       If count >= 500 Then
         Goal1.Text = "Congratulations - 500 goal!"
        Else
          Goal1.Text = "Your current score: "& count
        End If

      Catch ex As Exception
         Throw New Exception(ex.Message)
      Finally
         conn.Close()
      End Try
  End Sub
End Class

Resize image before upload

Markup Page

<div id="FileUpd1" runat="server" >
Default: <asp:FileUpload ID="ImgUpload1" runat="server" />
<asp:Button ID="Btn1" runat="server" Text="Submit " 
CssClass="btnSubmit" CausesValidation="true"/>



Imports System.IO Imports System.Web.UI Imports System.Data.Sql Imports System.Data.SqlClient Imports System.Drawing Imports System.Drawing.Imaging Protected Sub Btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn1.Click 'define variable Dim adPhotoName As String = "PhotoNum" 'img directory - physical path Dim saveDir As String = "\photo_repository\" Dim appPath As String = Request.PhysicalApplicationPath 'img extensiong check Dim ImgExt1 As String = LCase(System.IO.Path.GetExtension(ImgUpload1.FileName)) Dim ImgName1 = adPhotoName & "_1" & ImgExt1 If (ImgUpload1.HasFile) Then 'save with custom file name Dim savePath As String = Path.Combine(Server.MapPath("~/photo_repository"), ImgName1) 'save with default file name 'Dim savePath As String = appPath + saveDir + Server.HtmlEncode(ImgUpload1.FileName) 'allow only jpg and gif photo files If (ImgExt1.ToString = ".jpg") Or (ImgExt1.ToString = ".jpeg") Or (ImgExt1.ToString = ".gif") Then 'resize image before upload Dim image As New Bitmap(System.Drawing.Image.FromStream + (ImgUpload1.PostedFile.InputStream)) Dim width As Integer = 448 'image width Dim height As Integer = 336 'image height Using thumbnail As System.Drawing.Image = image.GetThumbnailImage + (width, height, New System.Drawing.Image.GetThumbnailImageAbort + (AddressOf ThumbnailCallback), IntPtr.Zero) Using memoryStream As New MemoryStream() '**default img name - use these two lines of code 'thumbnail.Save(Server.MapPath("~/Uploaded/") + 'Path.GetFileName(Me.ImgUpload1.FileName), ImageFormat.Png) '******** thumbnail.Save(savePath) End Using End Using 'ends here 'confirm image upload ImgUpConf1 = ImgName1.ToString FileUpd1.InnerHtml = + "<table><tr><td><img src='../photo_repository/" + ImgUpConf1 & "' height='35' width='50'>" + "<td style='vertical-align: top;'><strong>Image 1: " + ImgUpload1.FileName & "<br />" + "<span class='lblAdded'>Image 1 successfuly uploaded!" Else 'alert if img fiel ext <> jpg or gif BadImgFormat = ImgExt1.ToString ImgName1 = NoImage 'db photo record FileUpd1.InnerHtml = + "<table><tr><td><img src='../photo_repository/no_image.jpg' " + "height='35' width='50'/>" + "<td style='vertical-align: top;' ><span style='color: red;'>" + "ERROR: The default image format is " & BadImgFormat & "!<br />" + "Only .jpg and .gif file type allowed.<br /><a href='ad_photo.aspx?img=1'" + "class='colorbox2'>Try again." End If End Sub 'define function Public Function ThumbnailCallback() As Boolean Return False End Function End Class