Archive for March, 2008

How do I change the error message that the Active Server pages 2.0 sends to the browser?

Monday, March 24th, 2008

You can set the ASP error message anywhere that you have an application defined. If you haven’t already created the application then you will need to.

1. Expand the tree until you can see your application.
2. Select the application and right click.
3. Chosse Properties from the drop down list.
4. Select the Virtual Directory tab if you are on a virtual directory and the Home Directory tab if you are on a web site.
5. Click onthe Configuration button.
6. Click on ASP Debugging tab.
7. Click on Send detailed error message to client.
8. Change the Error Message to suit your needs
9. Click on OK to exit the Application configuration dialog.
10. Click on OK to exit the properties dialog.

Posted by Mahesh ( Tryangled )

How to Dividing a long string to pages using ASP.NET?

Monday, March 24th, 2008

function pageString(str,Nrow)

‘the string to act upon and number of rows in page

— this function cuts a string to “pages”=divs

res=”<div id=’page0′ style=’display:inline’>”‘result string
rowsRA=split(str,”<br>”)
pgI=0

‘page counter- counts the number of pages the string was divided to
i=0
for each row in rowsRA
‘alert (i)

if (i mod Nrow=0) and (not i< Nrow) then
res=res&”<p>”
if (pgI-1)>=0 then
res=res&”<input type=’button’ onclick=”&chr(34)&”page”&pgI&”.style.display=’none’;page”&pgI-1&”.style.display=

‘inline’”&chr(34)&”value=’prev.’> ”
end if
res=res&”<input type=’button’ onclick=”&chr(34)&”page”&pgI&”.style.display=’none’;page”&pgI+1&”.style.display=

‘inline’”&chr(34)&”value=’next’> ”
pgI=pgI+1
res=res&”</div>”&chr(13)&”<div id=’page”&pgI&”‘ style=’display:none’ >”&chr(13)
end if
res=res&rowsRA(i)&”<br>”&chr(13)
i=i+1
next
res=res&”<input type=’button’ onclick=”&chr(34)&”page”&pgI&”.style.display=’none’;page”&pgI-1&”.style.display=

‘inline’”&chr(34)&”value=’next’> ”
res=res&chr(13)&”</div>”&chr(13)
pageString=res
end function

Posted by Mahesh ( Tryangled )

Incrementing an Alpha-Numeric Number using ASP script?

Monday, March 24th, 2008

An alpha-numeric number is a number that contains both letters and numbers.
(Example: B2XC41YD)

‘Open the function and pass in the old number
function GetNextOrderNumber(Old_Number)
maxOrderID = Old_Number
Dim alphaNum
Dim alphaNumList
alphaNumList =”0,1,2,3,4,5,6,7,8,9,B,C,D,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y,Z”
‘Note that the alphaNumList does not contain the vowels.
REM Split the alphanumList into an array
alphaNum = split(alphaNumList,”,”)
‘Parse the current order number into an array.
Dim F(4)
F(0) = Right(Left(maxOrderID,1),1)
F(1) = Right(Left(maxOrderID,2),1)
F(2) = Right(Left(maxOrderID,3),1)
F(3) = Right(Left(maxOrderID,4),1)
Loop thru each position starting with the last digit.
For Y = 3 to 0 Step -1
If the last position is Z then roll it over to 0
If F(Y) = “Z” then
F(Y) = “0″
else
For X = 0 to 30
If F(Y) = alphaNum(X) then
NewF = alphaNum(X+1)
End If
Next
F(Y) = New F
exit for
End if
Next
Rebuild the new order number from the array
newOrderID = F(0) & F(1) & F(2) & F(3)
Return the new order number
GetNextOrderNumber = newOrderID
end function

Posted by Mahesh ( Tryangled )

How to get the confirmation of Yes/No from a javascript pop-up and display in ASP.NET Page?

Monday, March 24th, 2008

