Multiple file upload – VB.NET

Code behind only

Imports System.IO
Imports System.Web.UI
Imports System.Data.Sql
Imports System.Data.SqlClient

'define public classes/share them on page
Public ImgUpConf1 As String                   'displays img after success. upload
Public ImgUpConf2 As String                   ' "
Public BadImgFormat As String                 'passing banned file ext msg to page
Public NoImage As String = "no_image.jpg"     'default image no image jpg
Public NoImage2 As String = "no_image_2.jpg"  'no image jpg for img2, 3 and 4

'img directory - physical path
Dim saveDir As String = "\photos\"
Dim appPath As String = Request.PhysicalApplicationPath

If uID = "" And vaID = "" Then

MsgLbl.Text = "Your session expired. Please login again." Else 'img extensiong check Dim ImgExt1 As String = System.IO.Path.GetExtension(ImgUpload1.FileName) Dim ImgName1 = "photo1" & ImgExt1 Dim ImgExt2 As String = System.IO.Path.GetExtension(ImgUpload2.FileName) Dim ImgName2 = "photo2 & ImgExt2 'uploading first image If (ImgUpload1.HasFile) Then 'default file name 'Dim savePath As String = appPath + saveDir + Server.HtmlEncode(ImgUpload1.FileName) 'custom file name Dim savePath As String = Path.Combine(Server.MapPath("~/photos"), ImgName1) 'checking img type = only jpg and gif allowed If (ImgExt1.ToString = ".jpg") Or (ImgExt1.ToString = ".jpeg") + Or (ImgExt1.ToString = ".gif") Then ImgUpload1.SaveAs(savePath) ImgUpConf1 = ImgName1.ToString 'img photo and conf msg after sucess. upload FileUpd1.InnerHtml = "<table>" + <tr><td><img src='../photos/" & ImgUpConf1 & "' height='35' width='50'/>" + "<td style='vertical-align: top;'><strong>Image 1:" + " " & ImgUpload1.FileName & "<br />" + "<span class='lblAdded'>Image 1 successfuly uploaded!" Else 'if img <> jpg or <> gif msg BadImgFormat = ImgExt1.ToString ImgName1 = NoImage 'db photo record FileUpd1.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' ><span style='color: red;'>" + "ERROR: The default image format is " & BadImgFormat & "!
" + "Only .jpg and .gif file type allowed.<br />" + "<a href='photo_up2.aspx?img=1' class='colorbox2'>Try again." + "" End If 'if img is not selected or not required Else ImgName1 = NoImage 'db photo record FileUpd1.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' >The default image was not "+ "selected and uploaded.<br />" + "Default image photo will apply!" End If 'uploading second image If (ImgUpload2.HasFile) Then 'default file name 'Dim savePath As String = appPath + saveDir + Server.HtmlEncode(ImgUpload2.FileName) 'custom file name Dim savePath As String = Path.Combine(Server.MapPath("~/photos"), ImgName2) 'checking img type = only jpg and gif allowed If (ImgExt2.ToString = ".jpg") Or (ImgExt2.ToString = ".jpeg") + Or (ImgExt2.ToString = ".gif") Then ImgUpload2.SaveAs(savePath) ImgUpConf2 = ImgName2.ToString 'img photo and conf msg after sucess. upload FileUpd2.InnerHtml = "<table>" + <tr><td><img src='../photos/" & ImgUpConf1 & "' height='35' width='50'/>" + "<td style='vertical-align: top;'><strong>Image 2:" + " " & ImgUpload2.FileName & "<br />" + "<span class='lblAdded'>Image 2 successfuly uploaded!" + "" Else 'if img <> jpg or <> gif msg BadImgFormat = ImgExt1.ToString ImgName2 = NoImage 'db photo record FileUpd2.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' ><span style='color: red;'>" + "ERROR: The default image format is " & BadImgFormat & "!
" + "Only .jpg and .gif file type allowed.<br />" + "<a href='photo_up2.aspx?img=2' class='colorbox2'>Try again." + "" End If 'if img is not selected or not required Else ImgName2 = NoImage 'db photo record FileUpd2.InnerHtml = "<table>" + "<tr><td><img src='../photos/no_image.jpg' height='35' width='50'/>" + "<td style='vertical-align: top;' >The default image was not " + "selected and uploaded."<br />" + "Default image photo will apply!" End If

