Sunday, November 29, 2009

week 2 : asp.net

Caching ASP.NET applications
----------------------------


Caching asp.net applications improve the performance.

Dim strprod As String

Dim strSelect As String

Dim con As OleDbConnection

Dim cmd As OleDbCommand

Dim dr As OleDbDataReader

strprod= Request.Param("PID")


if strprod = Nothing then

strprod=1


End if


con = New OleDbConnection("Provider=Microsoft.jet.OLEDB.4.0;

Data Source=c:\Nwind.mdb"))

strSelect = "select * from Products where productdID= "

& strprod

cmd = New OleDbCommand(strSelect, con)

con.Open()
dr = cmd.ExecuteReader()
GridView1.DataSource = dr
GridView1.DataBind()

dr.Close()

con.Close()
-----------------------------------------------------------

Use the same procedure for the authors table of pubs database

with state as Varying parameter

--------------------------------------------------------
Protected Sub btn_WriteCookies_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_WriteCookies.Click

Response.Cookies("userName").Value = txtcookie_name.Text

Response.Cookies("userName").Expires =

DateTime.Now.AddDays(1)

End Sub

-------------------------------------------------------------
If Not Request.Cookies("userName") Is Nothing Then

txtcookie_name.Text =

server.HtmlEncode(Request.Cookies("userName").Value)


txtcookie_expired.Text =

Server.HtmlEncode(Request.Cookies("userName").Expires)

End If
--------------------------------------
Protected Sub btn_deleteCookies_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_deleteCookies.Click

If Not Request.Cookies("userName") Is Nothing Then

Response.Cookies("userName").Expires =

DateTime.Now.AddDays(-1D)

End If


End Sub

----------------------------------------------------

Dim con As OleDbConnection

Dim cmd As OleDbCommand

con = New OleDbConnection("Provider=Microsoft.jet.OLEDB.4.0;

Data Source=c:\pubs.mdb")

con.Open()

cmd = New OleDbCommand(txtquery.Text.ToString, con)


GridView1.DataSource = cmd.ExecuteReader()

GridView1.DataBind()

con.Close()

INNER JOIN:

LIST RECORDS ONLY IF THERE IS A MATCHING ON BOTH SIDES.

[ WHERE CLASS ]


LEFT JOIN

LIST ALL RECORDS FROM THE LEFT TABLE , EVEN

THERE IS NO MATCHING ON RIGHT TABLE

[ ON CLASS ]


RIGHT JOIN

LIST ALL RECORDS FROM THE RIGTH TABLE , EVEN

THERE IS NO MATCHING ON LEFT TABLE

[ ON CLASS ]


SELF JOIN

A TABLE JOINS TO ITSELF IS KNOWN AS

SELF JOIN



SELECT e.EMPID , e.EMPNAME "EMPLOYEE NAME" ,

m.EMPNAME "MANAGERS NAME"

FROM EMP e , EMP m

WHERE e.MGRID = m.EMPID

------------------------------------------------------------

web configuration file
------------------------

each web page has web configuration file.

Parameters can be intialized or modified in the

web configuration file.

web configuration file is an xml file.

----------------------------------------------------------










---------------------------------------------------------

Imports System.Configuration
---------------------------------------------------------

Dim str As String = ""

Dim con As SqlConnection

Dim cmd As SqlCommand

str = ConfigurationSettings.AppSettings("constring")

con = New SqlConnection(str)

cmd = New SqlCommand("select * from emp", con)

con.Open()

GridView1.DataSource = cmd.ExecuteReader

GridView1.DataBind()

con.Close()

------------------------------------------------------------

change the web config file for the

ms-access database. display authors table

in the grid view format.
----------------------------------------------------------



-------------------------------------------------------------
Dim con As OleDbConnection

Dim cmd As OleDbCommand

Dim str As String

str = ConfigurationSettings.AppSettings("constring")

con = New OleDbConnection(str)

cmd = New OleDbCommand("select * from authors", con)

con.Open()

GridView1.DataSource = cmd.ExecuteReader

GridView1.DataBind()
con.Close()
--------------------------------------------------------------









-------------------------------------------------------------
imports system.data.oledb
imports system.data.sqlclient
imports system.configuration
-------------------------------------------------------------
Dim conaccess As OleDbConnection
Dim consqlserver As sqlconnection

Dim cmdaccess As OleDbCommand
Dim cmdsqlcommand As SqlCommand

Dim str1, str2 As String

str1 = ConfigurationSettings.AppSettings("conaccessstr")
str2 = ConfigurationSettings.AppSettings("consqlserverstr")

conaccess = New OleDbConnection(str1)
consqlserver = New SqlConnection(str2)

cmdaccess=New OleDbCommand("select * from authors", conaccess)
cmdsqlcommand=New SqlCommand("Select * from emp", consqlserver)

conaccess.Open()
consqlserver.Open()

GridView1.DataSource = cmdaccess.ExecuteReader
GridView1.DataBind()

GridView2.DataSource = cmdsqlcommand.ExecuteReader
GridView2.DataBind()

conaccess.Close()
consqlserver.Close()
---------------------- End of Connection String---------------

------------------------Break Time ---------------------------

Error Handling or Exceptional Handlings
---------------------------------------


Dim con As SqlConnection

Dim cmd As SqlCommand

con = New SqlConnection("server=localhost ;uid=sa;

pwd=dbexpert;database=dbx; Connection Timeout=15")

cmd = New sqlcommand("Select * from products" , con)

try

con.open()

gridview1.datasource = cmd.executeReader()

gridview1.databind()

con.close()

Catch

Response.write("we are sorry , Server is down ")

End try


----------------------End of Today-----------------------
_

Public Function CTOF(ByVal C As Double)

As double

Return (C * 1.8 + 32)

End Function
------------------------------------------------

_


Public Function FTOC(ByVal F As Double)

As double

Return (F - 32) / 1.8


End Function

---------------------------------------------------
_

Public Function TotalMarks(ByVal x As Integer,

ByVal y As Integer,

ByVal z As Integer) As Integer

Return (x + y + z)

End Function

------------------------------------------------------------

write webmethod area_circle

to find the area of circle for a given radius (r)

r is double , return value is also double

---------------------------------------------------------

use dbx

go

create table userlist

( u_id int not null identity ,

u_username varchar(25) ,

u_password varchar(25) )

----------------------------------------------------

identity is a constraint , incremented by 1

everytime , whenever we insert a record
-----------------------------------------------------

insert into userlist (u_username,u_password)

values('dbexpert' , 'abc123')
------------------------------------------------------

insert into userlist (u_username,u_password)

values('sam' , 'secret')
------------------------------------------------------

delete from userlist

--------------------------------------------------------
Dim con As SqlConnection

Dim cmd As SqlCommand

LBLmessage.Text = ""

Dim found As Integer = 0

con = New SqlConnection("server=localhost;uid=sa;pwd=dbexpert;database=dbx")

cmd = New SqlCommand("select count(*) from userlist where u_username=@name

and u_password =@pwd", con)

con.Open()

cmd.Parameters.AddWithValue("@name", txtUserName.Text)

cmd.Parameters.AddWithValue("@pwd", txtPassword.Text)

found = cmd.ExecuteScalar()

If found > 0 Then

Response.Redirect("welcome.aspx")

Else
LBLmessage.Text = "Invalid username / password"
End If

con.Close()

-----------------------------------------------------------------------

Dim con As SqlConnection

Dim cmdfound As SqlCommand

Dim cmdinsert As sqlcommand

Dim found As Integer = 0

Dim rec As Integer =0


LBLmessage.Text = ""

con = New SqlConnection("server=localhost;uid=sa;pwd=dbexpert;database=dbx")

cmdfound = new sqlcommand("select count(*) from userlist

where u_username = @name" ,con)

con.open()

cmdfound.parameters.addwithvalue("@name" , txtnewusername.text)


found = cmdfound.executescalar()

if found > 0 then

lblmessage.text ="Use Name alreay in the database ,try different name"

else


cmd = New SqlCommand("insert into userlist (u_username,u_password)

values( @name ,@pwd)", con)


cmd.Parameters.AddWithValue("@name", txtnewUserName.Text)

cmd.Parameters.AddWithValue("@pwd", txtnewPassword.Text)


rec = cmd.executeNonQuery()


if rec > 0 then

LBLmessage.Text = "Ur Name is registered "

Response.redirect("default.aspx")

End If

con.Close()
-----------------------------------------------------------------------------

Dim con As SqlConnection

Dim cmdfound As SqlCommand

Dim cmdinsert As SqlCommand

Dim found As Integer = 0

Dim rec As Integer = 0

LBLmessage.Text = ""

con = New SqlConnection("server=localhost;uid=sa;pwd=dbexpert;database=dbx")

cmdfound = New SqlCommand("select count(*) from userlist

where u_username = @name", con)

con.Open()

cmdfound.Parameters.AddWithValue("@name", txtnewusername.Text)

found = cmdfound.ExecuteScalar()

If found > 0 Then

LBLmessage.Text = "Use Name alreay in the database ,try different name"

Else

cmdinsert = New SqlCommand("insert into userlist (u_username,u_password) values( @name ,@pwd)", con)

cmdinsert.Parameters.AddWithValue("@name", txtnewusername.Text)

cmdinsert.Parameters.AddWithValue("@pwd", txtnewpassword.Text)

rec = cmdinsert.ExecuteNonQuery()

If rec > 0 Then

LBLmessage.Text = "Ur Name is registered "

Response.Redirect("default.aspx")

End If

End If

con.Close()

No comments: