How to Bind Data in MVC Application With MySQL Server in GridView

We will discuss how to bind data to a GridView using MySQL Server. Here we will explain an MVC application; we will create a database schema in MySQL. In Visual Studio we added a connection to our database.
Here we have to start by creating a new ASP.Net MVC Application and bind the data through GridView with MySQL server database.

We have to follow these steps to create a new ASP.Net MVC web Application.

Step 1: Launch Visual Studio 2010.
Step 2: Select menu options File->New Project.
Step 3: In the new project, select a language and select the ASP.Net MVC Web Application.

first open.gif

Step 4:
Under the Toolbox we find the GridView Control. Drag and drop this control onto the Index.aspx page and under the task menu items choose data source.


Step 5:
Click on the <New Data Source...> item in Choose data source. This will bring up the Data Source Configuration wizard. Here we need to choose a source of the data we are trying to bring into the application to be bound to the GridView control.

mvc 01.gif

Step 6: Here in this wizard are several options and we will be using data from a database. Click on the Database icon as shown in the previous figure. With this we will be specifying an instance of SQLDataSource2 as our source of data. Click OK.

After the ok button the next window is shown here. Here we will try to establish a connection to the data source. In the combo-box shown you may see some of the existing connections you have previously established, one of which may initially show up. From here on we will be making a new connection. Click the New Connection... button.

mvc 02.gif

Step 7: Enter the information to connect the selected data source and test connection.

mvc 1.gif

Step 8:
Select the connection we have to made earlier to the mcn database. If not already done so, we can create the new connection at that time by clicking New connection.

mvc 2.gif

Step 9: After selecting the table we also have to select the columns to be printed.

mvc 3.gif

Step 10:  Define custom statements which are given below.

mvc 4.gif

Step 11: Here we are to find many tables attached to select the database (mcn) and select a table.

mvc 5.gif

Step 12: After selecting a table we have to bulid the query and test query.

mvc 6.gif 

Step 13: Build the application and run in browser see as below.

mvc 7.gif


Here are some useful resources:

Dispaly Data from MySQL 5.5.16 using ASP.NET 3.5
Bind data on Gridview using MVC
Repeater within ASP.NET 2.0 Gridview
Customizing GridView control for data modifications
Server side evaluation of Gridview Data