Imports System.Data
Imports System.Data.OleDb
Partial Class frm_edit2
Inherits System.Web.UI.Page
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtReader As OleDbDataReader
Dim strConnString, strSQL As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
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
Protected Sub save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles save.Click
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
End Class
มัน ขึ้นเแบบนี้ค่ะ
Server Error in '/Project' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:
1. Add a "Debug=true" directive at the top of the file that generated the error. Example:
<%@ Page Language="C#" Debug="true" %>
or:
2) Add the following section to the configuration file of your application:
Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.
Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
frm_edit2.ViewData() +556
frm_edit2.Page_Load(Object sender, EventArgs e) +128
System.Web.UI.Control.OnLoad(EventArgs e) +73
System.Web.UI.Control.LoadRecursive() +52
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2184
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927