Create a webpage trymain.aspx  Drag and drop a hidden control and <asp:button> control on the web form.Step 1. trymain.aspx.vbWrite the following code on page load event< …The sample code is on the most frequently asked query on “How to get the confirmation of Yes/No from a javascript pop up and display the value on the page using ASP.NET”

* Create a webpage trymain.aspx
* Drag and drop a hidden control and <asp:button> control on the web   form.
Step 1. trymain.aspx.vb
Write the following code on page load event
Button1.Attributes.Add(”onclick”, “getMessage()”)

Step 2.In trymain.aspx
Add the client side-script block
<SCRIPT language=”javascript”>
function getMessage()
{
var ans;
ans=window.confirm(’Is it your confirmation…..?’);
//alert (ans);
if (ans==true)
{
//alert(’Yes’);
document.Form1.hdnbox.value=’Yes’;
}
else
{
//alert(’No’);
document.Form1.hdnbox.value=’No’;}
}
</SCRIPT>
Step 3. trymain.aspx.vb

To display the value of the value selected by the user in the pop up write the following code
Response.Write(Request.Form(”hdnbox”))

Posted by Mahesh ( Tryangled )

Fetching title from HTML page with C#?

Monday, March 24th, 2008

You jnust enter the web address in the Url textbox, presses the button and the webpage title comes out in your screen.
private void button1_Click(object sender, EventArgs e)
{
//Fetched the title…
string sFullHtml = WebHelper.FetchHTML(textBox1.Text);
string sTitle = WebHelper.FetchTitleFromHTML(sFullHtml);
Response.write sTitle;
}
public class WebHelper
{
public static string FetchHTML(string sUrl)
{
System.Net.WebClient oClient = new System.Net.WebClient();
return oClient.DownloadString(sUrl);
//return new System.Text.UTF8Encoding().GetString(oClient.DownloadData(sUrl));
}
public static string FetchTitleFromHTML(string sHtml)
{
string regex = @”(?<=<title.*>)([\s\S]*)(?=</title>)”;
System.Text.RegularExpressions.Regex ex = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return ex.Match(sHtml).Value.Trim();
}
}

Posted by Mahesh ( Tryangled )

How to Convert String to Byte Array Function using ASP?

Monday, March 24th, 2008

ASP function to convert a string to a byte array.
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
</head>
<body>
<%
‘ String to byte array conversion function:
Function StringToByteArray(s)
Dim i, byteArray
For i=1 To Len(s)
byteArray = byteArray & ChrB(Asc(Mid(s,i,1)))
Next
StringToByteArray = byteArray
End Function

s = “ABCDEFG”
‘ Convert the string to a byte array.
Dim byteArray
byteArray = StringToByteArray(s)
‘ Display the length of the byte array
Response.Write CStr(LenB(byteArray)) & “<p>”
‘ Display the bytes as decimal integers:
Dim d
d = “”
For i = 1 To LenB(byteArray)
d = d & CStr(AscB(MidB(byteArray, i, 1))) & “,”
Next
Response.Write “<p>” & d & “</p>”

‘ Output: 65,66,67,68,69,70,71,
%>
</body>
</html>

Posted by Mahesh ( Tryangled )

View contents of your inbox using ASP?

Monday, March 24th, 2008

