Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > ASP.NET > ASP.NET Microsoft Access (System.Data.OleDb) > ASP.NET Microsoft Access Edit/Update Record


ASP.NET Microsoft Access Edit/Update Record

ASP.NET Microsoft Access Edit/Update Record เป็นตัวอย่างการเขียน ASP.NET กับ Microsoft Access ในการเรียกข้อมูลจากฐานข้อมูลมาแก้ไข

Instance NameSpace

VB.NET
1.Imports System.Data
2.Imports System.Data.OleDb


ASP.NET & System.Data.OleDb

Language Code : VB.NET || C#

AspNetAccessEditListRecord.aspx

001.<%@ Import Namespace="System.Data"%>
002.<%@ Import Namespace="System.Data.OleDb"%>
003.<%@ Page Language="VB" %>
004.<script runat="server">
005. 
006.    Dim objConn As OleDbConnection
007.    Dim objCmd As OleDbCommand
008. 
009.    Sub Page_Load(sender As Object, e As EventArgs)
010.        Dim strConnString As String
011.        strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
012.        objConn = New OleDbConnection(strConnString)
013.        objConn.Open()
014. 
015.        BindData()
016.    End Sub
017. 
018.    Sub BindData()
019.        Dim strSQL As String
020.        strSQL = "SELECT * FROM customer"
021. 
022.        Dim dtReader As OleDbDataReader
023.        objCmd = New OleDbCommand(strSQL, objConn)
024.        dtReader = objCmd.ExecuteReader()
025.         
026.        '*** BindData to Repeater ***'
027.        myRepeater.DataSource = dtReader
028.        myRepeater.DataBind()
029. 
030.        dtReader.Close()
031.        dtReader = Nothing
032. 
033.    End Sub
034. 
035.    Sub Page_UnLoad()
036.        objConn.Close()
037.        objConn = Nothing
038.    End Sub
039. 
040.    Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
041. 
042.            '*** CustomerID ***'
043.            Dim lblCustomerID As Label = CType(e.Item.FindControl("lblCustomerID"),Label)
044.            IF Not IsNothing(lblCustomerID) Then
045.                lblCustomerID.Text = e.Item.DataItem("CustomerID")
046.            End IF
047. 
048.            '*** Name ***'
049.            Dim lblName As Label = CType(e.Item.FindControl("lblName"),Label)
050.            IF Not IsNothing(lblName) Then
051.                lblName.Text = e.Item.DataItem("Name")
052.            End IF
053. 
054.            '*** Email ***'
055.            Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"),Label)
056.            IF Not IsNothing(lblEmail) Then
057.                lblEmail.Text = e.Item.DataItem("Email")
058.            End IF
059. 
060.            '*** CountryCode ***'
061.            Dim lblCountryCode As Label = CType(e.Item.FindControl("lblCountryCode"),Label)
062.            IF Not IsNothing(lblCountryCode) Then
063.                lblCountryCode.Text = e.Item.DataItem("CountryCode")
064.            End IF
065. 
066.            '*** Budget ***'
067.            Dim lblBudget As Label = CType(e.Item.FindControl("lblBudget"),Label)
068.            IF Not IsNothing(lblBudget) Then
069.                lblBudget.Text = e.Item.DataItem("Budget")
070.            End IF
071. 
072.            '*** Used ***'
073.            Dim lblUsed As Label = CType(e.Item.FindControl("lblUsed"),Label)
074.            IF Not IsNothing(lblUsed) Then
075.                lblUsed.Text = e.Item.DataItem("Used")
076.            End IF
077. 
078.            '*** Hyperlink ***'
079.            Dim hplEdit As Hyperlink = CType(e.Item.FindControl("hplEdit"),Hyperlink)
080.            IF Not IsNothing(hplEdit) Then
081.                hplEdit.Text = "Edit"
082.                hplEdit.NavigateUrl = "AspNetAccessEditForm.aspx?CustomerID=" & e.Item.DataItem("CustomerID")
083.            End IF
084.             
085.    End Sub
086. 
087.</script>
088.<html>
089.<head>
090.<title>ThaiCreate.Com ASP.NET - Microsoft Access</title>
091.</head>
092.<body>
093.    <form id="form1" runat="server">
094.    <asp:Repeater id="myRepeater" runat="server">
095.    <HeaderTemplate>
096.        <table border="1">
097.            <tr>
098.                <th>CustomerID</th>
099.                <th>Name</th>
100.                <th>Email</th>
101.                <th>CountryCode</th>
102.                <th>Budget</th>
103.                <th>Used</th>
104.                <th>Edit</th>
105.            </tr>
106.    </HeaderTemplate>
107.    <ItemTemplate>
108.        <tr>
109.            <td align="center"><asp:Label id="lblCustomerID" runat="server"></asp:Label></td>
110.            <td><asp:Label id="lblName" runat="server"></asp:Label></td>
111.            <td><asp:Label id="lblEmail" runat="server"></asp:Label></td>
112.            <td align="center"><asp:Label id="lblCountryCode" runat="server"></asp:Label></td>
113.            <td align="right"><asp:Label id="lblBudget" runat="server"></asp:Label></td>
114.            <td align="right"><asp:Label id="lblUsed" runat="server"></asp:Label></td>
115.            <td align="right"><asp:Hyperlink id="hplEdit" runat="server"></asp:Hyperlink></td>
116.        </tr>        
117.    </ItemTemplate>
118.    </asp:Repeater>
119.    </form>
120.</body>
121.</html>



