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 DataList Control > ASP.NET DataList Control - MySQL Database - MySql.Data.MySqlClient


ASP.NET DataList Control - MySQL Database - MySql.Data.MySqlClient

ASP.NET DataList & MySQL เป็นตัวอย่างการเขียน ASP.NET โดยใช้ Tool ชื่อ DataList กับฐานข้อมูล MySQL ใช้ NameSpace ของ MySql.Data.MySqlClient เพื่อนำข้อมูลในฐานข้อมูลมาใช้ในรูปแบบต่าง ๆ ตัวอย่างนี้ผมพัฒนาบน .NET Framework 2 และ 3 (Visual Studio 2005,2008)

Language Code : VB.NET || C#

Framework : 2,3,4


DataList1.aspx

01.<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DataList1.aspx.vb" Inherits="DataList1" %>
02. 
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04. 
05.<html xmlns="http://www.w3.org/1999/xhtml" >
06.<head runat="server">
07.    <title>ThaiCreate.Com ASP.NET - DataList & MySQL</title>
08.</head>
09.<body>
10.    <form id="form1" runat="server" enctype="multipart/form-data">
11.    <asp:DataList id="myDataList"
12.    runat="server" RepeatColumns="2"   
13.    cellpadding="2"
14.    cellspacing="2"
15.    borderstyle="inset">
16.        <HeaderTemplate>
17.            <b>My Category</b><br/>
18.        </HeaderTemplate>
19.        <ItemTemplate>
20.            <div style="width:100px" align="center">
21.                <asp:Image id="imgPicture" runat="server"></asp:Image>
22.                <br />
23.                <asp:HyperLink id="hplCategory" runat="server"></asp:HyperLink>
24.                <br />
25.                <asp:LinkButton id="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
26.            </div>
27.        </ItemTemplate>
28.        <EditItemTemplate>
29.          <asp:Label id="lblCateID" runat="server" Visible = "False" text='<%#Container.DataItem("CategoryID")%>'></asp:Label>   
30.          <asp:TextBox id="txtCategory" runat="server" text='<%#Container.DataItem("CategoryName")%>'></asp:TextBox><br />
31.          <input id="filPicture" type="file" runat="server"><br />
32.          &nbsp;<asp:LinkButton id="lnkUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
33.          <asp:LinkButton id="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
34.        </EditItemTemplate>
35.    </asp:DataList>
36.    </form>
37.</body>
38.</html>



DataList1.aspx.vb

