001.
Imports
System.Data
002.
Imports
System.Data.SqlClient
003.
004.
Public
Class
MStaff
005.
Inherits
System.Web.UI.Page
006.
007.
008.
009.
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
Handles
Me
.Load
010.
If
Not
IsPostBack
Then
011.
TxtSurname.Text =
""
012.
Txtage.Text =
""
013.
TxtP.Text =
""
014.
TxtSurname.Focus()
015.
End
If
016.
BSDel.Attributes.Add(
"onclick"
,
"if(!window.confirm('Are you sure delete record??'))"
&
"return false;"
)
017.
If
Not
IsPostBack
Then
018.
BindData()
019.
End
If
020.
021.
End
Sub
022.
023.
Protected
Sub
BSDel_Click(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
Handles
BSDel.Click
024.
025.
Dim
strConn
As
String
=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
026.
Dim
objConn
As
New
SqlConnection(strConn)
027.
objConn.Open()
028.
Dim
cmdSQL
As
String
=
"DELETE FROM dbo.Staff"
&
"WHERE StaffID=@StaffID"
029.
Dim
Cmd
As
New
SqlCommand(cmdSQL, objConn)
030.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffID"
, lblStaffID.Text))
031.
Cmd.ExecuteNonQuery()
032.
objConn.Close()
033.
BindData()
034.
lstStaff.Focus()
035.
lblMessage.Text =
"Successfully Delete:"
& TxtSurname.Text &
" "
& Txtage.Text &
" "
& TxtP.Text
036.
TxtSurname.Enabled =
False
037.
Txtage.Enabled =
False
038.
TxtP.Enabled =
False
039.
BSUpdate.Visible =
False
040.
BSDel.Visible =
False
041.
End
Sub
042.
043.
044.
Protected
Sub
BSAdd_Click(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
Handles
BSAdd.Click
045.
lblErrorSurname.Visible =
False
046.
lblErrorage.Visible =
False
047.
lblErrorP.Visible =
False
048.
049.
If
TxtSurname.Text =
""
Then
050.
lblErrorSurname.Visible =
True
051.
lblError.Text =
"Please Enter Name"
052.
TxtSurname.Focus()
053.
Return
054.
End
If
055.
If
Txtage.Text =
""
Then
056.
lblErrorage.Visible =
True
057.
lblError.Text =
"Please Enter age"
058.
Txtage.Focus()
059.
Return
060.
End
If
061.
If
TxtP.Text =
""
Then
062.
lblErrorP.Visible =
True
063.
lblError.Text =
"Please Enter P"
064.
TxtP.Focus()
065.
Return
066.
End
If
067.
068.
Dim
strConn
As
String
=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
069.
Dim
objConn
As
New
SqlConnection(strConn)
070.
objConn.Open()
071.
Dim
cmdSQL
As
String
=
"INSERT INTO dbo.Staff"
&
"(StaffName,StaffAge,StaffP)"
&
"VALUES(@StaffName,@StaffAge,@StaffP)"
072.
Dim
Cmd
As
New
SqlCommand(cmdSQL, objConn)
073.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffName"
, TxtSurname.Text))
074.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffAge"
, Txtage.Text))
075.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffP"
, TxtP.Text))
076.
Cmd.ExecuteNonQuery()
077.
objConn.Close()
078.
lblError.Text =
"Successfully Insert:"
& TxtSurname.Text &
" "
& Txtage.Text &
" "
& TxtP.Text
079.
TxtSurname.Text =
""
080.
Txtage.Text =
""
081.
TxtP.Text =
""
082.
TxtSurname.Focus()
083.
084.
End
Sub
085.
086.
087.
088.
089.
090.
Protected
Sub
BSUpdate_Click(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
Handles
BSUpdate.Click
091.
092.
093.
lblMessage.Text =
""
094.
lblErrorSurname.Visible =
False
095.
lblErrorage.Visible =
False
096.
lblErrorP.Visible =
False
097.
If
TxtSurname.Text =
""
Then
098.
lblErrorSurname.Visible =
True
099.
lblError.Text =
"Please Enter Name"
100.
TxtSurname.Focus()
101.
Return
102.
End
If
103.
If
Txtage.Text =
""
Then
104.
lblErrorage.Visible =
True
105.
lblError.Text =
"Please Enter age"
106.
Txtage.Focus()
107.
Return
108.
End
If
109.
If
TxtP.Text =
""
Then
110.
lblErrorP.Visible =
True
111.
lblError.Text =
"Please Enter P"
112.
TxtP.Focus()
113.
Return
114.
End
If
115.
116.
Dim
strConn
As
String
=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
117.
Dim
objConn
As
New
SqlConnection(strConn)
118.
objConn.Open()
119.
Dim
cmdSQL
As
String
=
"UPDATE dbo.Staff"
&
"SET StaffName=@StaffName,StaffAge=@StaffAge,StaffP=@StaffP "
&
"WHERE StaffID=@StaffID"
120.
Dim
Cmd
As
New
SqlCommand(cmdSQL, objConn)
121.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffID"
, lblStaffID.Text))
122.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffName"
, TxtSurname.Text))
123.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffAge"
, Txtage.Text))
124.
Cmd.Parameters.Add(
New
SqlParameter(
"@StaffP"
, TxtP.Text))
125.
Cmd.ExecuteNonQuery()
126.
objConn.Close()
127.
BindData()
128.
lstStaff.SelectedIndex = lstStaff.Items.IndexOf(lstStaff.Items.FindByValue(lblStaffID.Text))
129.
lstStaff.Focus()
130.
lblMessage.Text =
"Successfully Update:"
& TxtSurname.Text &
" "
& Txtage.Text &
" "
& TxtP.Text
131.
TxtSurname.Enabled =
False
132.
Txtage.Enabled =
False
133.
TxtP.Enabled =
False
134.
BSUpdate.Visible =
False
135.
BSDel.Visible =
False
136.
End
Sub
137.
138.
139.
Protected
Sub
BSEdit_Click(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
Handles
BSEdit.Click
140.
If
lstStaff.SelectedIndex < 0
Then
141.
lblMessage.Text =
"Please Select Staff"
142.
Return
143.
End
If
144.
Dim
strConn
As
String
=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
145.
Dim
objConn
As
New
SqlConnection(strConn)
146.
objConn.Open()
147.
Dim
cmdSQL
As
String
=
"SELECT StaffID,StaffName,StaffAge,StaffP"
&
"FROM dbo.Staff Where StaffID ="
& lstStaff.SelectedItem.Value
148.
Dim
Cmd
As
New
SqlCommand(cmdSQL, objConn)
149.
Dim
objDR
As
SqlDataReader
150.
objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
151.
While
objDR.Read()
152.
lblStaffID.Text = objDR(
"StaffID"
)
153.
TxtSurname.Text = objDR(
"StaffName"
)
154.
Txtage.Text = objDR(
"StaffAge"
)
155.
TxtP.Text = objDR(
"StaffP"
)
156.
End
While
157.
TxtSurname.Enabled =
True
158.
Txtage.Enabled =
True
159.
TxtP.Enabled =
True
160.
BSUpdate.Visible =
True
161.
BSDel.Visible =
True
162.
lblMessage.Text =
""
163.
lblErrorSurname.Visible =
False
164.
lblErrorage.Visible =
False
165.
lblErrorP.Visible =
False
166.
End
Sub
167.
168.
169.
170.
171.
172.
Sub
BindData()
173.
Dim
strConn
As
String
=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Rescuedb.mdf;Integrated Security=True;User Instance=True"
174.
Dim
objConn
As
New
SqlConnection(strConn)
175.
objConn.Open()
176.
Dim
cmdSQL
As
String
=
"SELECT StaffID+''+StaffName+''+StaffAge+''+StaffP As StaffData"
&
"FROM dbo.Staff "
177.
Dim
Cmd
As
New
SqlCommand(cmdSQL, objConn)
178.
179.
180.
181.
lstStaff.DataValueField =
"StaffID"
182.
lstStaff.DataTextField =
"StaffData"
183.
lstStaff.DataBind()
184.
If
lstStaff.Rows > 0
Then
185.
lstStaff.SelectedIndex = 0
186.
End
If
187.
lstStaff.Focus()
188.
objConn.Close()
189.
End
Sub
190.
191.
End
Class