Introduction
This article shows how to bind data from a MySQL database to a GridView in ASP.Net 4.5.
Use
- ASP .NET web page
- MySQL Database
- GridView
- MySQLCommand ,DataSet and MySqlDataAdapter
See the following screen; select "MySQL Wamp Server database" > "customers data".

Create a new project using "File" -> "New" -> "Project..." then select web "ASP .Net Web Forms Application". Name it "GridViewBindMySql".

Now in the Design page “Default.aspx” design the web page as in the following screen:

In the code behind file (Default.aspx.cs) write the code as:
Default.aspx.cs
- <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridViewBindMySql._Default" %>
- <asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
- <section class="featured">
- <div class="content-wrapper">
- <hgroup class="title">
- <h2>Bind Data from MySql to GridView using DataSet and MySqlDataAdapter in Asp .NET 4.5
- </h2>
- </hgroup>
- <p>
- To learn more about ASP.NET 4.5 ,
- </p>
- </div>
- </section>
- </asp:Content>
- <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
- <h3>We suggest the following:</h3>
- <asp:Panel ID="Panel1" runat="server" Width="100%" ScrollBars="Horizontal">
- <p>
- <asp:Button ID="btnBind" runat="server" Text="View" OnClick="btnBind_Click" /> <asp:Label ID="Label1" runat="server" Font-Bold="true" ForeColor="Green" Text="Total Customers:"> </asp:Label><asp:Label ID="lbltotalcount" runat="server" ForeColor="Red" Font-Size="Larger"></asp:Label> </p>
- <asp:GridView ID="grvCustomers" runat="server"></asp:GridView>
- </asp:Panel>
- </asp:Content>
In the Web.config file create the connection string as:
Web.config
- <connectionStrings>
- <add name="ConnectionString" connectionString="Server=localhost;userid=root;password=;Database=northwind" providerName="MySql.Data.MySqlClient"/>
- </connectionStrings>
Default.aspx.cs


Join the conversation! Your thoughts help the community grow.