01.Imports system.Data
02.Imports MySql.Data.MySqlClient
03.Partial Class DataList1
04.    Inherits System.Web.UI.Page
05.    Dim objConn As MySqlConnection
06.    Dim objCmd As MySqlCommand
07.    Dim strSQL As String
08. 
09.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
10.        Dim strConnString As String
11.        strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
12.        objConn = New MySqlConnection(strConnString)
13.        objConn.Open()
14. 
15.        If Not Page.IsPostBack() Then
16.            BindData()
17.        End If
18.    End Sub
19. 
20.    Protected Sub BindData()
21.        strSQL = "SELECT * FROM category"
22. 
23.        Dim dtReader As MySqlDataReader
24.        objCmd = New MySqlCommand(strSQL, objConn)
25.        dtReader = objCmd.ExecuteReader()
26. 
27.        '*** BindData to DataList ***'
28.        myDataList.DataSource = dtReader
29.        myDataList.DataBind()
30. 
31.        dtReader.Close()
32.        dtReader = Nothing
33. 
34.    End Sub
35. 
36.    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
37.        objConn.Close()
38.        objConn = Nothing
39.    End Sub
40. 
41.    Protected Sub myDataList_CancelCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles myDataList.CancelCommand
42.        myDataList.EditItemIndex = -1
43.        BindData()
44.    End Sub
45. 
46.    Protected Sub myDataList_EditCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles myDataList.EditCommand
47.        myDataList.EditItemIndex = e.Item.ItemIndex
48.        BindData()
49.    End Sub
50. 
51.    Protected Sub myDataList_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) Handles myDataList.ItemDataBound
52.        '*** Image ***'
53.        Dim img As Image = CType(e.Item.FindControl("imgPicture"), Image)
54.        If Not IsNothing(img) Then
55.            img.ImageUrl = e.Item.DataItem("Picture")
56.            'img.Attributes.Add("OnClick","window.location='https://www.thaicreate.com?CateID="&e.Item.DataItem("CategoryID")&"'")
57.            'img.Style.Add("cursor","hand")
58.        End If
59. 
60.        '*** HyperLink ***'    
61.        Dim hplCate As HyperLink = CType(e.Item.FindControl("hplCategory"), HyperLink)
62.        If Not IsNothing(hplCate) Then
63.            hplCate.Text = e.Item.DataItem("CategoryName")
64.            hplCate.ToolTip = e.Item.DataItem("CategoryName")
65.            hplCate.NavigateUrl = "https://www.thaicreate.com?CateID=" & e.Item.DataItem("CategoryID")
66.        End If
67.    End Sub
68. 
69.    Protected Sub myDataList_UpdateCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles myDataList.UpdateCommand
70.        '*** CategoryID ***'
71.        Dim lblCateID As Label = CType(e.Item.FindControl("lblCateID"), Label)
72.        '*** txtCategory ***'
73.        Dim txtCategory As TextBox = CType(e.Item.FindControl("txtCategory"), TextBox)
74. 
75. 
76.        strSQL = "UPDATE category SET CategoryName = '" & txtCategory.Text & "' " & _
77.        " WHERE CategoryID = " & lblCateID.Text & " "
78.        objCmd = New MySqlCommand(strSQL, objConn)
79.        objCmd.ExecuteNonQuery()
80. 
81.        '*** If Select File Upload ***'
82.        Dim filPicture As HtmlInputFile = CType(e.Item.FindControl("filPicture"), HtmlInputFile)
83.        Dim strFileName As String
84.        If Trim(filPicture.PostedFile.FileName) <> "" Then
85.            strFileName = System.IO.Path.GetFileName(filPicture.Value)
86.            filPicture.PostedFile.SaveAs(Server.MapPath("images/" & strFileName))
87.            strSQL = "UPDATE category SET Picture = 'images/" & strFileName & "' " & _
88.            " WHERE CategoryID = " & lblCateID.Text & " "
89.            objCmd = New MySqlCommand(strSQL, objConn)
90.            objCmd.ExecuteNonQuery()
91.        End If
92. 
93.        myDataList.EditItemIndex = -1
94.        BindData()
95.    End Sub
96.End Class




Screenshot

ASP.NET DataList & MySQL

ASP.NET DataList & MySQL


ASP.NET & MySql.Data.MySqlClient


ASP.NET & MySQL


   
Hate it
Don't like it
It's ok
Like it
Love it
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2009-09-22 19:30:55 / 2017-03-28 21:30:21
  Download : Download  ASP.NET DataList Control - MySQL Database - MySql.Data.MySqlClient
 Sponsored Links / Related

 
ASP.NET DataList Control
Rating :

 
ASP.NET DataList Control - DataBind
Rating :

 
ASP.NET DataList Control - Repeat Multiple Columns
Rating :

 
ASP.NET DataList Control - DataBound
Rating :

 
ASP.NET DataList Control - DataSet,DataTable,TableRows
Rating :

 
ASP.NET DataList Control - FindControl
Rating :

 
ASP.NET DataList Control - ItemCommand
Rating :

 
ASP.NET DataList Control - XML
Rating :

 
ASP.NET DataList Control - AccessDataSource
Rating :

 
ASP.NET DataList Control - VS .NET 2003 (FX 1.1)
Rating :

 
ASP.NET DataList Control - ItemCommand VS .NET 2003 (FX 1.1)
Rating :

 
ASP.NET DataList Control - VS 2005,2008,2010 (FX 2.0,3.5,4.0)
Rating :

 
ASP.NET DataList Control - ItemCommand VS 2005,2008,2010 (FX 2.0,3.5,4.0)
Rating :

 
ASP.NET DataList Control - Microsoft Access (.mdb) - System.Data.OleDb
Rating :

 
ASP.NET DataList Control - SQL Server 2000,2005,2008 - System.Data.SqlClient
Rating :

 
ASP.NET DataList Control - Oracle Database - System.Data.OracleClient
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 อัตราราคา คลิกที่นี่