try another color scheme:


Go Back   TECH6.0 > Get Techie > Programming > ASP/ASP.NET


Some useful functions...

This is a discussion on Some useful functions... within the ASP/ASP.NET section, part of the Programming category; Hai mates . Here i am starting a new thread for useful functions which can be used in classic asp/asp.net..If ...

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 09-03-2008, 08:44 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Some useful functions...

Hai mates .

Here i am starting a new thread for useful functions which can be used in classic asp/asp.net..If you have any scripts that you think may be helpful to others please post here ....

Function for format date(asp):

Code:
Function FormatMediumDate(DateValue)
   Dim strYYYY
   Dim strMM
   Dim strDD
  strYYYY = CStr(DatePart("yyyy", DateValue))
  strMM = CStr(DatePart("m", DateValue))
  If Len(strMM) = 1 Then strMM = "0" & strMM
  strDD = CStr(DatePart("d", DateValue))
  If Len(strDD) = 1 Then strDD = "0" & strDD
  FormatMediumDate = strYYYY & "/" & strMM & "/" & strDD
End Function
To call this function :FormatMediumDate(12/09/2002)


Function for generate random string(asp)

Code:
function GenerateRandomString(intLength)
	Randomize
	strRndString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	For i = 0 to intLength
		intPos = Int(36 * Rnd + 1)
		strRandom = strRandom & mid(strRndString,intPos,1)
		GenerateRandomString = strRandom
	Next
end function
To call this function : GenerateRandomString(8)



Function for generate random number (asp)

Code:
Function RandomNumber(intHighestNumber)
	Randomize
	RandomNumber = Int(Rnd * intHighestNumber) + 1
End Function
To call this function :RandomNumber(9)


Spec_tray
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
  #2  
Old 09-03-2008, 10:48 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default 301 Redirect

301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It's not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it's the safest option. The code "301" is interpreted as "moved permanently".

ASP Redirect:-

Code:
 <%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.your-new-url.com/"
%>
ASP.NET Redirect

Code:
 <script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.your-new-url.com");
}
</script>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #3  
Old 10-03-2008, 12:25 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Default great ..keep it going..


For replacing single quotes and reversing the replacement (it can be used to remove single quotes from sql insert statement)


Code:
function rep_sq(stg)
    if isnull(stg)=false then 
        rep_sq=replace(stg,"'","&rsquo;")
    else
        rep_sq=""
    end if
end function


function rep_sq_rev(stg)
    if isnull(stg)=false then 
        rep_sq_rev=replace(stg,"&rsquo;","'")
    else
        rep_sq_rev=""
    end if
end function
Function to strip HTML codes
Return value - the submitted string after cleaning up


Code:
function stripHTML(str)
    stripHTML =  Replace(Replace(str, "<", "&lt;", 1, -1, 1), "<", "&lt;", 1, -1, 1)
end function
Spec_tray
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
  #4  
Old 11-03-2008, 02:23 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Default Function to strip non alphanumeric characters..

Return value - the string after cleaning up

Code:
Private Function characterStrip(strTextInput)
'Dimension variable Dim intLoopCounter 'Holds the loop counter 'Loop through the ASCII characters For intLoopCounter = 0 to 37 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the ASCII characters For intLoopCounter = 39 to 44 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the ASCII characters numeric characters to lower-case characters For intLoopCounter = 65 to 94 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the extended ASCII characters For intLoopCounter = 123 to 125 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Loop through the extended ASCII characters For intLoopCounter = 127 to 255 strTextInput = Replace(strTextInput, CHR(intLoopCounter), "", 1, -1, 0) Next 'Strip individul ASCII characters left out from above left over strTextInput = Replace(strTextInput, CHR(59), "", 1, -1, 0) strTextInput = Replace(strTextInput, CHR(60), "", 1, -1, 0) strTextInput = Replace(strTextInput, CHR(62), "", 1, -1, 0) strTextInput = Replace(strTextInput, CHR(96), "", 1, -1, 0) 'Return the string characterStrip = strTextInput
End Function
Spec_tray
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
  #5  
Old 11-03-2008, 02:40 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
ASP Auto responder(Using CDONT)..

Simple ASP script invoking CDONTS mail component (asp)

