Monday, December 13, 2010

Disable the Back button in Browser

You can use either server side solution or client side (javascript solution)

Server side
After logging out use this code,

Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetAllowResponseInBrowserHistory(False)


client side
include the following text in the head portion of each page.

<script>
  history.forward();
</script>

Thursday, December 9, 2010

Query to get All Column names of a table in Sql server

Solution

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'tablename'

Friday, November 26, 2010

Removing(Deleting) Querystring in code

Problem

Request.QueryString.Remove("foo")

If u r removing just like this will not work. U will get error states that 'collection is read-only'.

Solution

So the solution is, before deleting the querystring u need to write the below code.

In C#,
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

// make collection editable
isreadonly.SetValue(this.Request.QueryString, false, null);

// remove
this.Request.QueryString.Remove("foo");


In VB,
Dim isreadonly As PropertyInfo = GetType(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance Or BindingFlags.NonPublic)

' make collection editable
isreadonly.SetValue(Me.Request.QueryString, False, Nothing)

' remove
Me.Request.QueryString.Remove("foo")


Now try. It will work. have a great day. Enjoy....

Tuesday, November 2, 2010

Photo Viewer

The index will go here.Slideshow Mode (10 second delay):On / OffPrevious Next
PhotoCaption

Friday, October 22, 2010

GridView - Export to Excel

Solution

Step 1

Paste this code on export button click.

Protected Sub imgExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgExport.Click

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=filename.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"

Dim stringWrite As New StringWriter
Dim htmlWrite As New HtmlTextWriter(stringWrite)

grid.AllowSorting = False
grid.AllowPaging = False
subBindGrid()

grid.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()

grid.AllowSorting = True
grid.AllowPaging = True
subBindGrid()

End Sub


If u run this code now, you will get error - 'Control 'ctl00_ContentPlaceHolder1_grid' of type 'GridView' must be placed inside a form tag with runat=server'. so, Don't worry. Just follow Step 2.

Step 2

Just copy and paste the below code. Its done.

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

' Confirms that an HtmlForm control is rendered for the specified ASP.NET
' server control at run time.

End Sub

Tuesday, September 28, 2010

Merging Pdf files in Asp.Net

Solution

1. First u need to download 'itextsharp.dll' from the internet.
2. Then add this dll using add reference.
3. Import the below name space.

Imports iTextSharp.text
Imports iTextSharp.text.pdf


4. Use the below function to merge the pdf.

Public Sub Merge()

Private ReadOnly m_documents As List(Of PdfReader)
Dim newDocument As Document = Nothing

//Merge pdf path. All the pdf files will merged here and become one pdf.
ByVal outputStream As Stream
outputStream = New FileStream(pdfMergePath, FileMode.Create)

Try
newDocument = New Document()
Dim pdfWriter__1 As PdfWriter = PdfWriter.GetInstance(newDocument, outputStream)

newDocument.Open()
newDocument.SetPageSize(PageSize.A3)

Dim pdfContentByte__2 As PdfContentByte = pdfWriter__1.DirectContentUnder

// Add all the pdf to merge
m_documents.Add(New PdfReader(pdfPath1))
m_documents.Add(New PdfReader(pdfPath2))
m_documents.Add(New PdfReader(pdfPath2))

For Each doc In m_documents
totalPages += doc.NumberOfPages
Next

Dim currentPage As Integer = 1
For Each pdfReader As PdfReader In m_documents
For page As Integer = 1 To pdfReader.NumberOfPages
newDocument.NewPage()

Dim importedPage As PdfImportedPage = pdfWriter__1.GetImportedPage(pdfReader, page)
pdfContentByte__2.AddTemplate(importedPage, 0, 0)

pdfContentByte__2.BeginText()
m_baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
pdfContentByte__2.SetFontAndSize(m_baseFont, 9)
pdfContentByte__2.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, String.Format("{0} of {1}", currentPage, totalPages), 520, 5, 0)
pdfContentByte__2.EndText()

currentPage = currentPage + 1
Next
Next

Finally
outputStream.Flush()
If newDocument IsNot Nothing Then
newDocument.Close()
End If
outputStream.Close()
End Try

End Sub

Browser display area or view port width & height using Javascript

Solution

The below script will help u to get the width and height of the browser view port area.

function fnScreenWidth() {
//for IE
var viewportwidth;
var viewportheight;

// the more standards compliant browsers (mozilla/netscape/opera/IE7) //use window.innerWidth and window.innerHeight

if (typeof window.innerWidth != 'undefined') {
viewportwidth = window.innerWidth;
viewportheight = window.innerHeight;
}

// IE6 in standards compliant mode (i.e. with a valid doctype as the //first line in the document)

else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
viewportwidth = document.documentElement.clientWidth;
viewportheight = document.documentElement.clientHeight;
}

// older versions of IE
else {
viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
viewportheight = document.getElementsByTagName('body')[0].clientHeight;
}


alert('Your viewport width is ' + viewportwidth + 'x' + viewportheight);

}


You can call this java script from body tag,

body onload="fnScreenWidth()"

Get Physical Path of Application in ASP.Net

Solution

HttpRuntime.AppDomainAppPath

This wil gets the physical drive path of the application directory for the application hosted in the current application domain.

Monday, September 27, 2010

Accessing session in a class file in Asp.Net

Solution

The Session property provides programmatic access to the properties and methods of the HttpSessionState class. Because, ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. For example, you can use just Session("SessionVariable1") to get or set the value of the session state variable SessionVariable1. However, class file will not inherit System.web namespace. So we need access like below,

HttpContext.Current.Session("Session_Name")

Password Encoding and Decoding in ASP.Net

Encoding

Public Function fnBase64Encode(ByVal strPwd As String) As String
Dim byteEncode(strPwd.Length) As Byte
Dim strEncoded As String

byteEncode = System.Text.Encoding.UTF8.GetBytes(strPwd)
strEncoded = Convert.ToBase64String(byteEncode)

Return strEncoded
End Function


Decoding

Public Function fnBase64Decode(ByVal strPwd As String) As String
Dim encoder As New System.Text.UTF8Encoding()
Dim utf8Decode As System.Text.Decoder
Dim byteDecode() As Byte
Dim intCharCnt As Integer
Dim strDecoded As String

utf8Decode = encoder.GetDecoder()
byteDecode = Convert.FromBase64String(strPwd)
intCharCnt = utf8Decode.GetCharCount(byteDecode, 0, byteDecode.Length)

Dim charDecoded(intCharCnt) As Char

utf8Decode.GetChars(byteDecode, 0, byteDecode.Length, charDecoded, 0)
strDecoded = New String(charDecoded)

Return strDecoded
End Function