0

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)
    {

    }
}
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by crishlay
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,

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.