Code:
 

                Dim objCDONTS                 ' Email object
    Dim strFromName               ' From persons' real name
    Dim strFromEmail, strToEmail  ' Email addresses
    Dim strSubject, strBody       ' Message

    strSubject    = "Dear " 
    strFromName   = "Webadministrator"
    strFromEmail  = "Fromemail@gmail.com"
    strToEmail    = "Toemail@gmail.com"
    strBody       = "Thank you for... "& vbcrlf & vbcrlf &"Body of email goes here..bla..bla..bla"& vbcrlf & vbcrlf &"With regards"
    Set objCDONTS = Server.CreateObject("CDONTS.NewMail")
    objCDONTS.From    = strFromName & " <" & strFromEmail & ">"
    objCDONTS.To      = strToEmail
    objCDONTS.Subject = strSubject
    objCDONTS.Body    = "--" & vbcrlf & vbcrlf & strbody & vbcrlf & vbcrlf & vbcrlf & "--" 
    objCDONTS.Send
    Set objCDONTS = Nothing
Spec_tray
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator

Last edited by Spec_tray; 17-03-2008 at 01:31 PM.
Reply With Quote
  #6  
Old 11-03-2008, 02:58 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default

Simple autoresponder using CDONT (asp)



Spec_tray
As Said by Spec_tray View Post
Hey Spectray,

how does the above asp code acts as a auto responder to an email ??

coz i find it to be a simple ASP script invoking CDONTS mail component used to send emails.

As far as I know, autoresponder means a system implemented to send AUTOMATIC mail in reply to an email. I don't think the above script can send an automatic reply as strToEmail variable has a custom email add. as a value.

please correct me if i'm wrong !!

thankss
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #7  
Old 11-03-2008, 03:22 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Sending mail through Persits ASPEmail

This ASP script will send mail using Persits ASPEmail component.

Requirements:- ASPEmail to be installed on server
Note:- You can check what components are installed on your server. Simply refer to this thread here.


Code:
<%
' Instantiate the COM Object
Set Mail = Server.CreateObject("Persits.MailSender")

' This is the Your IP Address
Mail.Host = "mail.yoursite.com"

Mail.Username = "you@yoursite.com"
Mail.Password = "your_password"

' This is the Hosts Station Internal SMTP Port
Mail.Port = "25"

' This is the FROM email ADDRESS
Mail.From = "you@yoursite.com"

' This is the FROM email NAME
Mail.FromName = "Yoursite.com"

' This is who the email will be sent to
Mail.AddAddress "mailme@yoursite.com"

' This is the SUBJECT of the email
Mail.Subject = "Testing Email"

' This is the BODY of the email
Mail.Body = "Thanks for registering with Yoursite.com................"

'Mail.AddAttachment "c:\dir\receipt.doc

' This is Error Checking and the "Mail.Send"
' is the actual action of sending the email

On Error Resume Next

Mail.Send

If Err <> 0 Then 
    Response.Write "An error occurred: " & Err.Description 
End If

%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #8  
Old 11-03-2008, 07:48 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Default Now some string functions.

Find string position using InStr

Code:
DIM strpos
Dim strexmp
strexmp = "Few cool vb scripts for you."
strpos = InStr(1, strexmp, "scripts", 1)
Response.Write strpos
Split the string to an array

Code:
Dim Array_word
Dim N_Str 

N_Str = "tech6 forums"
Array_word = Split(N_Str, " ")
'For break a string passing certain length

Code:
Function BreakString(strInput, intBreakPoint)
intStart = 1 intEnd = intBreakPoint intStringLength = Len(strInput) strOutput = "" If intStringLength > intBreakPoint Then Do While intStart < intStringLength if(intStart + intBreakPoint > intStringLength) Then strOutput = strOutput & Mid(strInput, intStart) else strOutput = strOutput & Mid(strInput, intStart, intEnd) & "<BR />" End If intStart = intStart + intBreakPoint Loop End If BreakString = strOutput
End Function
Spec_tray
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
  #9  
Old 17-03-2008, 01:30 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Default Thanks for a good catch..

Hey Spectray,

how does the above asp code acts as a auto responder to an email ??

coz i find it to be a simple ASP script invoking CDONTS mail component used to send emails.

As far as I know, autoresponder means a system implemented to send AUTOMATIC mail in reply to an email. I don't think the above script can send an automatic reply as strToEmail variable has a custom email add. as a value.

please correct me if i'm wrong !!

thankss
As Said by admin View Post
yes you are correct ..that was my mistake ...I wrote incorrect title actually it was simple ASP script invoking CDONTS mail component used to send emails.

regards
spec_tray
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
  #10  
Old 17-03-2008, 01:38 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Default Word and character count..

Code:
Function GetWordCount(strInput)
   Dim str_word
   str_word = Replace(strInput, vbTab, " ")
   str_word = Replace(str_word, vbCr, " ")
   str_word = Replace(str_word, vbLf, " ")
   str_word = Trim(str_word)
   Do While InStr(1, str_word, "  ", 1) <> 0
    str_word = Replace(str_word, "  ", " ")
   Loop
   GetWordCount = UBound(Split(str_word, " ", -1, 1)) + 1
