Groups > Databases > MySQL for ASP.NET > Re: GridView Update with MySql




GridView Update with MySql

GridView Update with MySql
Thu, 28 Feb 2008 01:23:39 +000
I am trying to display data from a table in my DB using a gridview but I want to
be able to edit the data using the update command that comes with the GridView.
Is this possible when using MySql or can it only be used with Sql Server and if
so can someone point me where I can get information on how to?

Thanks in advance
Post Reply
Re: GridView Update with MySql
Thu, 28 Feb 2008 01:51:25 +000
The SQLDataSource object supports any OLEDB or ODBC data sources so you should
be able to use MySQL in the same manner, provided you connect via ODBC.  You
will need the MySQL Connector/ODBC driver (google for it).
Post Reply
Re: GridView Update with MySql
Thu, 28 Feb 2008 02:05:25 +000
I have this downloaded but I'm not sure how to use it. Is there any chance you
could look at my code and see what obvious errors I'm making? when I try to use
'using MySql.Data.MySqlClient;' it doesn't recognise it. should I be using this
or an ODBC one?


 

public partial class Admin_Teams : System.Web.UI.Page
{
    public DataSet myDataSet;

    protected void Page_Load(object sender, EventArgs e)
    {

        string ConnectionString = ("Driver={MySQL ODBC 3.51
Driver};Server=localhost;Database=KDL;User=root;Password="";");
        string CommandText = "SELECT * FROM team";
        
        MySqlConnection myConnection = new MySqlConnection();
        MySqlCommand myCommand = new MySqlCommand(CommandText, myConnection);

        //create a new DataAdapter
        MySqlDataAdapter myAdapter = new MySqlDataAdapter();

        myAdapter.SelectCommand = myCommand;
        myDataSet = new DataSet();

        try
        {
            //open connection to the database
            myConnection.Open();

            //Use the DataAdapter to fill the DataSet
            myAdapter.Fill(myDataSet, "team");
          
            GridView1.DataSource = myDataSet;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            
            myConnection.Close();
        }
Post Reply
Re: GridView Update with MySql
Thu, 28 Feb 2008 02:12:12 +000
If you're going to hand code like this, I would go ahead and use the connector
you're using (Connector/NET).  I find it to be easier to use.  You need to add a
reference to the DLL.  Right click your project and select Add Reference.  In
the .NET list, search for MySQL.Data.  Then add using MySql.Data to the top of
your class.  Also, if you're using Connector/NET then your connection string
should just be:
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Post Reply
Re: GridView Update with MySql
Thu, 28 Feb 2008 02:58:56 +000
Thanks for your help. I have done what you said but still can't get the gridview
to work. Is there anything else you know of that I'm not doing?
Post Reply
about | contact