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