End Function 


Function GetCharCount(strInput)
   GetCharCount = Len(strInput)
End Function
Happy coding

Spec _tray
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
  #11  
Old 11-04-2008, 05:58 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Dynamically Display copyright information based on Server Date

If you want to display your copyright information, instead of trying to remember to change your HTML, display it as a dynamic include file.

This is in class ASP. The following will display the copyright symbol Company Name, the year you first copyrighted through to the current year. So now, when the server year updates, so does the copyright script.

Code:
                    © Copyright Your Company Name 1994 -  
<%  
with response  
curr_year = Year(now)  
.write(curr_year)  
end with  
%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #12  
Old 11-04-2008, 06:00 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Building a numerical dropdown box dynamically

A simple routine that allows you to quickly set up a numerical based dropdown box via a quick call to a sub routine.

Code:
<%  
Sub BuildDropdownNumericalList(ddName, ddStart, ddEnd, ddIncrement)  
Dim output, i  
For i = ddStart To ddEnd Step ddIncrement  
output = output & "<option value=""" & i & """>" & i & "</option>"  
Next  
output = "<select name=""" & ddName & """>" & output & "</select>"  
Response.Write output  
End Sub  
%>  
  
<%  
'DISPLAY THE DROPDOWN BOX  
Call BuildDropdownNumericalList("testdd", 1975, 2008, 1)   
%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #13  
Old 11-04-2008, 06:03 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Display Page Loading Time

Ever wondered how those "This page was processed in 3 seconds" messages were generated? Here's the code ...

Code:
' At the top of the page put...

<%
Dim strStartTime
Dim strEndTime

strStartTime = Timer 'Starts timer
%>


' and then this at the bottom (same page)...

<%
' Start our End timer
strEndTime = Timer

Response.Write ("Page Processed in: ")

Response.Write FormatNumber(strEndTime - strStartTime, 4)
Response.Write (" Seconds.")
%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #14  
Old 11-04-2008, 06:08 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Display First 20 Characters Followed By Dots

Sometimes, when you are returning search items you may only want to show a part of the search result followed by a series of dots to indicate that there is more to see if the visitor follows a link.

The Snippet below limits the display to the first 20 Characters, but you can change the 20 to any value you like...

Code:
<%
Dim CutShort
CutShort = rsYourRecordset.Fields.Item("YourField").Value
Response.Write LEFT (CutShort, 20) & "........"
%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #15  
Old 11-04-2008, 06:11 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Show Default Picture If Dynamic Picture Field Is Empty

This lets you display a default picture (staticPic.gif) if there is no picture held in the database field that you call your dynamic images from.

"PicShow" is your variable. If you use this routine more than once on a page make sure that you give all the variables different names or you will get a "Variable redefined" error.

Code:
<%Dim PicShow
PicShow = rsShowHide.Fields.Item("shMainPix").Value
IF PicShow <>"" THEN%>
<img src="<%=rsShowHide.Fields.Item("shMainPix").Value%>">
<%ELSE%>
<img src="StaticPic.gif">
<%End If%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #16  
Old 11-04-2008, 06:13 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Samsung Wave
Posts: 1,116
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Show Message If Data Field Is Empty

This bit of code displays the message "THIS DATA FIELD IS EMPTY" if the datafield ("YourDataField") is empty.

Code:
<%
Dim strShowHide
strShowHide = rsYourRecordset.Fields.Item("YourDataField").Value
IF strShowHide <>"" THEN%>
THIS DATA FIELD IS EMPTY
<%END IF%>
__________________
Always TAG and SHARE your threads
Submit your site to TECH6 Directory
TECH6 on Facebook - Like Us

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter


Reply With Quote
  #17  
Old 12-04-2008, 01:40 PM
Spec_tray's Avatar

Moderator
 
Join Date: Feb 2008
Posts: 147
Spec_tray will become famous soon enoughSpec_tray will become famous soon enough
Default

Hai

Good job ...keep it going...
__________________
" There are no failures - just experiences and your reactions to them.

New members Please read before u post
General Tech6 Rules !!!

CSS Validator | Markup Validator | RSS Validator
Reply With Quote
  #18  
Old 25-03-2010, 05:09 PM
No Avatar

Learner
 
Join Date: Mar 2010
Posts: 9
william is on a distinguished road
Default

Good Job Nice Post....Keep Going...

Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes




All times are GMT +5.5. The time now is 08:15 PM.

Contact Us - Tech6.com - Link to Us - Advertise - Submit Site - Privacy Statement - TOS - Top