AspNetAccessEditForm.aspx

001.<%@ Page Language="VB" %>
002.<%@ import Namespace="System.Data" %>
003.<%@ import Namespace="System.Data.OleDb" %>
004.<script runat="server">
005.    Dim objConn As New OleDbConnection
006.    Dim objCmd As New OleDbCommand
007.    Dim dtReader As OleDbDataReader
008.    Dim strConnString,strSQL As String
009. 
010.    Sub Page_Load(sender As Object, e As EventArgs)
011.        strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
012.        objConn.ConnectionString = strConnString
013.        objConn.Open()
014. 
015.        IF Not Page.IsPostBack() Then
016.            ViewData()
017.        End IF 
018.    End Sub
019. 
020.    Sub ViewData()
021.        '*** DataTable ***'
022.        Dim dtAdapter As OleDbDataAdapter
023.        Dim dt As New DataTable
024.        strSQL = "SELECT * FROM customer WHERE CustomerID = '"& Request.QueryString("CustomerID") &"' "
025.        dtAdapter = New OleDbDataAdapter(strSQL, objConn)
026.        dtAdapter.Fill(dt)
027. 
028.        If dt.Rows.Count > 0 Then
029.            Me.txtCustomerID.Text = dt.Rows(0)("CustomerID")
030.            Me.txtName.Text = dt.Rows(0)("Name")
031.            Me.txtEmail.Text = dt.Rows(0)("Email")
032.            Me.txtCountryCode.Text = dt.Rows(0)("CountryCode")
033.            Me.txtBudget.Text = dt.Rows(0)("Budget")
034.            Me.txtUsed.Text = dt.Rows(0)("Used")
035.        End IF
036.    End Sub
037.     
038.    Sub btnSave_Click(sender As Object, e As EventArgs)                
039.         
040.        strSQL = "UPDATE customer SET " & _
041.        " CustomerID = '"& Me.txtCustomerID.Text &"' " & _
042.        " ,Name = '"& Me.txtName.Text &"' " & _
043.        " ,Email = '"& Me.txtEmail.Text &"' " & _
044.        " ,CountryCode = '"& Me.txtCountryCode.Text &"' " & _
045.        " ,Budget = '"& Me.txtBudget.Text &"' " & _
046.        " ,Used = '"& Me.txtUsed.Text &"' " & _
047.        " WHERE CustomerID = '" & Request.QueryString("CustomerID") & "' "
048. 
049.        objCmd = New OleDbCommand
050.        With objCmd
051.           .Connection = objConn
052.           .CommandText = strSQL
053.           .CommandType = CommandType.Text        
054.        End With
055.                         
056.        Me.pnlAdd.Visible = False
057.        Try
058.            objCmd.ExecuteNonQuery()           
059.            Me.lblStatus.Text = "Record Updated"
060.            Me.lblStatus.Visible = True
061.        Catch ex As Exception
062.            Me.lblStatus.Text = "Record can not update"
063.        End Try
064. 
065.    End Sub
066. 
067.    Sub Page_UnLoad()
068.        objConn.Close()
069.        objConn = Nothing
070.    End Sub
071. 
072.</script>
073.<html>
074.<head>
075.    <title>ThaiCreate.Com ASP.NET - Microsoft Access</title>
076.</head>
077.<body>
078.    <form id="form1" runat="server">
079.        <asp:Panel id="pnlAdd" runat="server">
080.            <table width="353" border="1">
081.                <tbody>
082.                    <tr>
083.                        <td width="102">
084.                            &nbsp;<asp:Label id="lblCustomerID" runat="server" text="CustomerID"></asp:Label></td>
085.                        <td width="235">
086.                            &nbsp;<asp:TextBox id="txtCustomerID" runat="server" Width="79px"></asp:TextBox>
087.                        </td>
088.                    </tr>
089.                    <tr>
090.                        <td>
091.                            &nbsp;<asp:Label id="lblName" runat="server" text="Name"></asp:Label></td>
092.                        <td>
093.                            &nbsp;<asp:TextBox id="txtName" runat="server" Width="177px"></asp:TextBox>
094.                        </td>
095.                    </tr>
096.                    <tr>
097.                        <td>
098.                            &nbsp;<asp:Label id="lblEmail" runat="server" text="Email"></asp:Label></td>
099.                        <td>
100.                            &nbsp;<asp:TextBox id="txtEmail" runat="server" Width="155px"></asp:TextBox>
101.                        </td>
102.                    </tr>
103.                    <tr>
104.                        <td>
105.                            &nbsp;<asp:Label id="lblCountryCode" runat="server" text="CountryCode"></asp:Label></td>
106.                        <td>
107.                            &nbsp;<asp:TextBox id="txtCountryCode" runat="server" Width="38px"></asp:TextBox>
108.                        </td>
109.                    </tr>
110.                    <tr>
111.                        <td>
112.                            &nbsp;<asp:Label id="lblBudget" runat="server" text="Budget"></asp:Label></td>
113.                        <td>
114.                            &nbsp;<asp:TextBox id="txtBudget" runat="server" Width="76px"></asp:TextBox>
115.                        </td>
116.                    </tr>
117.                    <tr>
118.                        <td>
119.                            &nbsp;<asp:Label id="lblUsed" runat="server" text="Used"></asp:Label></td>
120.                        <td>
121.                            &nbsp;<asp:TextBox id="txtUsed" runat="server" Width="76px"></asp:TextBox>
122.                        </td>
123.                    </tr>
124.                </tbody>
125.            </table>
126.            <br />
127.            <asp:Button id="btnSave" onclick="btnSave_Click" runat="server" Text="Save"></asp:Button>
128.            <br />
129.        </asp:Panel>
130.        <asp:Label id="lblStatus" runat="server" visible="True"></asp:Label>
131.    </form>
132.</body>
133.</html>




