Dispaly Data from MySQL 5.5.16 using ASP.NET 3.5

Here we have to persent an ASP.NET web page connected to the MySQL server with the help of Mysql.Data.MysqlClient. It is a connector provider which we can use with .NET framework applications.

Overview 

Here first of all we create an ASP.NET 3.5 Website Project in Visual Studio 2008 and create a gridview ASP.NET control on to the default.aspx page. Then we build the web site project and display the data in the gridview on the default.aspx page.

Create an ASP.NET 3.5 Web Site Project

Start Visual Studio 2008 and Click File > New >Web Site... to open up the New Web Site window as shown. Here we have to change the default name of the web site . I choose mysql2 as shown.

img 1.gif
 

Take a Gridview

Under Toolbox  we find the GridView Control. Drag and drop this control on to the Default.aspx page and under the task menu items choose data source.

image.gif

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.

image3.gif

Here in this wizard 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 SQLDataSource1 as our source of data. Click OK.

After the ok button the next window 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. Here in we will be making a new connection. Click the New Connection... button.

img 4.gif

This brings up the Add Connection window which gets displayed with the default data source, the Microsoft SQL Server Compact 3.5 as shown.

img 5.gif
 

Connecting to MySQL

Before establishing the connection make sure that our MySQL server is running. we can start the server from the command line as shown in the next figure.

img 20.gif

Click the Change... button to open the Change Data Source window as shown in the next figure.

img 6.gif


This window shows a number of Data Sources one of which is the MySQL Database and click OK. This will bring we back to the Add Connection window with form controls appropriate for making a connection to a MySQL Database.

img 7.gif


The Server name; user name and Password are appropriate to the MySQL Server on the local computer and you should enter those appropriate for our installation. we may also test the connection as shown. Click OK after the connection is successful. This adds the connection information to the Configure Data Source wizard. we may expand the connection string item to review the connection string created by our entries.

img 8.gif

Click Next. Here shown an option to save the connection string to the Application Configuration File. This is a recommended practice and hence shown checked.

img 9.gif

Click Next. Here we will be selecting the set of columns that you want to bring in to your application. It has already chosen the 'persons' table on the MySQL database mcn .

img 10.gif


Choose several columns from the list of columns. The SELECT statement is shown at the bottom of the above figure. If i want to click Next then would be probably face a page which throws an exception. The square braces [ ] shown for each of the columns is not acceptable to the server.  Click on the first option, "Specify a custom SQL Statement or stored procedure" and then click Next. This opens the "Define Custom Statements or Stored Procedures" page with a Query Builder... button. Here you can not only select columns but also other data modification operations such as Update, Insert and Delete. For now we will be doing just a selection.

img 11.gif

Click OK to the Query Builder and click Next. This brings you to the page where you can test the query and review the results as shown.

img 12.gif

Click Finish and returned to the Default.aspx page. The GridView now has the column names we chose in  configuring SQLDataSource1. The smart tasks list has grown with some additional tasks. From adding new columns to editing the template you can carry out additional tasks.

img 14.gif

In the GridView's properties window we can set many of the formatting properties.

img 15.gif

In Auto Format... link at the bottom of the above window we can change auto format to choose a scheme for the window as shown.

img 16.gif

After choosing the scheme build the project and browse to the Default.aspx  page on browser to display the page as shown given below.

img 17.gif

In order to implement sorting of the data by column headers you need to choose the option Enable Sorting at design time as shown.

img 18.gif

After you download and run the MySQL connector program you will find the MySQL.Data in the References folder of Visual Studio as shown above. Even the splash screen of Visual Studio shows MySQL as one of the products with which it is integrated.

img 19.gif


Similar Articles