Add / Substract Date From Current Date

Add 14 days to the current date
Current date:    
Date in 2 weeks: 

Markup Page

Current date:
<asp:TextBox ID="TodayDt" runat="server" >
Date in 2 weeks:
<asp:TextBox ID="DaysAhead" runat="server" >


Code Behind

TodayDt.Text = Date.Now.ToShortDateString
DaysAhead.Text = Date.Now.AddDays(-Date.Now.DayOfWeek).AddDays(14).ToShortDateString

Substract 14 days from current date
Current date:     
Date 2 weeks ago: 

Markup Page

Current date:
<asp:TextBox ID="TodayDt" runat="server" >
Date 2 weeks ago:
<asp:TextBox ID="PastDate" runat="server" >


Code Behind

TodayDt.Text = Date.Now.ToShortDateString
PastDate.Text = Date.Now.AddDays(-Date.Now.DayOfWeek).AddDays(-14).ToShortDateString

Count number of characters in textbox

Markup Page

<strong>Required Info:</strong> 
<asp:Label ID="TotCharReqInfo" runat="server" ></asp:Label>  

<strong>Body Length:</strong> 
<span id="BodyLength">0

<strong>Total Length:</strong> <span id="TotLength"> 
<asp:Label ID="TotLengthLbl" runat="server" ></asp:Label>

<strong>Left:</strong> <span id="CharLeft">
<asp:Label ID="ChLeft" runat="server" ></asp:Label>


<asp:TextBox id="txtInputOnln" runat="server" 
    TextMode="MultiLine" Width="300px" Height="90px" 
    MaxLength="350" onKeyUp="Count(this,350)" 
    onChange="Count(this,350)"/>


<script type="text/javascript"> var ChLeftVar = "<%=CharLeftInt %>"; var ChLeftOut var ChLefVar2 function count(clientId) { var txtInputOnln = document.getElementById(clientId); var CharLeft = document.getElementById('CharLeft'); var BodyLength = document.getElementById('BodyLength'); var TotLength = document.getElementById('TotLength'); BodyLength.innerHTML = txtInputOnln.value.length; ChLeftOut = 350 - ChLeftVar ChLeftVar2 = (350 - ChLeftOut) + txtInputOnln.value.length; TotLength.innerHTML = ChLeftVar2; CharLeft.innerHTML = ChLeftOut - txtInputOnln.value.length; } //limit user input (350 this case) function Count(text, long) { var maxlength = new Number(ChLeftOut); // Change number to your max length. if (text.value.length > maxlength) { text.value = text.value.substring(0, maxlength); // alert(" Only " + long + " chars");// alert(" Only 350 characters "); } } </script> Code Behind Protected CharLeftStr As String Protected CharLeftInt As Integer Dim count As Integer = 0 Dim ReqInfoTot As String = TxtBx1.Text + TxtBx2.Text + TxtBx3 For Each c As Char In ReqInfoTot If Char.IsLetter(c) Or Char.IsNumber(c) Then count += 1 End If Next TotCharReqInfo.Text = count.ToString() - 2 TotLengthLbl.Text = count.ToString() - 2 '(2) is number of "+" inside the ReqInfoTot string txtInputOnln.Attributes("onkeydown") = = String.Format("count('{0}')", txtInputOnln.ClientID) ChLeft.Text = 350 - TotCharReqInfo.Text.ToString() CharLeftStr = TotCharReqInfo.Text.ToString() CharLeftInt = Convert.ToInt32(CharLeftStr)