Grid View - MySQL
I attempted to get the records from the MySQL database and display it on a Grid View but nothing appeared. The code is below:
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" +
"DATABASE=mysql;" +
"UID=root;" +
"PASSWORD=bamboboy;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select * from corporate";
connection.Open();
MySqlDataAdapter myadapter = new MySqlDataAdapter(command);
DataSet dts = new DataSet();
myadapter.Fill(dts);
GridView1.DataSource = dts.Tables[0];
connection.Close();
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
}
solomon_13000
Junior Poster in Training
88 posts since Jul 2009
Reputation Points: 24
Solved Threads: 0
You need to call dataBind() on the GridView to actually make it display the data. All you have done is tell what its data source is. Also, you don't need to specifically open and close a connection for a dataAdapter, it takes care of that itself so you're using two connections here when you don't have to.
Hope that helps,
hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 168
Have you checked to make sure the dataTable actually has data in it?
hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 168
Yes it works. Upon binding the data to the grid, I can now see the data.
solomon_13000
Junior Poster in Training
88 posts since Jul 2009
Reputation Points: 24
Solved Threads: 0