Tuesday, April 21, 2009

Using button field to get datakey

Scenario:

Consider we have a data grid view which show a list of books from database and you want a link button which will edit the same grid view entry but in a separate page and on separate form with the help of the primary key which you may want to send to another page using query string or else.
So the task is to get the primary key of the row on which the link button is pressed.

Solutions:

it can slove by three way
1-by using command argument
2- By using hidden field
3-by using datakeys

First of all doing it convert button field into template

Approach (1)

1- By using Command Argument.





‘ runat=”server”>Edit





Then we write simple code on onRowCommand Event

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

If e.CommandName.ToLower() = “edit” Then

Dim Key As String = e.CommandArgument ’Getting id Value

Response.Redirect(makeeditForm.aspx?id=” + Key) ‘redirecting

End If

End Sub
-----------------------------------------------------------------------------------
Approach (2)

2-By using hidden field



‘ />
Edit





Then we write simple code on onRowCommand Event

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

If e.CommandName.ToLower() = “edit” Then

Dim link As LinkButton = CType(e.CommandSource, LinkButton) ‘getting clicked button

Dim Key As String = CType(link.Parent.FindControl(”hidden1”), HiddenField).Value ‘getting id Value

Response.Redirect(makeeditForm.aspx?id=” + Key) ‘redirecting

End If

End Sub
---------------------------------------------------------------------------------------
Approach(3)

3-by using datakeys





Edit





Then we write simple code on onRowCommand Event

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

If e.CommandName.ToLower() = “edit” Then

Dim link As LinkButton = CType(e.CommandSource, LinkButton) ‘getting clicked button

Dim Row As GridViewRow = link.BindingContainer ‘Getting current row to get index

Dim Key As String = GridView1.DataKeys(Row.RowIndex)(0).ToString()

Response.Redirect(makeeditForm.aspx?id=” + Key) ‘redirecting

End If

End Sub

Monday, April 20, 2009

get the computer Name and its IP address

The following example demonstrates the use of System.NET.DNS namespace


Imports system
Imports System.Net.DNS // namespace to use class
Public Class GetIP

Shared Function GetIPAddress() As String
Dim oAddr As System.Net.IPAddress
Dim sAddr As String
With system.Net.DNS.GetHostByName(system.Net.DNS.GetHostName())
oAddr = New System.Net.IPAddress(.AddressList(0).Address)
sAddr = oAddr.ToString
End With
GetIPAddress = sAddr
End Function

Shared Sub main()
Dim shostname As String
shostname = system.Net.DNS.GetHostName
console.writeline("Your Machine Name = " & shostname)
'Call Get IPAddress
console.writeline("Your IP = " & GetIPAddress)
End Sub

End Class

How many types of cookies are available in asp?

There are two type of cookies in asp.net .
--------------------------------------------------------------------------------

Persistent cookies are stored on your computer hard disk. They stay on your hard disk and can be accessed by web servers until they are deleted or have expired. Persistent cookies are not affected by your browser setting that deletes temporary files when you close your browser.

Non-persistent cookies are saved only while your web browser is running. They can be used by a web server only until you close your browser. They are not saved on your disk. Microsoft Internet Explorer 5.5 can be configured to accept non-persistent cookies but reject persistent cookies.

Clear Cache in asp.net

it clear cache in memory
write this in aspx.vb or aspx.c# code file
<%@ OutputCache Location="None" VaryByParam="None" %>
------------------------------------------------------

write this at load event of page
Response.Cache.SetCacheability(HttpCacheability.NoCache)