web 2.0

ASP.NET(vb.net) & Repeater - MySQL Database - MySql.Data.MySqlClient

ASP.NET(vb.net) & Repeater - MySQL Database - MySql.Data.MySqlClient Example scripts how to use Repeater control in asp.net , Using Connector MySql.Data.MySqlClient namespace is the .NET Framework Data Provider for MySQL,  how to the connect to MySQL Database.

ShotDev Focus:
- ASP.NET(vb.net) & Repeater - MySQL Database - MySql.Data.MySqlClient

Example

Repeater1.aspx

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Repeater1.aspx.vb" Inherits="Repeater1" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7. <title>ShotDev.Com Tutorial</title>  
  8. </head>  
  9. <body>  
  10. <form id="form1" runat="server">  
  11. <asp:Repeater id="myRepeater" runat="server">  
  12. <HeaderTemplate>  
  13. <table border="1">  
  14. <tr>  
  15. <th>CustomerID</th>  
  16. <th>Name</th>  
  17. <th>Email</th>  
  18. <th>CountryCode</th>  
  19. <th>Budget</th>  
  20. <th>Used</th>  
  21. </tr>  
  22. </HeaderTemplate>  
  23. <ItemTemplate>  
  24. <tr>  
  25. <td align="center"><asp:Label id="lblCustomerID" runat="server"></asp:Label></td>  
  26. <td><asp:Label id="lblName" runat="server"></asp:Label></td>  
  27. <td><asp:Label id="lblEmail" runat="server"></asp:Label></td>  
  28. <td align="center"><asp:Label id="lblCountryCode" runat="server"></asp:Label></td>  
  29. <td align="right"><asp:Label id="lblBudget" runat="server"></asp:Label></td>  
  30. <td align="right"><asp:Label id="lblUsed" runat="server"></asp:Label></td>  
  31. </tr>  
  32. </ItemTemplate>  
  33. <FooterTemplate>  
  34. <!--  
  35. <tr>  
  36. <th>CustomerID</th>  
  37. <th>Name</th>  
  38. <th>Email</th>  
  39. <th>CountryCode</th>  
  40. <th>Budget</th>  
  41. <th>Used</th>  
  42. </tr>  
  43. -->  
  44. </table>  
  45. </FooterTemplate>  
  46. </asp:Repeater>  
  47. </form>  
  48. </body>  
  49. </html>  

Repeater1.aspx.vb

  1. Imports System.Data  
  2. Imports MySql.Data.MySqlClient  
  3. Partial Class Repeater1  
  4. Inherits System.Web.UI.Page  
  5. Dim objConn As MySqlConnection  
  6. Dim objCmd As MySqlCommand  
  7.   
  8. Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  9. Dim strConnString As String  
  10. strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"  
  11. objConn = New MySqlConnection(strConnString)  
  12. objConn.Open()  
  13.   
  14. BindData()  
  15. End Sub  
  16.   
  17. Protected Sub BindData()  
  18. Dim strSQL As String  
  19. strSQL = "SELECT * FROM customer"  
  20.   
  21. Dim dtReader As MySqlDataReader  
  22. objCmd = New MySqlCommand(strSQL, objConn)  
  23. dtReader = objCmd.ExecuteReader()  
  24.   
  25. '*** BindData to Repeater ***'  
  26. myRepeater.DataSource = dtReader  
  27. myRepeater.DataBind()  
  28.   
  29. dtReader.Close()  
  30. dtReader = Nothing  
  31. End Sub  
  32.   
  33. Protected Sub myRepeater_ItemDataBound(ByVal sender As ObjectByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound  
  34.   
  35. '*** CustomerID ***'  
  36. Dim lblCustomerID As Label = CType(e.Item.FindControl("lblCustomerID"),Label)  
  37. IF Not IsNothing(lblCustomerID) Then  
  38. lblCustomerID.Text = e.Item.DataItem("CustomerID")  
  39. End IF  
  40.   
  41. '*** Name ***'  
  42. Dim lblName As Label = CType(e.Item.FindControl("lblName"),Label)  
  43. IF Not IsNothing(lblName) Then  
  44. lblName.Text = e.Item.DataItem("Name")  
  45. End IF  
  46.   
  47. '*** Email ***'  
  48. Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"),Label)  
  49. IF Not IsNothing(lblEmail) Then  
  50. lblEmail.Text = e.Item.DataItem("Email")  
  51. End IF  
  52.   
  53. '*** CountryCode ***'  
  54. Dim lblCountryCode As Label = CType(e.Item.FindControl("lblCountryCode"),Label)  
  55. IF Not IsNothing(lblCountryCode) Then  
  56. lblCountryCode.Text = e.Item.DataItem("CountryCode")  
  57. End IF  
  58.   
  59. '*** Budget ***'  
  60. Dim lblBudget As Label = CType(e.Item.FindControl("lblBudget"),Label)  
  61. IF Not IsNothing(lblBudget) Then  
  62. lblBudget.Text = e.Item.DataItem("Budget")  
  63. End IF  
  64.   
  65. '*** Used ***'  
  66. Dim lblUsed As Label = CType(e.Item.FindControl("lblUsed"),Label)  
  67. IF Not IsNothing(lblUsed) Then  
  68. lblUsed.Text = e.Item.DataItem("Used")  
  69. End If  
  70.   
  71. End Sub  
  72.   
  73. Protected Sub Page_Unload(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Unload  
  74. objConn.Close()  
  75. objConn = Nothing  
  76. End Sub  
  77.   
  78. End Class  

Create a asp.net file and save to path root-path/dotnet/

Run
http://localhost/dotnet/Repeater1.aspx

Screenshot

ASP.NET(vb.net) & Repeater - MySQL Database - MySql.Data.MySqlClient
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.