<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<script runat="server">
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtReader As OleDbDataReader
Dim strConnString,strSQL As String
Sub Page_Load(sender As Object, e As EventArgs)
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/mydatabase.mdb") & ";Jet OLEDB:Database Password=;"
objConn.ConnectionString = strConnString
objConn.Open()
IF Not Page.IsPostBack() Then
ViewData()
End If
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
If Not Page.IsPostBack() Then
Dim strSQL As String
strSQL = "SELECT *FROM tb_type"
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
With Me.listtype_id
.DataSource = dt
.DataTextField = "type_name"
.DataValueField = "type_id"
.DataBind()
End With
listtype_id.SelectedIndex = listtype_id.Items.IndexOf(listtype_id.Items.FindByValue("type_id"))
End If
End Sub
Sub ViewData()
'*** DataTable ***'
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
strSQL = "SELECT * FROM tb_book WHERE book_id = '" & Request.QueryString("book_id") & "' "
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
Me.txtbook_id.Text = dt.Rows(0)("book_id")
Me.txtbook_name.Text = dt.Rows(0)("book_name")
Me.txtbook_by.Text = dt.Rows(0)("book_by")
Me.txtbook_number.Text = dt.Rows(0)("book_number")
Me.txtbook_detail.Text = dt.Rows(0)("book_detail")
Me.txtbook_price.Text = dt.Rows(0)("book_price")
Me.listtype_id.SelectedItem.Value = dt.Rows(0)("type_id")
End If
End Sub
Sub save_Click(ByVal sender As Object, ByVal e As EventArgs)
strSQL = "UPDATE tb_book SET " & _
" book_id = '" & Me.txtbook_id.Text & "' " & _
" ,book_name = '" & Me.txtbook_name.Text & "' " & _
" ,book_by = '" & Me.txtbook_by.Text & "' " & _
" ,book_number = '" & Me.txtbook_number.Text & "' " & _
" ,book_detail = '" & Me.txtbook_detail.Text & "' " & _
" ,book_price = '" & Me.txtbook_price.Text & "' " & _
" ,type_id = '" & Me.listtype_id.SelectedItem.Value & "' " & _
" WHERE book_id = '" & Request.QueryString("book_id") & "' "
objCmd = New OleDbCommand
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Me.pnlAdd.Visible = False
Try
objCmd.ExecuteNonQuery()
Me.lblstatus.Text = "แก้ไขข้อมูลเรียบร้อยแล้ว"
Me.lblstatus.Visible = True
Catch ex As Exception
Me.lblstatus.Text = "ไม่สามารถแก้ไขข้อมูลได้"
End Try
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
</script>