Friday, January 7, 2011

How to Connect GridView to MySql

First of all u need to install the MySql Connecter(MySql.Data).
Then add the MySql.Data , by rightClicking on References , in the Solution Explorer.

MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["NetsGate"].ToString());


//select data from database
  MySqlCommand cmdGetData = new MySqlCommand("SELECT * FROM Employee",conn);

                conn.Open();
                MySqlDataAdapter daGetData = new MySqlDataAdapter(cmdGetData);
                DataSet dsGetData = new DataSet();
                daGetData.Fill(dsGetData);
                conn.Close();

                GridView1.DataSource = dsGetData;
                GridView1.DataBind();


Don't forget declare the connection String in the web.Config

Place this tag within the <connectionStrings> tag in web.Config
<add name="NetsGate" connectionString="SERVER=localhost;UID=root;DATABASE=leave_mub;PASSWORD="
   providerName="System.Data.SqlClient" />



Hope this will be helpfull for u guys ........................