This shows how to view the contents of your inbox of a specified account . You will have to change the “password” and “email@youraddress.com” to your own details . Note this will only work on the local machine you are working with and you cannot access email accounts remotely.
Sample Code:
<%
‘create our variables
Dim cdoSession , strMessage , strMessages , objInbox , intMsgID
‘create an instance of the CDONTS.Session object
Set cdoSession = Server.CreateObject(”CDONTS.Session”)
‘this is your logon details
cdoSession.LogonSMTP “password” , “email@youraddress.com”
‘open the inbox folder
Set objInbox = cdoSession.Inbox
‘retrieve the messages collection
Set strMessages = objInbox.Messages
intMsgID = Trim(Request.QueryString(”MsgId”))
If (”" = intMsgID Or Not IsNumeric (intMsgID)) Then
‘display all the messages
For Each strMessage in strMessages
i = i + 1
Response.Write “<a href=”"”
Response.Write Request.ServerVariables(”SCRIPT_NAME”)
Response.Write “?MsgId=” & i & “”">” & strMessage.Subject
Response.Write “</a>”
Response.Write ” sent by : ” & strMessage.Author
Response.Write “<br>”
Next
Else
Set strMessage = strMessages(intMsgID)
Response.Write “Subject : ” & strMessage.Subject & “<br>”
Response.Write “Sender : ” & strMessage.Sender & “<br>”
Response.Write “Message : ” & strMessage.Text
End If
‘log off from the session
cdoSession.LogOff
%>

Posted by Mahesh ( Tryangled )

Random image from a database using ASP?

Monday, March 24th, 2008

This example shows how to create a database using Access, in which we store the details of our image . We will then display one of these images at random.
First start access and create the database.

Sample Code:
<%
Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
‘declare our variables
Dim objConn , objRS , strConnect , RandomNo
‘create an instance of connection object
Set objConn = Server.CreateObject(”ADODB.Connection”)
‘open our database
objConn.Open “DBQ=” & Server.MapPath(”tryimage.mdb”)& “;Driver={Microsoft Access Driver (*.mdb)}”
‘create an instance of the recordset object
Set objRS = Server.CreateObject(”ADODB.Recordset”)
‘open the tblpostcard table
objRS.Open “Select * FROM tryimage” , objConn , 3, , adCmdTable
Randomize
‘create a random number between 1 and the amount of records in the
‘table and store this in RandomNo
RandomNo = Int(Rnd * objRS.RecordCount)
‘move to the RandomNo record
objRS.Move RandomNo
‘display the image
Response.Write “<img src = ” & objRS.Fields(”url”) & “>”
Response.Write “<br>”
‘display our description
Response.Write objRS.Fields(”description”)
‘tidy up by closing and destroying objects
objRS.Close
Set objRS = Nothing
Set objConn = Nothing
%>

Posted by Mahesh ( Tryangled )

How to Check for an integer in ASP?

Monday, March 24th, 2008

This example checks for a valid positive or negative integer . It also checks whether a string contains numbers.

<%
Function CheckInt(intNumber)
‘declare our variables
Dim objRegExp , blnValid
‘create an instance of regexp object
Set objRegExp = New RegExp
‘this is our pattern to check for integers
objRegExp.Pattern = “^[-+]?\d*$”
‘chech what was entered
blnValid = objRegExp.Test(intNumber)
‘if true then print this message
If blnValid Then
Response.Write “This contains integers<br>”
‘if false display this message
Else
Response.Write “does not contain any integers<br>”
End If
End Function
%>
<% CheckInt(12) %>
<% CheckInt(-1545) %>
<% CheckInt(”asadsda”) %>
<% CheckInt(”12″) %>

Posted by Mahesh ( Tryangled )

How to validate dollar amount in ASP?

Monday, March 24th, 2008

<%
Function ValidDollars(dblDollar)
‘our variables
Dim tryExp , blnValid
‘create an instance of the Regular expression object
Set tryExp = New tryExp
‘our pattern to match check for correct format
tryExp.Pattern = “^\$[0-9]+(\.[0-9][0-9])?$”
’store the result , true or false in blnValid
blnValid = tryExp.Test(dblDollar)
‘if test is true display the first message
If blnValid Then
Response.Write “This is a correctly formatted dollar amount<br>”
‘incorrect response here
Else
Response.Write “Incorrectly formatted dollar amount<br>”
End If
End Function
%>
<% ValidDollars(”$1.15″) ‘valid %>
<% ValidDollars(”1.422″) ‘invalid too many decimal place %>
<% ValidDollars(”1.65″) ‘invalid no $ sign %>
<% ValidDollars(”$81″) ‘valid %>

Posted by Mahesh ( Tryangled )