Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Partial Public Class WebForm17
Inherits System.Web.UI.Page
Dim Service As New WebService1
Dim ConnStr As String = "server=(local);database=police;uid=police;password=police"
Dim Conn As New SqlConnection(ConnStr)
Dim strPeID As String = ""
Dim Sql As String
'Dim Sql As String = "SELECT * FROM TblPersonel"
Dim Cmd As SqlCommand
'Dim Idx As String = Request.QueryString("id")
Dim MyDa As New SqlDataAdapter(Sql, Conn)
Dim Da As New SqlDataAdapter
Dim Ds As New DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GridView1.AllowPaging = True
GridView1.PageSize = 5
If Not Page.IsPostBack Then
GridView1.DataSource = CreateDataTable()
GridView1.DataBind()
'ReadSIdToDdl()
End If
End Sub
'*** DataTable ***'
Protected Function CreateDataTable() As DataTable
Dim strConnString As String = "server=(local);database=police;uid=police;password=police"
Dim objConn As New SqlConnection(strConnString)
Dim strCmdString As String = "SELECT * FROM [TblCheckEvent]"
Dim objCmd As New SqlCommand(strCmdString, objConn)
Dim Dt As New DataTable()
Dim objAdapter As New SqlDataAdapter(objCmd)
objAdapter.Fill(Dt)
Return Dt '*** Return DataTable ***'
End Function
Sub Search(ByVal SqlSearch As String)
Dim Found As Boolean = False
Dim DView As DataView
'Sql = "Select * From Stock Where SId=" + "'" + Request.Form("TId") + "'" + " Order by SId "
Da = New SqlDataAdapter(SqlSearch, Conn)
Da.Fill(Ds, "TblCheckEvent")
DView = Ds.Tables("TblCheckEvent").DefaultView
If DView.Count <= 0 Then 'ถ้า DView.Count มีค่ามากกว่า 0 แสดงว่ามีข้อมูลรหัสนั้นๆ แล้ว
Found = False
MsgBox("ค้นหาข้อมูลไม่พบ")
GridView1.Visible = False
Exit Sub
Else
Found = True
GridView1.Visible = True
'LMsg.Text = "<Center>...มีข้อมูลรหัส :" & TId.Text & "จ้า...</Center>"
GridView1.DataSource = Ds.Tables("TblCheckEvent").DefaultView
'GridView1.DataKeyField = "Pe_Id"
GridView1.DataBind()
Conn.Close()
End If
End Sub
Sub SearchField()
If Len(Trim(TxtSearch.Text)) <= 0 Then
Response.Write("<script language=javascript>alert('กรุณาใส่ข้อมูลสำหรับการค้นหา');</script")
Else
If UCase(DdlField.SelectedItem.Value) = UCase("Date") Then
'Sql = "Select * From TblPersonel Where Pe_Id ="
Sql = "Select * From TblCheckEvent Where Ch_DateAccident Like"
Sql = Sql & "'%" & Trim(TxtSearch.Text) & "%'"
ElseIf UCase(DdlField.SelectedItem.Value) = UCase("StyleEvents") Then
Sql = "Select * From TblCheckEvent Where Ch_StyleEvents Like" '//Likeเป็นการค้นหาแบบตัวใดตัวหนึ่งก็ได้แต่ต้องให้%ปิดหัวท้าย
Sql = Sql & "'%" & Trim(TxtSearch.Text) & "%'"
Else
Sql = "Select * From TblCheckEvent Where Ch_CheifInspector Like" '//Likeเป็นการค้นหาแบบตัวใดตัวหนึ่งก็ได้แต่ต้องให้%ปิดหัวท้าย
Sql = Sql & "'%" & Trim(TxtSearch.Text) & "%'"
'Else
' Sql = "Select * From T Where Zipcode ="
' Sql = Sql & "'" & Val(TxtSearch.Text) & "'"
End If
End If
Search(Sql)
End Sub
Protected Sub DdlField_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DdlField.SelectedIndexChanged
SearchField()
End Sub
Protected Sub BtnGo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnGo.Click
SearchField()
End Sub
Protected Sub DetailsViewBindData()
Sql = "SELECT * FROM TblCheckEvent WHERE Ch_Id = '" & strPeID & "' "
Dim dtReader As SqlDataReader
Cmd = New SqlCommand(Sql, Conn)
Conn.Open()
dtReader = Cmd.ExecuteReader()
'*** BindData to DetailsView ***'
DetailsView1.DataSource = dtReader
DetailsView1.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
Conn.Close()
Conn = Nothing
End Sub
Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.DataSource = CreateDataTable()
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
Private Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.Visible = False
strPeID = GridView1.DataKeys(e.NewEditIndex).Value.ToString
DetailsView1.Visible = True
DetailsViewBindData()
End Sub
Private Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) Handles DetailsView1.ModeChanging
Select Case e.NewMode
Case DetailsViewMode.Edit
Dim lblChId As Label = CType(DetailsView1.FindControl("lblChId"), Label)
strPeID = lblChId.Text
DetailsView1.ChangeMode(DetailsViewMode.Edit)
DetailsViewBindData()
Case DetailsViewMode.ReadOnly
'DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
DetailsView1.Visible = False
GridView1.Visible = True
Response.Redirect("CheckEdit.aspx")
End Select
End Sub
Private Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Dim txtTextBox As TextBox = CType(DetailsView1.FindControl("TextBox1"), TextBox)
'*** Old PeID ***'
Dim lblDate0 As Label = CType(DetailsView1.FindControl("lblDate0"), Label)
strPeID = lblDate0.Text
'*** PersonelID ***'
Dim txtCheckDate As eWorld.UI.CalendarPopup = CType(DetailsView1.FindControl("txtEditCheckDate"), eWorld.UI.CalendarPopup)
'*** Position ***'
Dim txtReTime As DropDownList = CType(DetailsView1.FindControl("txtEditTime"), DropDownList)
Dim txtTimeAcc As TextBox = CType(DetailsView1.FindControl("txtEditTimeAcc"), TextBox)
Dim txtStyle As TextBox = CType(DetailsView1.FindControl("txtEditStyle"), TextBox)
Dim txtPlace As TextBox = CType(DetailsView1.FindControl("txtEditPlace"), TextBox)
Dim txtCircum As TextBox = CType(DetailsView1.FindControl("txtEditCircum"), TextBox)
Dim txtFlag As TextBox = CType(DetailsView1.FindControl("txtEditFlag"), TextBox)
Dim txtChief As DropDownList = CType(DetailsView1.FindControl("txtEditChief"), DropDownList)
Dim txtOffer As DropDownList = CType(DetailsView1.FindControl("txtEditOffer"), DropDownList)
Dim txtReceiver As DropDownList = CType(DetailsView1.FindControl("txtEditReceiver"), DropDownList)
'Dim myDate As Date
'myDate = CalendarPopup1.SelectedDate
'Dim strdate As String = myDate.ToString.Substring(0, 10)
'txtCheckDate.Text = strdate
Sql = "UPDATE TblCheckEvent SET Ch_Id = '" & txtTextBox.Text.Replace("'", "''") & "' " & _
" ,Ch_DateAccident='" & txtCheckDate.SelectedDate & "' " & _
" ,Re_Time = '" & txtReTime.SelectedItem.Text & "' " & _
" ,Ch_TimeAccident = '" & txtTimeAcc.Text & "' " & _
" ,Ch_StyleEvents = '" & txtStyle.Text & "' " & _
" ,Ch_Place = '" & txtPlace.Text & "' " & _
" ,Ch_Circumstance = '" & txtCircum.Text & "' " & _
" ,Ch_Flag = '" & txtFlag.Text & "' " & _
" ,Ch_CheifInspector = '" & txtChief.SelectedItem.Text & "' " & _
" ,Ch_Offer = '" & txtOffer.SelectedItem.Text & "' " & _
" ,Ch_Receiver = '" & txtReceiver.SelectedItem.Text & "' " & _
" WHERE Ch_Id = '" & strPeID & "'"
Cmd = New SqlCommand(Sql, Conn)
Conn.Open()
Cmd.ExecuteNonQuery()
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)
DetailsView1.Visible = False
GridView1.Visible = True
'GridViewBindData()
Response.Redirect("CheckEdit.aspx")
End Sub
End Class