Display Mysql data using ASP.NET C#
Step 1) Download mysql connector for .net framework 2.0 from mysql website
Step 2)Open visual studio editor and
add reference the following dll file that may be located following path
C:\Program
Files\MySQL\MySQL Connector Net 5.1.7\Binaries\.NET 2.0\ MySql.Data.dll
Step 3)Now write the following code in
aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mysql.aspx.cs" Inherits="mysql" %>
<!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>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="403px">
<RowStyle BackColor="White" ForeColor="#003399" />
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
</div>
</form>
</body>
</html>
Step 4) Write following code in code
behind of your ASP.Net page
using System;
using System.Data;
using
System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
public partial class mysql : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
string cnnString ="Server=localhost;Port=3306;Database=cdcol;Uid=root;Pwd=";
//
Create a connection object and data adapter
MySqlConnection
cnx = new MySqlConnection(cnnString);
MySqlDataAdapter
adapter = new MySqlDataAdapter();
MySqlDataReader
rd;
//
Create a SQL command object
string
cmdText = "SELECT * FROM cds";
MySqlCommand
cmd = new MySqlCommand(cmdText,
cnx);
////Bind
data in grid view.
DataSet
ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
this.GridView1.DataSource
= ds;
this.GridView1.DataBind();
}
}
N.B :- Don’t forget to modify database
server name, Database name and table name according to your setting
No comments:
Post a Comment