Screenshot

ASP.NET & Microsoft Access

ASP.NET & Microsoft Access


ASP.NET System.Data.OleDb - Parameter Query


   
Hate it
Don't like it
It's ok
Like it
Love it
Total Votes: 606Overall Rating: 3.9 / 5
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-26 22:45:26 / 2017-03-29 09:49:46
  Download : Download  ASP.NET Microsoft Access Edit/Update Record
 Sponsored Links / Related

 
ASP.NET Microsoft Access Connect to Database
Rating :

 
ASP.NET Microsoft Access List Table Properties
Rating :

 
ASP.NET Microsoft Access List Record
Rating :

 
ASP.NET Microsoft Access & Odbc (System.Data.Odbc)
Rating :

 
ASP.NET Microsoft Access Random Record
Rating :

 
ASP.NET Microsoft Access List Record Paging/Pagination
Rating :

 
ASP.NET Microsoft Access Search Record
Rating :

 
ASP.NET Microsoft Access Search Record Paging/Pagination
Rating :

 
ASP.NET Microsoft Access Add/Insert Record
Rating :

 
ASP.NET Microsoft Access Add-Insert Multiple Record
Rating :

 
ASP.NET Microsoft Access Check Already Exists Add/Insert Record
Rating :

 
ASP.NET Microsoft Access Transaction (BeginTransaction,Commit,Rollback)
Rating :

 
ASP.NET Microsoft Access Delete Record
Rating :

 
ASP.NET Microsoft Access Multiple Checkbox Delete Record
Rating :

 
ASP.NET Microsoft Access and GridView, DataSource
Rating :

 
ASP.NET Microsoft Access Database Class
Rating :

 
ASP.NET Microsoft Access Database Class (Visual Studio .Net 2003 - .NET 1.1)
Rating :

 
ASP.NET Microsoft Access Database Class (Visual Studio 2005,2008,2010 - .NET 2.0,3.5,4.0)
Rating :

 
ASP.NET Access BLOB Binary Data and Parameterized Query
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่