web 2.0

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

ASP.NET(vb.net) & ListView - MySQL Database - MySql.Data.MySqlClient Example scripts how to use ListView 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) & ListView - MySQL Database - MySql.Data.MySqlClient



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ListView1.aspx.vb" Inherits="ListView1" %>

<%@ Register assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.WebControls" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ShotDev.Com Tutorial</title>
<form id="form1" runat="server">
<asp:ListView ID="myListView" runat="server" DataKeyNames="CustomerID">

<table id="Table1" runat="server" border="1">
<th id="Th1" runat="server">
<th id="Th2" runat="server">
<th id="Th3" runat="server">
<th id="Th4" runat="server">
<th id="Th5" runat="server">
<th id="Th6" runat="server">
<th id="Th7" runat="server">
<tr ID="itemPlaceholder" runat="server">

<asp:CheckBox id="chkCustomerID" runat="server"/>
<asp:Label ID="lblCustomerID" runat="server"/>
<asp:Label ID="lblName" runat="server"/>
<asp:Label ID="lblEmail" runat="server"/>
<asp:Label ID="lblCountryCode" runat="server" />
<asp:Label ID="lblBudget" runat="server" />
<asp:Label ID="lblUsed" runat="server" />


<br />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
<hr />
<asp:Label id="lblText" runat="server"></asp:Label>


Imports System.Data
Imports MySql.Data.MySqlClient

Partial Class ListView1
Inherits System.Web.UI.Page

Dim objConn As MySqlConnection
Dim objCmd As MySqlCommand

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnString As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
objConn = New MySqlConnection(strConnString)

If Not Page.IsPostBack() Then
End If
End Sub

Protected Sub BindData()
Dim strSQL As String
strSQL = "SELECT * FROM customer"

Dim dtReader As MySqlDataReader
objCmd = New MySqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()

'*** BindData to ListView ***'
myListView.DataSource = dtReader

dtReader = Nothing

End Sub

Sub Page_UnLoad()
objConn = Nothing
End Sub

Protected Sub myListView_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles myListView.ItemDataBound

Dim lvDataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)

'*** CustomerID ***'
Dim lblCustomerID As Label = CType(e.Item.FindControl("lblCustomerID"), Label)
If Not IsNothing(lblCustomerID) Then
lblCustomerID.Text = lvDataItem.DataItem("CustomerID")
End If

'*** Name ***'
Dim lblName As Label = CType(e.Item.FindControl("lblName"), Label)
If Not IsNothing(lblName) Then
lblName.Text = lvDataItem.DataItem("CustomerID")
End If

'*** Email ***'
Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"), Label)
If Not IsNothing(lblEmail) Then
lblEmail.Text = lvDataItem.DataItem("Email")
End If

'*** CountryCode ***'
Dim lblCountryCode As Label = CType(e.Item.FindControl("lblCountryCode"), Label)
If Not IsNothing(lblCountryCode) Then
lblCountryCode.Text = lvDataItem.DataItem("CountryCode")
End If

'*** Budget ***'
Dim lblBudget As Label = CType(e.Item.FindControl("lblBudget"), Label)
If Not IsNothing(lblBudget) Then
lblBudget.Text = lvDataItem.DataItem("Budget")
End If

'*** Used ***'
Dim lblUsed As Label = CType(e.Item.FindControl("lblUsed"), Label)
If Not IsNothing(lblUsed) Then
lblUsed.Text = lvDataItem.DataItem("Used")
End If
End Sub

Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim chkCusID As CheckBox
Dim lblID As Label
Dim i As Integer
lblText.Text = ""
For i = 0 To myListView.Items.Count - 1
chkCusID = myListView.Items(i).FindControl("chkCustomerID")
lblID = myListView.Items(i).FindControl("lblCustomerID")
If chkCusID.Checked = True Then
'*** Have lblID.Text ***'
Me.lblText.Text = Me.lblText.Text & "<br>" & lblID.Text
End If
End Sub

End Class

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



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

Download this script.

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

One Response to “ASP.NET(vb.net) & ListView - MySQL Database - MySql.Data.MySqlClient”

  1. 2interwoven…

Leave a Reply

You must be logged in to post a comment.